diff --git a/smart_date.views.inc b/smart_date.views.inc index 7819c1d..e76a4c9 100644 --- a/smart_date.views.inc +++ b/smart_date.views.inc @@ -69,7 +69,12 @@ function smart_date_field_views_data(FieldStorageConfigInterface $field) { $data[$table_name][$field_name . '_' . $column][$type]['property'] = $column; } } + + if ($column == "value") { + _smart_date_add_views_granularity_arguments($data, $field, $table_name, $column); + } } + // Provide a relationship for the entity type with the entity reference // revisions field. $args = [ @@ -92,3 +97,42 @@ function smart_date_field_views_data(FieldStorageConfigInterface $field) { return $data; } + +/** + * Helper for hook_views_data to add various argument types. + */ +function _smart_date_add_views_granularity_arguments(&$data, $field, $table_name, $column) { + $entity_type_id = $field->getTargetEntityTypeId(); + $entity_manager = \Drupal::entityTypeManager(); + $entity_type = $entity_manager->getDefinition($entity_type_id); + + // Granularity argument handlers. + $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_week' => t('Date in the form of YYYYW'), // Already added by calendar module(!) + '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) { + $column_name_text = $column === $field->getMainPropertyName() ? '' : ':' . $column; + $data[$table_name][$field->getName() . '_' . $column . '_' . $argument_type] = [ + 'title' => t('@label@column (@argument)', [ + '@label' => $field->getLabel(), + '@column' => $column_name_text, + '@argument' => $argument_type, + ]), + 'help' => $help_text, + 'argument' => [ + 'field' => $field->getName() . '_' . $column, + 'id' => 'date_' . $argument_type, + 'entity_type' => $field->getTargetEntityTypeId(), + 'field_name' => $field->getName(), + ], + 'group' => $entity_type->getLabel(), + ]; + } +}