19

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?

25

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;
}
  • 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 Apr 19 '13 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. – kiamlaluno Apr 19 '13 at 16:33
  • Thanks, @kiamlaluno -- OK, so I looked, did not find, then ventured to create a separate question -- see drupal.stackexchange.com/questions/70561/… – Vacilando Apr 19 '13 at 21:11
2

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, privacy policy and cookie policy

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