annotate core/modules/datetime/datetime.views.inc @ 19:fa3358dc1485 tip

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