How to make our custom table available to the Views UI  in Drupal

How to make our custom table available to the Views UI  in Drupal

Here we are discussing how to make our custom table available to the Views UI so that we can create views displays such as pages , blocks and Rest exports. 

Assuming we have a custom table called customer_table in our Drupal database.

Create view data hook in your custom module .module file, here our custom module name is dn_viewtable, implement hook_view_data() as below in dn_viewtable.module file.

/**
* Implements hook_views_data().
*/
function dn_viewtable_views_data() {


}


Next we are going to implement this hook with our custom table name and column details.

All our custom table and column details will be populated in an array variable called  $data[] and will return this variable at the end of the hook.

First initialize the $data variable with your custom table name. We are saving custom table names in $table_name so that we can reuse the same in all places whenever it is required.

$table_name = 'customer_table';


   $data = [];
   $data[$table_name] = [];

Now we will populate $data[‘custom_table’] array with group and provider keys as below.

$data[$table_name]['table']['group'] = t('Customer Table');
   $data[$table_name]['table']['provider'] = 'dn_viewtable';

Here the group name is what we are seeing in view settings ->show in our view create page. So this table will be shown as  Customer Table in our view system. Also this name will be the prefix for field , filter and sort in view UI.

In provider we are giving our  custom module name from which module view data is added. Here our module name is  dn_viewtable.

Next add base array to the table, since we are using this as a base table and an independent view without any joins, we are making this as base with field id as base field for view UI.

$data[$table_name]['table']['base'] = [
       'field' => 'id',
       'title' => t('Customer Table'),
       'help' => t('All fields of ' . $table_name . '.'),
       'weight' => -10,
     ];

In Next step we are adding each field array to the table name value, see below we are creating array for id field and field, sort, filter and argument id values.

$data[$table_name]['id'] = [
       'title' => t(' ID field'),
       'help' => t(' ID field.'),
       'field' => [
         'id' => 'numeric',
       ],
       'sort' => [
         'id' => 'standard',
       ],
       'filter' => [
         'id' => 'numeric',
       ],
       'argument' => [
         'id' => 'numeric',
       ],
     ];


Same way add customer_name field as below. Please note the id value change in field, sort, filter and argument.

$data[$table_name]['customer_name'] = [
       'title' => t(' Customer Name field'),
       'help' => t(' Customer Name field.'),
       'field' => [
         'id' => 'standard',
       ],
       'sort' => [
         'id' => 'standard',
       ],
       'filter' => [
         'id' => 'string ',
       ],
       'argument' => [
         'id' => 'string',
       ],
     ];

Finally  return the $data variable

In above you can see field ,sort, filter and argument id values keep on changing based on type of the field.

For char and text type use below id’s

‘field’=>[‘id’=>’standard’]

‘sort’=>[‘id’=>’standard’]

‘filter’=>[‘id’=>’string’]

‘argument’=>[‘id’=>’string’]

For time and date type use below id’s.

‘field’=>[‘id’=>’date’]

‘sort’=>[‘id’=>’date’]

‘filter’=>[‘id’=>’date’]

‘argument’=>[‘id’=>’date’]

For int, float, double, numeric and decimal use below

‘field’=>[‘id’=>’numeric’]

‘sort’=>[‘id’=>’standard’]

‘filter’=>[‘id’=>’numeric’] ‘argument’=>[‘id’=>’numeric’]

So below provided complete  hook_views_data implementation in your custom module *.module file, dn_viewtable is the custom module name.

<?php
/**
* Implements hook_views_data().
*/
function dn_viewtable_views_data() {


   $table_name = 'customer_table';


   $data = [];
   $data[$table_name] = [];
   $data[$table_name]['table'] = [];
   $data[$table_name]['table']['group'] = t('Customer Table');
   $data[$table_name]['table']['provider'] = 'dn_viewtable';
   $data[$table_name]['table']['base'] = [
       'field' => 'id',
       'title' => t('Customer Table'),
       'help' => t('All fields of ' . $table_name . '.'),
       'weight' => -10,
     ];


   $data[$table_name]['id'] = [
       'title' => t(' ID field'),
       'help' => t(' ID field.'),
       'field' => [
         'id' => 'numeric',
       ],
       'sort' => [
         'id' => 'standard',
       ],
       'filter' => [
         'id' => 'numeric',
       ],
       'argument' => [
         'id' => 'numeric',
       ],
     ];


     $data[$table_name]['customer_name'] = [
       'title' => t(' Customer Name field'),
       'help' => t(' Customer Name field.'),
       'field' => [
         'id' => 'standard',
       ],
       'sort' => [
         'id' => 'standard',
       ],
       'filter' => [
         'id' => 'string ',
       ],
       'argument' => [
         'id' => 'string',
       ],
     ];






   return $data;
}

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...