How to add a custom variable in Drupal’s views template file

| | 1 min read

Drupal offers flexible support for theming by allowing us to edit its template files. If we want to add a few custom variables other than the default variables then we need to do some coding within the template file. If you want to know how to add a custom variable in a Drupal's View template file then read on to find out more.

Follow the steps below to edit the View template file

STEP 1

  • Edit the View on which you want to edit the template file.
  • Click the theme information link from the basic settings.
  • Copy and paste the template file from the Views theme information link and save the content to a template file.



sitemap

STEP 2

  • Add a theme registry alter function in your module
  • 'views_view_row_rss__testfeed__feed_1 is the template file you have copied to the theme directory (hyphens are replaced with underscore)
  • "you_module_views_view_row_rss__testfeed__feed_1" is the custom preprocessor function.


view2

function your_module_name_theme_registry_alter(&$theme_registry) {
  $theme_registry['views_view_row_rss__testfeed__feed_1']['preprocess functions'][] = 'your_module_views_view_row_rss__testfeed__feed_1';
}

STEP 3

  • Add a custom variable to your pre processor function
function you_module_views_view_row_rss__testfeed__feed_1(&$vars) {
  $vars['copyright'] = $copy_right;
}

Rescan the template file and add the variable $copyright in the newly created template file.
Hope this helps.

If you have any feedback get in touch with us using the comment form below.