Drupal7: Amend View Header when no View Results present

David G - DrupalI have more than a few websites out in the wild (live). And occasionally all have some minor touch-ups they require from time to time for odd bugs or improvements — even though in general I consider the site completed and live. Recently a client approached me with an issue where some tokens within a View Header Area was not be replaced and I needed to quickly think of a fix. This blog post will outline the issue and solution I used.

The problem

On a faculty content type, I want to show courses they offer in the current academic quarter. The final output should look like this:


But when I originally created the View and inserted it onto the faculty content type using the Views Field View, I didn’t notice that sometimes the page looks like this:


Well, of course, the client noticed so I had to resolve the matter to show the page as desired.

Generally speaking, if an instructor is actively teaching courses in the current quarter then the page shows the desired useful information in the View:

The default view output for instructors actively teaching courses.

The default view output for instructors actively teaching courses.

The Underlying Rationale for the Problem

As described within this issue thread on Drupal.org with a similar issue in a contributed module:  https://www.drupal.org/node/2503873  this is simply the default behavior of Views. Meaning if the View has no results than tokens are not replaced with any values as there are no fields to pull replacement values from.

Possible solutions to this problem are:

  1. Use Views PHP to figure out the values and the Views global object to insert it into the Views header using PHP code as the input format (eww).
  2. Change the Views Header to it’s own View that simply queries the necessary site variables (eww, 2 Views now?!)
  3. Try to alter the rendered Views output to see if we got results and if not, fix the Header content.

Ultimately, I went with the third option above as within the same linked issue thread there is a patch outlining the necessary steps in code to take.

The Code Solution

Within an existing custom module for the site, I assured my hook_views_pre_render() has the following code:

 // DPG 02-01-2016
  // If an Instructor has no courses for the active quarter then the
  // Views Header token replacement does not occur by convention in
  // Drupal 7. The patch in this thread outlines a workaround using
  // Views hooks, which we implement below:
  // https://www.drupal.org/node/2503873
  if ($view->name === 'courses_for_instructor') {
    foreach ($view->header as $key => $header) {
      if ($header->options['tokenize'] && !isset($view->row_index)) {
        $quarter = variable_get('ccs_current_quarter', '');
        $year = variable_get('ccs_current_year', '');
        $view->header[$key]->options['content'] = "This instructor is currently teaching the following courses for $quarter $year:";

In the code above we look to see if the view header contains tokenized and check if the view has any results. If not we grab the site variables in question (quarter and year) and insert a string into the View Header with the correct information.

Then our view doesn’t show any of the ugly token markups if the View contains no results. This happens for example with the instructor is on vacation or sabbatical for the quarter. I hope you’ve found this blog post helpful in better understanding Drupal!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *