Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| VisitorsDayWeek | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| render | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| query | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| 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 | $first_day = $this->configFactory->get('system.date')->get('first_day') ?? 0; |
| 27 | |
| 28 | $weekdays = []; |
| 29 | $weekdays[((0 + 7 - $first_day) % 7)] = $this->t('Sunday'); |
| 30 | $weekdays[((1 + 7 - $first_day) % 7)] = $this->t('Monday'); |
| 31 | $weekdays[((2 + 7 - $first_day) % 7)] = $this->t('Tuesday'); |
| 32 | $weekdays[((3 + 7 - $first_day) % 7)] = $this->t('Wednesday'); |
| 33 | $weekdays[((4 + 7 - $first_day) % 7)] = $this->t('Thursday'); |
| 34 | $weekdays[((5 + 7 - $first_day) % 7)] = $this->t('Friday'); |
| 35 | $weekdays[((6 + 7 - $first_day) % 7)] = $this->t('Saturday'); |
| 36 | |
| 37 | $value = (int) $this->getValue($values); |
| 38 | |
| 39 | // $output = $weekdays[$value]; |
| 40 | $output = [ |
| 41 | '#markup' => $weekdays[$value], |
| 42 | ]; |
| 43 | // Add cache metadata. |
| 44 | $cache_metadata = new CacheableMetadata(); |
| 45 | $cache_metadata->addCacheTags(['config:system.date']); |
| 46 | $cache_metadata->applyTo($output); |
| 47 | |
| 48 | return $output; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * {@inheritdoc} |
| 53 | */ |
| 54 | public function query() { |
| 55 | $this->ensureMyTable(); |
| 56 | |
| 57 | $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : []; |
| 58 | /** @var \Drupal\views\Plugin\views\query\Sql $query */ |
| 59 | $query = $this->query; |
| 60 | |
| 61 | // Default to Sunday (0) |
| 62 | $system_date = $this->configFactory->get('system.date'); |
| 63 | $first_day = $system_date->get('first_day') ?? 0; |
| 64 | |
| 65 | $timezone_location = $system_date->get('timezone.default'); |
| 66 | |
| 67 | $timezone = new \DateTimeZone($timezone_location); |
| 68 | $offset = $timezone->getOffset(new \DateTime()); |
| 69 | |
| 70 | $field = $query->getDateField("$this->tableAlias.$this->realField", FALSE, FALSE); |
| 71 | $query->setFieldTimezoneOffset($field, $offset); |
| 72 | $formula = $query->getDateFormat($field, $this->getFormat(), FALSE); |
| 73 | $sorted_expression = $formula; |
| 74 | if ($first_day != 0) { |
| 75 | $sorted_expression = "(($formula + 7 - $first_day) % 7)"; |
| 76 | } |
| 77 | |
| 78 | $this->field_alias = $query->addField(NULL, $sorted_expression, $this->field, $params); |
| 79 | |
| 80 | $this->addAdditionalFields(); |
| 81 | } |
| 82 | |
| 83 | } |