datetime.views.inc 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Provides views data for the datetime module.
 */

use Drupal\field\FieldStorageConfigInterface;

/**
 * Implements hook_field_views_data().
 */
function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  return datetime_type_field_views_data_helper($field_storage, [], $field_storage->getMainPropertyName());
}

/**
 * Provides Views integration for any datetime-based fields.
 *
 * Overrides the default Views data for datetime-based fields, adding datetime
 * views plugins. Modules defining new datetime-based fields may use this
 * function to simplify Views integration.
 *
 * @param \Drupal\field\FieldStorageConfigInterface $field_storage
 *   The field storage config entity.
 * @param array $data
 *   Field view data or views_field_default_views_data($field_storage) if empty.
 * @param string $column_name
 *   The schema column name with the datetime value.
 *
 * @return array
 *   The array of field views data with the datetime plugin.
 *
 * @see datetime_field_views_data()
 * @see datetime_range_field_views_data()
 */
function datetime_type_field_views_data_helper(FieldStorageConfigInterface $field_storage, array $data, $column_name) {
38 39
  // @todo This code only covers configurable fields, handle base table fields
  //   in https://www.drupal.org/node/2489476.
40
  $data = empty($data) ? views_field_default_views_data($field_storage) : $data;
41 42
  foreach ($data as $table_name => $table_data) {
    // Set the 'datetime' filter type.
43
    $data[$table_name][$field_storage->getName() . '_' . $column_name]['filter']['id'] = 'datetime';
44 45

    // Set the 'datetime' argument type.
46
    $data[$table_name][$field_storage->getName() . '_' . $column_name]['argument']['id'] = 'datetime';
47 48

    // Create year, month, and day arguments.
49
    $group = $data[$table_name][$field_storage->getName() . '_' . $column_name]['group'];
50 51 52
    $arguments = [
      // Argument type => help text.
      'year' => t('Date in the form of YYYY.'),
53 54 55 56 57
      'month' => t('Date in the form of MM (01 - 12).'),
      'day' => t('Date in the form of DD (01 - 31).'),
      'week' => t('Date in the form of WW (01 - 53).'),
      'year_month' => t('Date in the form of YYYYMM.'),
      'full_date' => t('Date in the form of CCYYMMDD.'),
58 59
    ];
    foreach ($arguments as $argument_type => $help_text) {
60 61 62 63 64 65 66
      $column_name_text = $column_name === $field_storage->getMainPropertyName() ? '' : ':' . $column_name;
      $data[$table_name][$field_storage->getName() . '_' . $column_name . '_' . $argument_type] = [
        'title' => t('@label@column (@argument)', [
          '@label' => $field_storage->getLabel(),
          '@column' => $column_name_text,
          '@argument' => $argument_type,
        ]),
67 68
        'help' => $help_text,
        'argument' => [
69
          'field' => $field_storage->getName() . '_' . $column_name,
70
          'id' => 'datetime_' . $argument_type,
71 72
          'entity_type' => $field_storage->getTargetEntityTypeId(),
          'field_name' => $field_storage->getName(),
73 74 75 76 77 78
        ],
        'group' => $group,
      ];
    }

    // Set the 'datetime' sort handler.
79
    $data[$table_name][$field_storage->getName() . '_' . $column_name]['sort']['id'] = 'datetime';
80 81 82 83
  }

  return $data;
}