datetime.views.inc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. return datetime_type_field_views_data_helper($field_storage, [], $field_storage->getMainPropertyName());
  12. }
  13. /**
  14. * Provides Views integration for any datetime-based fields.
  15. *
  16. * Overrides the default Views data for datetime-based fields, adding datetime
  17. * views plugins. Modules defining new datetime-based fields may use this
  18. * function to simplify Views integration.
  19. *
  20. * @param \Drupal\field\FieldStorageConfigInterface $field_storage
  21. * The field storage config entity.
  22. * @param array $data
  23. * Field view data or views_field_default_views_data($field_storage) if empty.
  24. * @param string $column_name
  25. * The schema column name with the datetime value.
  26. *
  27. * @return array
  28. * The array of field views data with the datetime plugin.
  29. *
  30. * @see datetime_field_views_data()
  31. * @see datetime_range_field_views_data()
  32. */
  33. function datetime_type_field_views_data_helper(FieldStorageConfigInterface $field_storage, array $data, $column_name) {
  34. // @todo This code only covers configurable fields, handle base table fields
  35. // in https://www.drupal.org/node/2489476.
  36. $data = empty($data) ? views_field_default_views_data($field_storage) : $data;
  37. foreach ($data as $table_name => $table_data) {
  38. // Set the 'datetime' filter type.
  39. $data[$table_name][$field_storage->getName() . '_' . $column_name]['filter']['id'] = 'datetime';
  40. // Set the 'datetime' argument type.
  41. $data[$table_name][$field_storage->getName() . '_' . $column_name]['argument']['id'] = 'datetime';
  42. // Create year, month, and day arguments.
  43. $group = $data[$table_name][$field_storage->getName() . '_' . $column_name]['group'];
  44. $arguments = [
  45. // Argument type => help text.
  46. 'year' => t('Date in the form of YYYY.'),
  47. 'month' => t('Date in the form of MM (01 - 12).'),
  48. 'day' => t('Date in the form of DD (01 - 31).'),
  49. 'week' => t('Date in the form of WW (01 - 53).'),
  50. 'year_month' => t('Date in the form of YYYYMM.'),
  51. 'full_date' => t('Date in the form of CCYYMMDD.'),
  52. ];
  53. foreach ($arguments as $argument_type => $help_text) {
  54. $column_name_text = $column_name === $field_storage->getMainPropertyName() ? '' : ':' . $column_name;
  55. $data[$table_name][$field_storage->getName() . '_' . $column_name . '_' . $argument_type] = [
  56. 'title' => t('@label@column (@argument)', [
  57. '@label' => $field_storage->getLabel(),
  58. '@column' => $column_name_text,
  59. '@argument' => $argument_type,
  60. ]),
  61. 'help' => $help_text,
  62. 'argument' => [
  63. 'field' => $field_storage->getName() . '_' . $column_name,
  64. 'id' => 'datetime_' . $argument_type,
  65. 'entity_type' => $field_storage->getTargetEntityTypeId(),
  66. 'field_name' => $field_storage->getName(),
  67. ],
  68. 'group' => $group,
  69. ];
  70. }
  71. // Set the 'datetime' sort handler.
  72. $data[$table_name][$field_storage->getName() . '_' . $column_name]['sort']['id'] = 'datetime';
  73. }
  74. return $data;
  75. }