In Drupal 7, you can use the hook_views_data hook to define custom tables and relationships for Views. When implementing relationships, you need to define the relationship key in your hook_views_data implementation. By implementing the relationship in hook_views_data, you allow Views to utilize this relationship when building queries. Once the relationship is defined, you can select and configure it when adding or editing a relationship in the Views UI.
Source code viewer
/**
* Implements hook_views_data().
*/
function HOOK_views_data() {
// Define your base table.
'group' => t('My Table'),
'field' => 'PRIMARY_KEY',
'title' => t('My Table'),
'help' => t('My table description.'),
),
);
// Define the relationship.
'title' => t('My Relationship'),
'help' => t('Help text for my relationship.'),
'base' => 'my_related_table',
'base field' => 'related_table_id',
'field' => 'my_table_id',
'handler' => 'views_handler_relationship',
'label' => t('My Relationship Label'),
),
);
// ...
return $data;
}
// Remember to clear the Drupal cache after implementing or modifying hook_views_data to ensure that the changes take effect.
Programming Language: PHP