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.

Comments

Sebastian (not verified)

Mon, 07/16/2018 - 15:20

doesn't work for me when follwing this approach and including a relationthip in one field
'relationship' => array(
     ...
      'base'               => 'mydefaultdb' . '.users_field_data',
      'base field'         => 'uid',
      'field'              => 'uid',
    ),

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.