Manipulating Drupal 7 Views programmatically

Geschreven op 12 juli 2019 door Nathan Vergunst-Kolozsvári

As a site builder, there is a lot of information on how to configure Views on the internet. As a developer, however, it can be hard to find useful code snippets to manipulate Views according to your needs. In this article, I will summarize a few very useful techniques to manipulate your View.

Views basics

In order to understand how Views work it is useful to export one and to take a closer look at what Views are made of. Below I will highlight a few components and provide some simple explanation:

  • The $view itself is an object. Each View uses one data source, which can be found in the export as $view->base_table. Usually this is a database table like 'node' or 'users', but it can also be a Search API index etcetera.
  • The Views displays are the pages, blocks, feeds etcetera. One display is basically a set of conditions to query the database and the settings for displaying the query results. Note that if you are looking for variables in the View programmatically, you can find them in $view->display['default'] for default settings and in $view->display['display-id'] for overridden settings!
  • The Views display options contain the settings for the particular display, including the filters, the sort options and the settings for displaying the results.
  • Contextual filters. Contextual filters are filters that are provided by the actual page you are viewing. Often this is the ID of a node or a user. The ID is passed into the View as a filter value. In code you can find the contextual filter in the $view->args variable.

Useful snippets

Overriding View fields

Use template_preprocess_views_view_field() to override the output of a Views field. Rename the function to [your_module]_preprocess_views_view_field in the .module file or [your_theme]_preprocess_views_view_field in template.php and you're good to go. Below you will find an example:

function my_module_preprocess_views_view_field(&$variables) {
// Check for the machine name of your View and the machine name of the display ('page', 'block' etc).
  if ($variables['view']->name == 'my_view' 
    && $variables['view']->current_display == 'page') {

// Identify the field you wish to override
    $field = $variables['field']->options['id'];

    switch ($field) {
      case 'my_field_name':
              // Maybe check if the field has a value.
        if (!empty($variables['row']->my_field_name)) {
                  // Maybe check for a specific value.
          if ($variables['row']->my_field_name == 'my_value') {
                        // Use the $variables['output'] variable to override the field value. 
            $variables['output'] = 'My alternative value'

Overriding Views page title

Overriding a Views page title can be done with hook_preprocess_views_view.

 * Implements hook_preprocess_views_view().
function my_module_preprocess_views_view(&$variables) {
    if ($variables['view']->name == 'my_view' 
        && $variables['view']->current_display == 'page') {
        $view->build_info['title'] = t('My alternative title');

Overriding default Views

Drupal provides a couple of default Views out of the box, like the content overview (/admin/content). These can be overridden by using hook_views_default_views_alter

 * Implements hook_views_default_views_alter().
function my_module_views_default_views_alter(&$views) {
  $handler = $views['admin_views_node']->display['default']->handler;
  /* Field: Content: Language */
    // This adds and extra column for item language in the content overview.
  $handler->display->display_options['fields']['language']['id'] = 'language';
  $handler->display->display_options['fields']['language']['table'] = 'node';
  $handler->display->display_options['fields']['language']['field'] = 'language';

Reactie toevoegen

Plaats hier uw naam.