You are here

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

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

Implements hook_field_views_data().

See also

\hook_field_views_data()

\date_recur_field_views_data()

File

src/DateRecurViewsHooks.php, line 239

Class

DateRecurViewsHooks
Defines Views hooks.

Namespace

Drupal\date_recur

Code

public function fieldViewsData(FieldStorageConfigInterface $fieldDefinition) : array {
  $data = [];
  $entityTypeId = $fieldDefinition
    ->getTargetEntityTypeId();
  $entityType = $this->entityTypeManager
    ->getDefinition($entityTypeId);

  /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entityStorage */
  $entityStorage = $this->entityTypeManager
    ->getStorage($entityTypeId);

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $tableMapping */
  $tableMapping = $entityStorage
    ->getTableMapping();
  $fieldName = $fieldDefinition
    ->getName();

  // The field label, see also usage in views.views.inc.
  $fieldLabel = $this
    ->getFieldLabel($fieldDefinition
    ->getTargetEntityTypeId(), $fieldDefinition
    ->getName());
  $fieldTableName = $tableMapping
    ->getDedicatedDataTableName($fieldDefinition);
  $parentData = $this
    ->getParentFieldViewsData($fieldDefinition);
  if (empty($parentData)) {
    return $data;
  }
  $originalTable = $parentData[$fieldTableName];
  $occurrenceTableName = DateRecurOccurrences::getOccurrenceCacheStorageTableName($fieldDefinition);
  if ($this->database
    ->schema()
    ->tableExists($occurrenceTableName)) {
    $occurrenceTable = $originalTable;

    // Remove the automatic join, requires site builders to use relationship
    // plugin.
    unset($occurrenceTable['table']['join']);

    // Unset some irrelevant fields.
    foreach (array_keys($occurrenceTable) as $fieldId) {
      $fieldId = (string) $fieldId;
      if ($fieldId === 'table' || strpos($fieldId, $fieldName . '_value', 0) !== FALSE || strpos($fieldId, $fieldName . '_end_value', 0) !== FALSE) {
        continue;
      }
      unset($occurrenceTable[$fieldId]);
    }

    // Update table name references.
    $handlerTypes = $this
      ->getViewsPluginTypes();
    $recurTableGroup = $this
      ->t('Occurrences for @entity_type @field_name', [
      '@entity_type' => $entityType
        ->getLabel(),
      '@field_name' => $fieldLabel,
    ]);
    foreach ($occurrenceTable as $fieldId => &$field) {
      $field['group'] = $recurTableGroup;
      foreach ($handlerTypes as $handlerType) {
        if (!empty($field[$handlerType]['table'])) {
          $field[$handlerType]['table'] = $occurrenceTableName;
          $field[$handlerType]['additional fields'] = [
            $fieldName . '_value',
            $fieldName . '_end_value',
            'delta',
            'field_delta',
          ];
        }
      }
    }
    $data[$occurrenceTableName] = $occurrenceTable;
  }
  $fieldTable = $originalTable;

  // Change the title for all plugins provided by
  // \datetime_range_field_views_data().
  foreach ($fieldTable as $key => &$definitions) {

    /** @var \Drupal\Core\StringTranslation\TranslatableMarkup|string|null $originalTitle */
    $originalTitle = $definitions['title'] ?? '';
    $tArgs = $originalTitle instanceof TranslatableMarkup ? $originalTitle
      ->getArguments() : [];
    $tArgs['@field_label'] = $fieldLabel;
    if ($fieldName === $key) {
      $definitions['title'] = isset($tArgs['@argument']) ? $this
        ->t('@field_label (@argument)', $tArgs) : $this
        ->t('@field_label', $tArgs);
    }
    elseif (strpos($key, $fieldName . '_value', 0) !== FALSE) {
      $definitions['title'] = isset($tArgs['@argument']) ? $this
        ->t('@field_label: first occurrence start date (@argument)', $tArgs) : $this
        ->t('@field_label: first occurrence start date', $tArgs);
    }
    elseif (strpos($key, $fieldName . '_end_value', 0) !== FALSE) {
      $definitions['title'] = isset($tArgs['@argument']) ? $this
        ->t('@field_label: first occurrence end date (@argument)', $tArgs) : $this
        ->t('@field_label: first occurrence end date', $tArgs);
    }
    elseif (strpos($key, $fieldName . '_rrule', 0) !== FALSE) {
      $definitions['title'] = $this
        ->t('@field_label: recurring rule', $tArgs);
    }
    elseif (strpos($key, $fieldName . '_timezone', 0) !== FALSE) {
      $definitions['title'] = $this
        ->t('@field_label: time zone', $tArgs);
    }
    elseif (strpos($key, $fieldName . '_infinite', 0) !== FALSE) {
      $definitions['title'] = $this
        ->t('@field_label: is infinite', $tArgs);
    }
    elseif ('delta' === $key) {
      $definitions['title'] = $this
        ->t('@field_label: field delta', $tArgs);
    }
  }
  $data[$fieldTableName] = $fieldTable;
  return $data;
}