W3cubDocs

/Drupal 8

function hook_views_data_alter

hook_views_data_alter(array &$data)

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

Hooks
Define functions that alter the behavior of Drupal core.

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'] = array(
    'title' => t('Example field'),
    'help' => t('Some example content that references a user'),

    'field' => array(
      // 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'] = array(
    'title' => t('Title seen while adding relationship'),
    'help' => t('More information about the relationship'),

    'relationship' => array(
      // 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.
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!views!views.api.php/function/hook_views_data_alter/8.1.x