Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorsTimestamp
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 query
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 getFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCacheTags
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors\Plugin\views\field;
4
5use Drupal\views\Plugin\views\field\FieldPluginBase;
6use Symfony\Component\DependencyInjection\ContainerInterface;
7use Drupal\Core\Config\ConfigFactoryInterface;
8
9/**
10 * Base field handler to format timestamps.
11 */
12abstract 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    parent::__construct($configuration, $plugin_id, $plugin_definition);
42    $this->configFactory = $config_factory;
43  }
44
45  /**
46   * {@inheritdoc}
47   */
48  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49    return new static(
50      $configuration,
51      $plugin_id,
52      $plugin_definition,
53      $container->get('config.factory'),
54    );
55  }
56
57  /**
58   * {@inheritdoc}
59   */
60  public function query() {
61    $this->ensureMyTable();
62
63    $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
64    /** @var \Drupal\views\Plugin\views\query\Sql $query */
65    $query = $this->query;
66
67    $timezone_location = $this->configFactory->get('system.date')->get('timezone.default');
68
69    $timezone = new \DateTimeZone($timezone_location);
70    $offset = $timezone->getOffset(new \DateTime());
71    $field = $query->getDateField("$this->tableAlias.$this->realField", FALSE, FALSE);
72
73    $query->setFieldTimezoneOffset($field, $offset);
74    $formula = $query->getDateFormat($field, $this->getFormat(), FALSE);
75
76    $this->field_alias = $query->addField(NULL, $formula, $this->field, $params);
77
78    $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    return $this->format;
89  }
90
91  /**
92   * {@inheritdoc}
93   */
94  public function getCacheTags() {
95    return ['config:system.date'];
96  }
97
98}