Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\views\field; | |
| 4 | ||
| 5 | use Drupal\Core\Cache\CacheableMetadata; | |
| 6 | use Drupal\views\Attribute\ViewsField; | |
| 7 | ||
| 8 | /** | |
| 9 | * Field handler to display the day of the week of the visit. | |
| 10 | * | |
| 11 | * @ingroup views_field_handlers | |
| 12 | */ | |
| 13 | #[ViewsField("visitors_day_of_week")] | |
| 14 | final class VisitorsDayWeek extends VisitorsTimestamp { | |
| 15 | ||
| 16 | /** | |
| 17 | * {@inheritdoc} | |
| 18 | */ | |
| 19 | protected $format = '%w'; | |
| 20 | ||
| 21 | /** | |
| 22 | * {@inheritdoc} | |
| 23 | */ | |
| 24 | public function render($values) { | |
| 25 | // Default to Sunday (0) | |
| 26 | 2 | $first_day = $this->configFactory->get('system.date')->get('first_day') ?? 0; |
| 27 | ||
| 28 | 2 | $weekdays = []; |
| 29 | 2 | $weekdays[((0 + 7 - $first_day) % 7)] = $this->t('Sunday'); |
| 30 | 2 | $weekdays[((1 + 7 - $first_day) % 7)] = $this->t('Monday'); |
| 31 | 2 | $weekdays[((2 + 7 - $first_day) % 7)] = $this->t('Tuesday'); |
| 32 | 2 | $weekdays[((3 + 7 - $first_day) % 7)] = $this->t('Wednesday'); |
| 33 | 2 | $weekdays[((4 + 7 - $first_day) % 7)] = $this->t('Thursday'); |
| 34 | 2 | $weekdays[((5 + 7 - $first_day) % 7)] = $this->t('Friday'); |
| 35 | 2 | $weekdays[((6 + 7 - $first_day) % 7)] = $this->t('Saturday'); |
| 36 | ||
| 37 | 2 | $value = (int) $this->getValue($values); |
| 38 | ||
| 39 | // $output = $weekdays[$value]; | |
| 40 | 2 | $output = [ |
| 41 | 2 | '#markup' => $weekdays[$value], |
| 42 | 2 | ]; |
| 43 | // Add cache metadata. | |
| 44 | 2 | $cache_metadata = new CacheableMetadata(); |
| 45 | 2 | $cache_metadata->addCacheTags(['config:system.date']); |
| 46 | 2 | $cache_metadata->applyTo($output); |
| 47 | ||
| 48 | 2 | return $output; |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * {@inheritdoc} | |
| 53 | */ | |
| 54 | public function query() { | |
| 55 | 2 | $this->ensureMyTable(); |
| 56 | ||
| 57 | 2 | $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : []; |
| 58 | /** @var \Drupal\views\Plugin\views\query\Sql $query */ | |
| 59 | 2 | $query = $this->query; |
| 60 | ||
| 61 | // Default to Sunday (0) | |
| 62 | 2 | $system_date = $this->configFactory->get('system.date'); |
| 63 | 2 | $first_day = $system_date->get('first_day') ?? 0; |
| 64 | ||
| 65 | 2 | $timezone_location = $system_date->get('timezone.default'); |
| 66 | ||
| 67 | 2 | $timezone = new \DateTimeZone($timezone_location); |
| 68 | 2 | $offset = $timezone->getOffset(new \DateTime()); |
| 69 | ||
| 70 | 2 | $field = $query->getDateField("$this->tableAlias.$this->realField", FALSE, FALSE); |
| 71 | 2 | $query->setFieldTimezoneOffset($field, $offset); |
| 72 | 2 | $formula = $query->getDateFormat($field, $this->getFormat(), FALSE); |
| 73 | 2 | $sorted_expression = $formula; |
| 74 | 2 | if ($first_day != 0) { |
| 75 | 1 | $sorted_expression = "(($formula + 7 - $first_day) % 7)"; |
| 76 | } | |
| 77 | ||
| 78 | 2 | $this->field_alias = $query->addField(NULL, $sorted_expression, $this->field, $params); |
| 79 | ||
| 80 | 2 | $this->addAdditionalFields(); |
| 81 | } | |
| 82 | ||
| 83 | } |