03bc80bcdfe92b306bc7e4025d611ed7ed982c21
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / views_data_alter.twig
1 /**
2  * Implements hook_views_data_alter().
3  */
4 function {{ machine_name }}_views_data_alter(array &$data) {
5   // Alter the title of the node_field_data:nid field in the Views UI.
6   $data['node_field_data']['nid']['title'] = t('Node-Nid');
7
8   // Add an additional field to the users_field_data table.
9   $data['users_field_data']['example_field'] = [
10     'title' => t('Example field'),
11     'help' => t('Some example content that references a user'),
12
13     'field' => [
14       // ID of the field handler to use.
15       'id' => 'example_field',
16     ],
17   ];
18
19   // Change the handler of the node title field, presumably to a handler plugin
20   // you define in your module. Give the ID of this plugin.
21   $data['node_field_data']['title']['field']['id'] = 'node_title';
22
23   // Add a relationship that will allow a view whose base table is 'foo' (from
24   // another module) to have a relationship to 'example_table' (from my module),
25   // via joining foo.fid to example_table.eid.
26   //
27   // This relationship has to be added to the 'foo' Views data, which my module
28   // does not control, so it must be done in hook_views_data_alter(), not
29   // hook_views_data().
30   //
31   // In Views data definitions, each field can have only one relationship. So
32   // rather than adding this relationship directly to the $data['foo']['fid']
33   // field entry, which could overwrite an existing relationship, we define
34   // a dummy field key to handle the relationship.
35   $data['foo']['unique_dummy_name'] = [
36     'title' => t('Title seen while adding relationship'),
37     'help' => t('More information about the relationship'),
38
39     'relationship' => [
40       // Views name of the table being joined to from foo.
41       'base' => 'example_table',
42       // Database field name in example_table for the join.
43       'base field' => 'eid',
44       // Real database field name in foo for the join, to override
45       // 'unique_dummy_name'.
46       'field' => 'fid',
47       // ID of relationship handler plugin to use.
48       'id' => 'standard',
49       'label' => t('Default label for relationship'),
50     ],
51   ];
52
53   // Note that the $data array is not returned – it is modified by reference.
54 }