--- /dev/null
+/**
+ * Implements hook_views_data_alter().
+ */
+function {{ machine_name }}_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.
+}