Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\views\field; | |
| 4 | ||
| 5 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 6 | use Drupal\views\Plugin\views\field\FieldPluginBase; | |
| 7 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 8 | ||
| 9 | /** | |
| 10 | * Base field handler to format timestamps. | |
| 11 | */ | |
| 12 | abstract class VisitorsTimestamp extends FieldPluginBase { | |
| 13 | ||
| 14 | /** | |
| 15 | * The format of the date. | |
| 16 | * | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | protected $format; | |
| 20 | ||
| 21 | /** | |
| 22 | * The config factory service. | |
| 23 | * | |
| 24 | * @var \Drupal\Core\Config\ConfigFactoryInterface | |
| 25 | */ | |
| 26 | protected $configFactory; | |
| 27 | ||
| 28 | /** | |
| 29 | * Constructs a VisitorsTimestamp object. | |
| 30 | * | |
| 31 | * @param array $configuration | |
| 32 | * The configuration. | |
| 33 | * @param string $plugin_id | |
| 34 | * The plugin ID. | |
| 35 | * @param mixed $plugin_definition | |
| 36 | * The plugin definition. | |
| 37 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
| 38 | * The config factory service. | |
| 39 | */ | |
| 40 | public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) { | |
| 41 | 14 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 42 | 14 | $this->configFactory = $config_factory; |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * {@inheritdoc} | |
| 47 | */ | |
| 48 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
| 49 | 14 | return new static( |
| 50 | 14 | $configuration, |
| 51 | 14 | $plugin_id, |
| 52 | 14 | $plugin_definition, |
| 53 | 14 | $container->get('config.factory'), |
| 54 | 14 | ); |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * {@inheritdoc} | |
| 59 | */ | |
| 60 | public function query() { | |
| 61 | 1 | $this->ensureMyTable(); |
| 62 | ||
| 63 | 1 | $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : []; |
| 64 | /** @var \Drupal\views\Plugin\views\query\Sql $query */ | |
| 65 | 1 | $query = $this->query; |
| 66 | ||
| 67 | 1 | $timezone_location = $this->configFactory->get('system.date')->get('timezone.default'); |
| 68 | ||
| 69 | 1 | $timezone = new \DateTimeZone($timezone_location); |
| 70 | 1 | $offset = $timezone->getOffset(new \DateTime()); |
| 71 | ||
| 72 | 1 | $field = $query->getDateField("$this->tableAlias.$this->realField", FALSE, FALSE); |
| 73 | 1 | $query->setFieldTimezoneOffset($field, $offset); |
| 74 | 1 | $formula = $query->getDateFormat($field, $this->getFormat(), FALSE); |
| 75 | ||
| 76 | 1 | $this->field_alias = $query->addField(NULL, $formula, $this->field, $params); |
| 77 | ||
| 78 | 1 | $this->addAdditionalFields(); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Returns the format of the date. | |
| 83 | * | |
| 84 | * @return string | |
| 85 | * The format of the date. | |
| 86 | */ | |
| 87 | public function getFormat() { | |
| 88 | 9 | return $this->format; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * {@inheritdoc} | |
| 93 | */ | |
| 94 | public function getCacheTags() { | |
| 95 | 1 | return ['config:system.date']; |
| 96 | } | |
| 97 | ||
| 98 | } |