Coverage index

src/Plugin/views/field/VisitorsTimestamp.php

Lines: 100.00%

LineHitsSource
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) {
4114
    parent::__construct($configuration, $plugin_id, $plugin_definition);
4214
    $this->configFactory = $config_factory;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
4914
    return new static(
5014
      $configuration,
5114
      $plugin_id,
5214
      $plugin_definition,
5314
      $container->get('config.factory'),
5414
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function query() {
611
    $this->ensureMyTable();
62
631
    $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
64
    /** @var \Drupal\views\Plugin\views\query\Sql $query */
651
    $query = $this->query;
66
671
    $timezone_location = $this->configFactory->get('system.date')->get('timezone.default');
68
691
    $timezone = new \DateTimeZone($timezone_location);
701
    $offset = $timezone->getOffset(new \DateTime());
71
721
    $field = $query->getDateField("$this->tableAlias.$this->realField", FALSE, FALSE);
731
    $query->setFieldTimezoneOffset($field, $offset);
741
    $formula = $query->getDateFormat($field, $this->getFormat(), FALSE);
75
761
    $this->field_alias = $query->addField(NULL, $formula, $this->field, $params);
77
781
    $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() {
889
    return $this->format;
89
  }
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function getCacheTags() {
951
    return ['config:system.date'];
96
  }
97
98
}