You are here

function relation_views_data_alter in Relation 8

Same name and namespace in other branches
  1. 8.2 relation.views.inc \relation_views_data_alter()
  2. 7 views/relation.views.inc \relation_views_data_alter()

Implements hook_views_data_alter().

File

./relation.views.inc, line 218

Code

function relation_views_data_alter(&$data) {
  $entity_info = Drupal::entityTypeManager()
    ->getDefinitions();

  // Build the relations between the different tables.
  foreach (RelationType::loadMultiple() as $relation_type) {
    $type = $relation_type
      ->id();
    $target_index = $relation_type->directional ? 'target_bundles' : 'source_bundles';
    foreach ($relation_type->source_bundles as $source_bundle) {
      list($source_entity_type) = explode(':', $source_bundle, 2);
      $source_table = $entity_info[$source_entity_type]
        ->getDataTable();

      // Translatable.
      $placeholders = array(
        '@left' => $source_entity_type,
        '@relation_type_label' => $relation_type->label,
        '@relation_type_reverse_label' => $relation_type->reverse_label,
        '@arrow' => $relation_type->directional ? '→' : '↔',
      );
      if ($source_table) {
        $data[$source_table]['relation_base_left_' . $type] = array(
          'title' => t('Relation: @relation_type_label (@left → relation)', $placeholders),
          'help' => t('Provides a relationship from @left to the relation table via the relation @relation_type_label', $placeholders),
          'relationship' => array(
            'label' => $relation_type->label,
            'base' => 'relation',
            'base field' => 'relation_id',
            'relationship field' => $entity_info[$source_entity_type]
              ->getKey('id'),
            'id' => 'relation_relationship',
            'relation_type' => $type,
            'entity_type_left' => $source_entity_type,
            'directional' => $relation_type->directional,
          ),
        );
      }
      foreach ($relation_type->{$target_index} as $target_bundle) {
        list($target_entity_type) = explode(':', $target_bundle, 2);
        $target_table = $entity_info[$target_entity_type]
          ->getBaseTable();
        $placeholders['@right'] = $target_entity_type;

        // Provide forward relationships.
        if ($source_table && $target_table) {
          $data[$source_table]['relation_' . $type . '_' . $target_entity_type] = array(
            'title' => t('Relation: @relation_type_label (@left @arrow @right)', $placeholders),
            'help' => t('Provides a relationship from @left to @right via the relation @relation_type_label', $placeholders),
            'relationship' => array(
              'label' => $relation_type->label,
              'base' => $target_table,
              'base field' => $entity_info[$target_entity_type]
                ->getKey('id'),
              'relationship field' => $entity_info[$source_entity_type]
                ->getKey('id'),
              'id' => 'relation_relationship',
              'relation_type' => $type,
              'entity_type_left' => $source_entity_type,
              'entity_type_right' => $target_entity_type,
              'directional' => $relation_type->directional,
            ),
          );
        }
        if ($target_table) {
          $data['relation']['relation_base_' . $type . '_' . $target_entity_type] = array(
            'title' => t('Relation: @relation_type_label (relation → @right)', $placeholders),
            'help' => t('Provides a relationship from the relation table to @right via the relation @relation_type_label', $placeholders),
            'relationship' => array(
              'label' => $relation_type->label,
              'base' => $target_table,
              'base field' => $entity_info[$target_entity_type]
                ->getKey('id'),
              'relationship field' => 'relation_id',
              'id' => 'relation_relationship',
              'relation_type' => $type,
              'entity_type_right' => $target_entity_type,
              'directional' => $relation_type->directional,
            ),
          );
        }

        // Provide reverse relationships.
        if ($target_entity_type != $source_entity_type) {
          if ($source_table && $target_table) {
            $data[$target_table]['relation_' . $type . '_' . $source_entity_type] = array(
              'title' => t('Relation: @relation_type_reverse_label (@right @arrow @left)', $placeholders),
              'help' => t('Provides a relationship from @right to @left via the relation @relation_type_reverse_label', $placeholders),
              'relationship' => array(
                'label' => $relation_type->reverse_label,
                'base' => $source_table,
                'base field' => $entity_info[$source_entity_type]
                  ->getKey('id'),
                'relationship field' => $entity_info[$target_entity_type]
                  ->getKey('id'),
                'id' => 'relation_relationship',
                'relation_type' => $type,
                'entity_type_left' => $target_entity_type,
                'entity_type_right' => $source_entity_type,
                'directional' => $relation_type->directional,
              ),
            );
          }
          if ($target_table) {
            $data[$target_table]['relation_base_right_' . $type] = array(
              'title' => t('Relation: @relation_type_reverse_label (@right → relation)', $placeholders),
              'help' => t('Provides a relationship from @right to the relation table via the relation @relation_type_reverse_label. Usually only needed to access the fields of the relation itself.', $placeholders),
              'relationship' => array(
                'label' => $relation_type->reverse_label,
                'base' => 'relation',
                'base field' => 'relation_id',
                'relationship field' => $entity_info[$target_entity_type]
                  ->getKey('id'),
                'id' => 'relation_relationship',
                'relation_type' => $type,
                'entity_type_right' => $target_entity_type,
                'directional' => $relation_type->directional,
              ),
            );
          }
          if ($source_table) {
            $data['relation']['relation_base_' . $type . '_' . $source_entity_type] = array(
              'title' => t('Relation: @relation_type_reverse_label (relation → @left)', $placeholders),
              'help' => t('Provides a relationship from the relation table to @left via the relation @relation_type_reverse_label', $placeholders),
              'relationship' => array(
                'label' => $relation_type->reverse_label,
                'base' => $source_table,
                'base field' => $entity_info[$source_entity_type]
                  ->getKey('id'),
                'relationship field' => 'relation_id',
                'id' => 'relation_relationship',
                'relation_type' => $type,
                'entity_type_left' => $source_entity_type,
                'directional' => $relation_type->directional,
              ),
            );
          }
        }
      }
    }
  }
}