You are here

public function DateRecurViewsHooks::viewsData in Recurring Dates Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/DateRecurViewsHooks.php \Drupal\date_recur\DateRecurViewsHooks::viewsData()
  2. 3.x src/DateRecurViewsHooks.php \Drupal\date_recur\DateRecurViewsHooks::viewsData()
  3. 3.1.x src/DateRecurViewsHooks.php \Drupal\date_recur\DateRecurViewsHooks::viewsData()

Implements hook_views_data().

See also

\hook_views_data()

\date_recur_views_data()

File

src/DateRecurViewsHooks.php, line 108

Class

DateRecurViewsHooks
Defines Views hooks.

Namespace

Drupal\date_recur

Code

public function viewsData() : array {
  $allFields = $this
    ->getDateRecurFields();
  $data = [];
  foreach ($allFields as $entityTypeId => $fields) {
    $entityType = $this->entityTypeManager
      ->getDefinition($entityTypeId);

    /** @var \Drupal\views\EntityViewsDataInterface $viewsData */
    $viewsData = $this->entityTypeManager
      ->getHandler($entityType
      ->id(), 'views_data');
    $entityViewsTable = $viewsData
      ->getViewsTableForEntityType($entityType);

    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fields */
    foreach ($fields as $fieldId => $field) {
      $fieldLabel = $this
        ->getFieldLabel($field
        ->getTargetEntityTypeId(), $fieldId);
      $fieldName = $field
        ->getName();
      $entityIdField = $entityType
        ->getKey('id');
      $entityRevisionField = $entityType
        ->isRevisionable() ? $entityType
        ->getKey('revision') : NULL;
      $tArgs = [
        '@field_name' => $fieldLabel,
        '@entity_type' => $entityType
          ->getLabel(),
      ];

      // Occurrence filter.
      $data[$entityViewsTable][$fieldName . '_occurrences']['filter'] = [
        'id' => 'date_recur_occurrences_filter',
        'title' => $this
          ->t('Occurrences filter for @field_name', $tArgs),
        // Instruct the filter to join the occurrence.entity_id field on
        // base.entityId:
        'field base entity_id' => $entityIdField,
        'date recur field name' => $fieldName,
        'entity_type' => $entityType
          ->id(),
      ];

      // Relationship from entity table to occurrence table.
      $occurrenceTableName = DateRecurOccurrences::getOccurrenceCacheStorageTableName($field);
      $data[$entityViewsTable][$fieldName . '_occurrences']['relationship'] = [
        'id' => 'standard',
        'base' => $occurrenceTableName,
        'base field' => isset($entityRevisionField) ? 'revision_id' : 'entity_id',
        'help' => $this
          ->t('Get all occurrences for recurring date field @field_name', $tArgs),
        'field' => $entityRevisionField ?? $entityIdField,
        // Add new 'title'.
        'title' => $this
          ->t('Occurrences of @field_name', $tArgs),
        // Default label for relationship in the UI.
        'label' => $this
          ->t('Occurrences of @field_name', $tArgs),
      ];
      $dateField = [
        'id' => 'date_recur_date',
        'source date format' => $this
          ->getFieldDateFormat($field),
        'source time zone' => DateTimeItemInterface::STORAGE_TIMEZONE,
      ];
      $data[$occurrenceTableName][$fieldName . '_value']['field'] = $dateField;
      $data[$occurrenceTableName][$fieldName . '_end_value']['field'] = $dateField;

      // Attached fields get automatic functionality provided by
      // hook_field_views_data(). Add features here for base fields.
      if ($field instanceof BaseFieldDefinition) {
        $data[$occurrenceTableName]['table']['group'] = $this
          ->t('Occurrences for @entity_type @field_name', [
          '@entity_type' => $entityType
            ->getLabel(),
          '@field_name' => $fieldLabel,
        ]);
        $startField = $fieldName . '_value';
        $endField = $fieldName . '_end_value';
        $data[$occurrenceTableName][$startField]['title'] = $this
          ->t('Occurrence start date');
        $data[$occurrenceTableName][$endField]['title'] = $this
          ->t('Occurrence end date');

        // Sort.
        // datetime_range_field_views_data() uses 'datetime', which relies
        // on entity things.
        $data[$occurrenceTableName][$startField]['sort']['id'] = 'date';
        $data[$occurrenceTableName][$endField]['sort']['id'] = 'date';
      }
    }
  }
  return $data;
}