Views with custom Tables - hook_views_data | Drupal 8

Views with custom Tables - hook_views_data

Submitted by editor on Fri, 04/15/2016 - 13:48
Question

How to use Drupal Views with a custom table ?
Using hook_views_data() and hook_views_data_alter()

How tu use multiple database conncetion with views_data() ?

 

Officiel Documentation :

 https://api.drupal.org/api/drupal/core!modules!views!views.api.php/function/hook_views_data/8

 

Use multiple database conncetion with views_data() 

For that you must add 'database'

Example:

$data['mydatabasetable']['table']['base'] = array(
  ....
  'database' => 'mydatabase',
);

If you join databases:

Change 'name', 'base' and 'join' ()
$data['mydatabasetable']['table']['name'] = 'mydatabase' . '.fieldname';
$data['mydatabasetable']['table']['base']['database'] = 'mydatabase';
$data['mydatabasetable']['table']['join']['myseconddatatable']['table'] = 'mydatabase' . '.fieldname',

Like:

//Other Database name : mydatabase.
$db_name = 'mydatabase';

$data['fieldname'] = array(
'table' => array(
  'name' => $db_name . '.fieldname',
  'group' => t('The Group'),
  'base' => array(
    'field' => 'id',
    'title' => t('The field title'),
    'help' => t('Field description'),
    'database' => $db_name,
  ),
  'join' => array(
    'users' => array(
      'table' => $db_name . '.fieldname',
      'left_field' => 'id',
      'field' => 'id2',
    ),
  ),
),
);

Follow the tutorial Use multiple databases.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.