Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save PSF1/a8a03dce6456517cdd068790ae5dd602 to your computer and use it in GitHub Desktop.

Select an option

Save PSF1/a8a03dce6456517cdd068790ae5dd602 to your computer and use it in GitHub Desktop.
/**
* Alter the Views data for a single Field API field.
*
* Implements hook_field_views_data_alter().
*
* @see https://www.drupal.org/project/drupal/issues/2851325
*/
function turismo_customization_field_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
// $data = views_field_default_views_data($field_storage);
foreach ($data as $table_name => $table_data) {
// We like alter only daterange fields
if ($field_storage->getType() != 'daterange') {
continue;
}
// Set the 'datetime' filter type.
$data[$table_name][$field_storage->getName() . '_value']['filter']['id'] = 'datetime';
$data[$table_name][$field_storage->getName() . '_end_value']['filter']['id'] = 'datetime';
// Set the 'datetime' argument type.
$data[$table_name][$field_storage->getName() . '_value']['argument']['id'] = 'datetime';
$data[$table_name][$field_storage->getName() . '_end_value']['argument']['id'] = 'datetime';
// Create year, month, and day arguments.
$group = $data[$table_name][$field_storage->getName() . '_value']['group'];
$groupEnd = $data[$table_name][$field_storage->getName() . '_end_value']['group'];
$arguments = [
// Argument type => help text.
'year' => t('Date in the form of YYYY.'),
'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.'),
];
foreach ($arguments as $argument_type => $help_text) {
$data[$table_name][$field_storage->getName() . '_value_' . $argument_type] = [
'title' => $field_storage->getLabel() . ' (' . $argument_type . ')',
'help' => $help_text,
'argument' => [
'field' => $field_storage->getName() . '_value',
'id' => 'datetime_' . $argument_type,
],
'group' => $group,
];
$data[$table_name][$field_storage->getName() . '_end_value_' . $argument_type] = [
'title' => $field_storage->getLabel() . ' (' . $argument_type . ')',
'help' => $help_text,
'argument' => [
'field' => $field_storage->getName() . '_end_value',
'id' => 'datetime_' . $argument_type,
],
'group' => $groupEnd,
];
}
// Set the 'datetime' sort handler.
$data[$table_name][$field_storage->getName() . '_value']['sort']['id'] = 'datetime';
$data[$table_name][$field_storage->getName() . '_end_value']['sort']['id'] = 'datetime';
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment