hook_field_views_data_views_data_alter

hook_field_views_data_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field)

Alter the Views data on a per field basis.

The field module's implementation of hook_views_data_alter() invokes this for each field storage, in the module that defines the field type. It is not invoked in other modules.

Unlike hook_field_views_data_alter(), this operates on the whole of the views data. This allows a field type to add data that concerns its fields in other tables, which would not yet be defined at the point when hook_field_views_data() and hook_field_views_data_alter() are invoked. For example, entityreference adds reverse relationships on the tables for the entities which are referenced by entityreference fields.

(Note: this is weirdly named so as not to conflict with hook_field_views_data_alter().)

Parameters

array $data: The views data.

\Drupal\field\FieldStorageConfigInterface $field: The field storage config entity.

See also

hook_field_views_data()

hook_field_views_data_alter()

views_views_data_alter()

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/views/views.api.php, line 613
Describes hooks and plugins provided by the Views module.

Code

function hook_field_views_data_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field) {
  $field_name = $field->getName();
  $data_key = 'field_data_' . $field_name;
  $entity_type_id = $field->entity_type;
  $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
  $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
  list($label) = views_entity_field_label($entity_type_id, $field_name);
  $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping();

  // Views data for this field is in $data[$data_key].
  $data[$data_key][$pseudo_field_name]['relationship'] = array(
    'title' => t('@entity using @field', array('@entity' => $entity_type->getLabel(), '@field' => $label)),
    'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity_type->getLabel(), '@field' => $label)),
    'id' => 'entity_reverse',
    'field_name' => $field_name,
    'entity_type' => $entity_type_id,
    'field table' => $table_mapping->getDedicatedDataTableName($field),
    'field field' => $field_name . '_target_id',
    'base' => $entity_type->getBaseTable(),
    'base field' => $entity_type->getKey('id'),
    'label' => $field_name,
    'join_extra' => array(
      0 => array(
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ),
    ),
  );
}
doc_Drupal
2016-10-29 09:18:11
Comments
Leave a Comment

Please login to continue.