You are here

function relation_views_data_alter in Relation 7

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

Implements hook_views_data_alter().

File

views/relation.views.inc, line 211
Views support.

Code

function relation_views_data_alter(&$data) {

  // Find out which entity type has which base table.
  $entity_infos = entity_get_info();
  $entity_tables = array();
  foreach ($entity_infos as $entity_type => $entity_info) {
    if (isset($entity_info['base table'])) {
      $entity_tables[$entity_type] = $entity_info['base table'];
    }
  }
  $field = field_info_field('endpoints');
  $relation_data_table_name = _field_sql_storage_tablename($field);
  $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
  $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');

  // Build the relations between the different tables.
  $types = relation_get_types();
  foreach ($types as $type => $relation_type) {
    $target_index = $relation_type->directional ? 'target_bundles' : 'source_bundles';
    foreach ($relation_type->source_bundles as $source_bundle) {
      $source_bundle = explode(':', $source_bundle, 2);
      $entity_type_left = $source_bundle[0];
      $base_table_left = $entity_tables[$entity_type_left];
      $t_arguments = array(
        '@left' => $entity_type_left,
        '@relation_type_label' => $relation_type->label,
        '@relation_type_reverse_label' => $relation_type->reverse_label,
        '@arrow' => $relation_type->directional ? '→' : '↔',
      );
      $data[$base_table_left]['relation_base_left_' . $type] = array(
        'title' => t('Relation: @relation_type_label (@left → relation)', $t_arguments),
        'help' => t('Provides a relationship from @left to the relation table via the relation @relation_type_label', $t_arguments),
        'relationship' => array(
          // relation_handler_relationship::options_form() relies on this check_plain().
          'label' => check_plain($relation_type->label),
          'base' => 'relation',
          'base field' => 'rid',
          'relationship field' => $entity_infos[$entity_type_left]['entity keys']['id'],
          'handler' => 'relation_handler_relationship',
          'relation_type' => $type,
          'entity_type_left' => $entity_type_left,
          'directional' => $relation_type->directional,
        ),
      );
      foreach ($relation_type->{$target_index} as $target_bundle) {
        $target_bundle = explode(':', $target_bundle, 2);
        $entity_type_right = $target_bundle[0];
        $base_table_right = $entity_tables[$entity_type_right];
        $t_arguments['@right'] = $entity_type_right;

        // Provide forward relationships
        $data[$base_table_left]['relation_' . $type . '_' . $entity_type_right] = array(
          'title' => t('Relation: @relation_type_label (@left @arrow @right)', $t_arguments),
          'help' => t('Provides a relationship from @left to @right via the relation @relation_type_label', $t_arguments),
          'relationship' => array(
            // relation_handler_relationship::options_form() relies on this check_plain().
            'label' => check_plain($relation_type->label),
            'base' => $base_table_right,
            'base field' => $entity_infos[$entity_type_right]['entity keys']['id'],
            'relationship field' => $entity_infos[$entity_type_left]['entity keys']['id'],
            'handler' => 'relation_handler_relationship',
            'relation_type' => $type,
            'entity_type_left' => $entity_type_left,
            'entity_type_right' => $entity_type_right,
            'directional' => $relation_type->directional,
          ),
        );
        $data['relation']['relation_base_' . $type . '_' . $entity_type_right] = array(
          'title' => t('Relation: @relation_type_label (relation → @right)', $t_arguments),
          'help' => t('Provides a relationship from the relation table to @right via the relation @relation_type_label', $t_arguments),
          'relationship' => array(
            // relation_handler_relationship::options_form() relies on this check_plain().
            'label' => check_plain($relation_type->label),
            'base' => $base_table_right,
            'base field' => $entity_infos[$entity_type_right]['entity keys']['id'],
            'relationship field' => 'rid',
            'handler' => 'relation_handler_relationship',
            'relation_type' => $type,
            'entity_type_right' => $entity_type_right,
            'directional' => $relation_type->directional,
          ),
        );

        // Provide reverse relationships
        if ($entity_type_right != $entity_type_left) {
          $data[$base_table_right]['relation_' . $type . '_' . $entity_type_left] = array(
            'title' => t('Relation: @relation_type_reverse_label (@right @arrow @left)', $t_arguments),
            'help' => t('Provides a relationship from @right to @left via the relation @relation_type_reverse_label', $t_arguments),
            'relationship' => array(
              // relation_handler_relationship::options_form() relies on this check_plain().
              'label' => check_plain($relation_type->reverse_label),
              'base' => $base_table_left,
              'base field' => $entity_infos[$entity_type_left]['entity keys']['id'],
              'relationship field' => $entity_infos[$entity_type_right]['entity keys']['id'],
              'handler' => 'relation_handler_relationship',
              'relation_type' => $type,
              'entity_type_left' => $entity_type_right,
              'entity_type_right' => $entity_type_left,
              'directional' => $relation_type->directional,
            ),
          );
          $data[$base_table_right]['relation_base_right_' . $type] = array(
            'title' => t('Relation: @relation_type_reverse_label (@right → relation)', $t_arguments),
            '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.', $t_arguments),
            'relationship' => array(
              // relation_handler_relationship::options_form() relies on this check_plain().
              'label' => check_plain($relation_type->reverse_label),
              'base' => 'relation',
              'base field' => 'rid',
              'relationship field' => $entity_infos[$entity_type_right]['entity keys']['id'],
              'handler' => 'relation_handler_relationship',
              'relation_type' => $type,
              'entity_type_left' => $entity_type_right,
              'directional' => $relation_type->directional,
            ),
          );
          $data['relation']['relation_base_' . $type . '_' . $entity_type_left] = array(
            'title' => t('Relation: @relation_type_reverse_label (relation → @left)', $t_arguments),
            'help' => t('Provides a relationship from the relation table to @left via the relation @relation_type_reverse_label', $t_arguments),
            'relationship' => array(
              // relation_handler_relationship::options_form() relies on this check_plain().
              'label' => check_plain($relation_type->reverse_label),
              'base' => $base_table_left,
              'base field' => $entity_infos[$entity_type_left]['entity keys']['id'],
              'relationship field' => 'rid',
              'handler' => 'relation_handler_relationship',
              'relation_type' => $type,
              'entity_type_right' => $entity_type_left,
              'directional' => $relation_type->directional,
            ),
          );
        }
      }
    }
  }
}