Breaking

Followers

Wednesday, 8 February 2017

How to integrate custom table with views in drupal 7


Drupal views help in querying and displaying information fast and it becomes so hard when it comes to custom table fields to be accessed in the view arguments. To help Drupal views understand the exposure of modules and custom table data, we use hook_views_data().
In implementing hook_views_data, it calls a file expose.views.inc in the folder(/includes/views/) and the array structure of the
data fields and are returned by hook_schema.

STEP 1: 
I begin with my join_event.info, Apart from the regular .info directives like module name,desc,version etc, we need to specify the dependencies with views module”

name = Join Event
description = join_event module For join event views coding 
core=7.x version=VERSION 
dependencies[] = views 

STEP 2: 
“The below code shows .module file of our custom module. Here i just Specified the Views API version and path of the views.inc file.”

/**
 * Implements hook_views_api()
 */
  function join_event_views_api(){
   return array(
        'api' => 3,
        'path' => drupal_get_path('module', 'join_event') . '/includes/views',
    );
    }

STEP 3: 
Below is the code for our /includes/views/expose.views.inc.views.inc file.

 /**
  * Implement hook_views_data
  *
  */

function match_making_views_data() {

  $data = array();
    $data['join_events']['table']['group'] = t('Invited users for Events By Host/Co-host');

    $data['join_events']['table']['base'] = array(

    'field' => 'sr_id',
    'title' => t('Join Events'), 
    'help' => t('This table contains node content and can be related to nodes.'), 
    'weight' => 10,
  );

   $data['join_events']['table']['join'] = array(

    'node' => array(
      'left_field' => 'nid', 
      'field' => 'nid',
    ),
  );

  $data['join_events']['nid'] = array(

    'title' => t('event id'), 
    'help' => t('Event content that references a node.'),
    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid', // The name of the field on the joined table.
      // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
      'handler' => 'views_handler_relationship', 
      'label' => t('Event node'),
     ),

   'field' => array(

      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    )
  );

$data['join_events']['host_user'] = array(

    'title' => t('Host user id '), 
    'help' => t('user id who will orgnise the current event.'), 
    'relationship' => array(
      'base' => 'users',
      'base field' => 'uid', // The name of the field on the joined table.
      // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
      'handler' => 'views_handler_relationship', 
      'label' => t('Event node'),
     ),

    'field' => array(

      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 

    'filter' => array(

      'handler' => 'views_handler_filter_numeric',
    ), 

    'sort' => array(

      'handler' => 'views_handler_sort',
    ),
  );

$data['join_events']['invitee_user'] = array(

    'title' => t('Invited user id'), 
    'help' => t('Invited user id for current event.'), 
     'relationship' => array(
      'base' => 'users',
      'base field' => 'uid', // The name of the field on the joined table.
      // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
      'handler' => 'views_handler_relationship', 
      'label' => t('Event node'),
     ),
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

$data['join_events']['timestamp'] = array(

    'title' => t('Invite Time'), 
    'help' => t('Just a timestamp field.'), 
    'field' => array(
      'handler' => 'views_handler_field_date', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
  );
     return $data;

  }

STEP 4: 
Our Costume Table structure and data.

--
-- Table structure for table `join_events`
--

CREATE TABLE IF NOT EXISTS `join_events` (
  `sr_id` int(5) NOT NULL AUTO_INCREMENT,
  `nid` int(5) NOT NULL,
  `host_user` int(5) NOT NULL,
  `invitee_user` int(5) NOT NULL,
  `timestamp` int(11) NOT NULL,
  PRIMARY KEY (`sr_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;

-- Dumping data for table `join_events`
--

INSERT INTO `join_events` (`sr_id`, `nid`, `host_user`, `invitee_user`, `timestamp`) VALUES

(1, 3797, 1, 1757, 1483424304);

Step 5:

Now we are in the final part , navigate to admin/structure/view and create a new view with name join_events a new option called Join Events will be populated in the Show dropdown.





No comments:

Post a Comment