function hook_views_data

Same name in other branches
  1. 7.x-3.x views.api.php \hook_views_data()

Describe table structure to Views.

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. This must either be in the same directory as the .module file or in a subdirectory named 'includes'.

The full documentation for this hook is in the advanced help. http://views-help.doc.logrus.com/help/views/api-tables

Related topics

16 functions implement hook_views_data()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_views_data in modules/aggregator.views.inc
Implementation of hook_views_data()
book_views_data in modules/book.views.inc
Implementation of hook_views_data()
comment_views_data in modules/comment.views.inc
Implementation of hook_views_data()
contact_views_data in modules/contact.views.inc
@file Provide views data and handlers for contact.module
filter_views_data in modules/filter.views.inc
Implementation of hook_views_data()

... See full list

1 invocation of hook_views_data()
_views_fetch_data in includes/cache.inc
Fetch Views' data from the cache

File

docs/docs.php, line 73

Code

function hook_views_data() {
    // This example describes how to write hook_views_data() for the following
    // table:
    //
    // CREATE TABLE example_table (
    //   nid INT(11) NOT NULL         COMMENT 'Primary key; refers to {node}.nid.',
    //   plain_text_field VARCHAR(32) COMMENT 'Just a plain text field.',
    //   numeric_field INT(11)        COMMENT 'Just a numeric field.',
    //   boolean_field INT(1)         COMMENT 'Just an on/off field.',
    //   timestamp_field INT(8)       COMMENT 'Just a timestamp field.',
    //   PRIMARY KEY(nid)
    // );
    // The 'group' index will be used as a prefix in the UI for any of this
    // table's fields, sort criteria, etc. so it's easy to tell where they came
    // from.
    $data['example_table']['table']['group'] = t('Example table');
    // Define this as a base table. In reality this is not very useful for
    // this table, as it isn't really a distinct object of its own, but
    // it makes a good example.
    $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,
    );
    // This table references the {node} table.
    // This creates an 'implicit' relationship to the node table, so that when 'Node'
    // is the base table, the fields are automatically available.
    $data['example_table']['table']['join'] = array(
        // Index this array by the table name to which this table refers.
        // 'left_field' is the primary key in the referenced table.
        // 'field' is the foreign key in this table.
'node' => array(
            'left_field' => 'nid',
            'field' => 'nid',
        ),
    );
    // Next, describe each of the individual fields in this table to Views. For
    // each field, you may define what field, sort, argument, and/or filter
    // handlers it supports. This will determine where in the Views interface you
    // may use the field.
    // Node ID field.
    $data['example_table']['nid'] = array(
        'title' => t('Example content'),
        'help' => t('Some example content that references a node.'),
        // Because this is a foreign key to the {node} table. This allows us to
        // have, when the view is configured with this relationship, all the fields
        // for the related node available.
'relationship' => array(
            'base' => 'node',
            'field' => 'nid',
            'handler' => 'views_handler_relationship',
            'label' => t('Example node'),
        ),
    );
    // Example plain text field.
    $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',
        ),
    );
    // Example numeric text field.
    $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',
        ),
    );
    // Example boolean field.
    $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 boolean_field = 1 instead of boolean_field <> 0 in WHERE statment
'use equal' => TRUE,
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
    );
    // Example timestamp field.
    $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;
}