datetime.views.inc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Provides views data for the datetime module.
  5. */
  6. use Drupal\field\FieldStorageConfigInterface;
  7. /**
  8. * Implements hook_field_views_data().
  9. */
  10. function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
  11. // @todo This code only covers configurable fields, handle base table fields
  12. // in https://www.drupal.org/node/2489476.
  13. $data = views_field_default_views_data($field_storage);
  14. foreach ($data as $table_name => $table_data) {
  15. // Set the 'datetime' filter type.
  16. $data[$table_name][$field_storage->getName() . '_value']['filter']['id'] = 'datetime';
  17. // Set the 'datetime' argument type.
  18. $data[$table_name][$field_storage->getName() . '_value']['argument']['id'] = 'datetime';
  19. // Create year, month, and day arguments.
  20. $group = $data[$table_name][$field_storage->getName() . '_value']['group'];
  21. $arguments = [
  22. // Argument type => help text.
  23. 'year' => t('Date in the form of YYYY.'),
  24. 'month' => t('Date in the form of MM (01 - 12).'),
  25. 'day' => t('Date in the form of DD (01 - 31).'),
  26. 'week' => t('Date in the form of WW (01 - 53).'),
  27. 'year_month' => t('Date in the form of YYYYMM.'),
  28. 'full_date' => t('Date in the form of CCYYMMDD.'),
  29. ];
  30. foreach ($arguments as $argument_type => $help_text) {
  31. $data[$table_name][$field_storage->getName() . '_value_' . $argument_type] = [
  32. 'title' => $field_storage->getLabel() . ' (' . $argument_type . ')',
  33. 'help' => $help_text,
  34. 'argument' => [
  35. 'field' => $field_storage->getName() . '_value',
  36. 'id' => 'datetime_' . $argument_type,
  37. 'entity_type' => $field_storage->getTargetEntityTypeId(),
  38. 'field_name' => $field_storage->getName(),
  39. ],
  40. 'group' => $group,
  41. ];
  42. }
  43. // Set the 'datetime' sort handler.
  44. $data[$table_name][$field_storage->getName() . '_value']['sort']['id'] = 'datetime';
  45. }
  46. return $data;
  47. }