You are here

function hook_views_data_alter in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/views.api.php \hook_views_data_alter()

Alter the table and field information from hook_views_data().

Parameters

array $data: An array of all information about Views tables and fields, collected from hook_views_data(), passed by reference.

See also

hook_views_data()

Related topics

14 functions implement hook_views_data_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_views_data_alter in core/modules/comment/comment.views.inc
Implements hook_views_data_alter().
contact_views_data_alter in core/modules/contact/contact.views.inc
Implements hook_views_data_alter().
content_translation_views_data_alter in core/modules/content_translation/content_translation.module
Implements hook_views_data_alter().
contextual_views_data_alter in core/modules/contextual/contextual.views.inc
Implements hook_views_data_alter().
entity_test_views_data_alter in core/modules/system/tests/modules/entity_test/entity_test.views.inc
Implements hook_views_data_alter().

... See full list

1 invocation of hook_views_data_alter()
ViewsData::getData in core/modules/views/src/ViewsData.php
Gets all data invoked by hook_views_data().

File

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

Code

function hook_views_data_alter(array &$data) {

  // Alter the title of the node_field_data:nid field in the Views UI.
  $data['node_field_data']['nid']['title'] = t('Node-Nid');

  // Add an additional field to the users_field_data table.
  $data['users_field_data']['example_field'] = [
    'title' => t('Example field'),
    'help' => t('Some example content that references a user'),
    'field' => [
      // ID of the field handler to use.
      'id' => 'example_field',
    ],
  ];

  // Change the handler of the node title field, presumably to a handler plugin
  // you define in your module. Give the ID of this plugin.
  $data['node_field_data']['title']['field']['id'] = 'node_title';

  // Add a relationship that will allow a view whose base table is 'foo' (from
  // another module) to have a relationship to 'example_table' (from my module),
  // via joining foo.fid to example_table.eid.
  //
  // This relationship has to be added to the 'foo' Views data, which my module
  // does not control, so it must be done in hook_views_data_alter(), not
  // hook_views_data().
  //
  // In Views data definitions, each field can have only one relationship. So
  // rather than adding this relationship directly to the $data['foo']['fid']
  // field entry, which could overwrite an existing relationship, we define
  // a dummy field key to handle the relationship.
  $data['foo']['unique_dummy_name'] = [
    'title' => t('Title seen while adding relationship'),
    'help' => t('More information about the relationship'),
    'relationship' => [
      // Views name of the table being joined to from foo.
      'base' => 'example_table',
      // Database field name in example_table for the join.
      'base field' => 'eid',
      // Real database field name in foo for the join, to override
      // 'unique_dummy_name'.
      'field' => 'fid',
      // ID of relationship handler plugin to use.
      'id' => 'standard',
      'label' => t('Default label for relationship'),
    ],
  ];

  // Note that the $data array is not returned – it is modified by reference.
}