20

I need to set up some views that will pull some data from a custom table I created. Some views will need to pull content the regular way and from my custom table (where I may query for a particular nid, etc.).

How do I do this, or where is a good place to research?

2

2 Answers 2

27

Your module needs to implement hook_views_data().

The example given in the hook documentation is for a table defined from the following SQL.

CREATE TABLE example_table (
  nid INT(11) NOT NULL,
  plain_text_field VARCHAR(32,
  numeric_field INT(11),
  boolean_field INT(1),
  timestamp_field INT(8),
  PRIMARY KEY(nid)
);
function mymodule_views_data() {
  $data['example_table']['table']['group'] = t('Example table');

  $data['example_table']['table']['base'] = array(
    'field' => 'nid',
    'title' => t('Example table'), 
    'help' => t('Example table contains example content and can be related to nodes.'), 
    'weight' => -10,
  );

  $data['example_table']['table']['join'] = array(
    'node' => array(
      'left_field' => 'nid', 
      'field' => 'nid',
    ),
  );

  $data['example_table']['nid'] = array(
    'title' => t('Example content'), 
    'help' => t('Some example 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('Example node'),
    ),
  );

  $data['example_table']['plain_text_field'] = array(
    'title' => t('Plain text field'), 
    'help' => t('Just a plain text field.'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['example_table']['numeric_field'] = array(
    'title' => t('Numeric field'), 
    'help' => t('Just a numeric field.'), 
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['example_table']['boolean_field'] = array(
    'title' => t('Boolean field'), 
    'help' => t('Just an on/off field.'), 
    'field' => array(
      'handler' => 'views_handler_field_boolean', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_boolean_operator',
      'label' => t('Published'),
      'type' => 'yes-no',
      'use equal' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['example_table']['timestamp_field'] = array(
    'title' => t('Timestamp field'), 
    '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;
}
2
  • This has been an excellent help. However, one of my fields is multivalue, and so I think it must be set up differently -- but how?
    – Vacilando
    Commented Apr 19, 2013 at 16:07
  • The difference is in the handlers you need to use, and what you use instead of "yes-no"; I cannot tell you which handlers are for a multi-value field, though. Probably it is better as question, if nobody asked it already.
    – avpaderno
    Commented Apr 19, 2013 at 16:33
3

I think it would probably be worth investigating the Data module. This is very powerful, since it allows you to declare a non-Drupal table to Drupal such that it becomes visible in Views as a data source (like "Content", "Taxonomy", etc). You can also declare joins between the non-Drupal table and Drupal entities (so for example if you can stock a nid in your non-Drupal table, then you can declare a join on the nid with any node).

There is also a submodule which allows you to declare your non-Drupal table as an entity, but so far I've not tried that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.