How do you expose your legacy database tables to views in drupal 6 Part 1

This is a 2 part post, that shows how I exposed a legacy table to views in drupal and used the incredible power of views to create many combinations static and conditional views. This was very helpful for my client.

I had a requirement that warranted exposing existing database table to views, so that views can contain data from this table.  To enable this to happen, the following steps need to be taken.

In a custom module you need to first define the views_api hook as following:

function candidate_views_api(){
    $view = array();
    $view = array('api' => 2,
                  'path' => drupal_get_path('module','candidate'). '/views');
    return $view;
}

candidate module was a custom module which I already had, so it made sense to add this hook here. 

The important bit here is telling the drupal sub-system where the views file will reside. 

Then create a file with your <modulename>.views.inc in my case it was candidate.views.inc, this file should have two hooks, one hook_views_data() that exposes your table to drupal and then one which can define custom handlers for you data.

function candidate_views_data(){
    $data = array();
    $data['candidate']['table']['group'] = t('Candidate');

    $data['candidate']['table']['base'] = array(
        'field' => 'candidate_ref',
        'title' => t('Candidate'),
        'help' => t("Candidate content."),
        'weight' => -10,
    );

    $data['candidate']['candidate_ref'] = array(
        'title' => t('Candidate Ref'),
        'field' => array(
          'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
    );    
  
    $data['candidate']['candidate_first_name'] = array(
        'title' => t('First Name'),
        'field' => array(
          'click sortable' => TRUE,
        ),
    );

    $data['candidate']['candidate_gender'] = array(
        'title' => t('Gender'),
        'field' => array(
          'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'candidate_handler_filter_gender',
        ),
    );

    return $data;
}

I have removed all the other fields from the above function for brevity of the post, and it is enough to explain the point.

some explanation about the code above.

$data['candidate']['table']['base']

tells drupal what is the base table, base tables, because you can also join tables using a unique field in each table, for eg. you may want to join the candidate table with the node table.

then definition of all your fields is just like any other drupal array, important things of note is the field which you can decide it to be sortable, and the filter. in case of views when you want to define views based on filters and you want these to appear in conditions options while defining views through the admin screen.

Part 2 will show how to define custom handlers for your views.

Comments

Popular posts from this blog

Install the right version of php_mongo.dll in windows

Installing sqlyog on Mac OS X Yosemite

Use ProxySQL to obfuscate data for development.