Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Views with custom Tables - hook_views_data

By editor , 15 April, 2016
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.

Tags

  • Drupal 8
  • Code
  • Views
  • Hook
  • Add new comment
Use multiple / external databases in Drupal 8
Create a simple module to use Drupal 8 Hook System
Create a View Plugin Without using Database field
Custom Views Data handler for a custom Entity on drupal 8

Comments1

Plain text

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

Sebastian (not verified)

8 years 4 months ago

not working

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',
    ),

 

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer