1

I am preprocessing a view in order to create a custom variable and then pass it to a view template file.

I have added the following code to the template.php file:

function mycustomtheme_preprocess_views_view(&$vars) {
    $view = $vars['view'];

    $currentpath = current_path();
    $path_alias = drupal_lookup_path('alias',$currentpath);

    // define the custom variable
    $vars['mycustomvariable'] = '';

    if ($view->name == 'mycustomviewname') {

        foreach($view->result as $key => $result) {
            $tid = $result->tid; // get the taxonomy term's ID
            $termpath = drupal_get_path_alias('taxonomy/term/' . $tid); // term's path
            // if the current path is same to the one of the term
            if ($termpath == $path_alias) {
                // give a value to the custom variable
                $vars['mycustomvariable'] = 'blablabla';
            }
        }
    }
}

Then in the views-view-list--mycustomviewname--block_1.tpl.php file, I try to print the variable like this:

<?php print $mycustomvariable; ?>

But I am getting the error:

Notice: Undefined variable: mycustomvariable in include() (line 18 of views-view-list--mycustomviewname--block_1.tpl.php)

I have spent a considerable amount of time on this issue and used dpm and print_r to print out variables, arrays and objects but I cannot seem to find solution to this issue.

Any help would be appreciated. Thank you in advance.

6
  • Are you sure the if ($termpath == $path_alias) condition holds?
    – M a m a D
    Commented Apr 26, 2015 at 20:07
  • @Drupalist Yes the condition is met. I tested it by printing out something when it's met and it works. I also tried to print the custom variable outside the if clause, but I still get the same error. Any clues?
    – consuela
    Commented Apr 26, 2015 at 20:27
  • are you sure the variable is being assigned correctly - what does a dpm() of the $vars at the end of the function show for the variable?
    – Geoff
    Commented Apr 26, 2015 at 21:47
  • @Geoff a dpm($vars) right after $vars['mycustomvariable'] = 'blablabla'; gives "mycustomvariable (String, 9 characters ) blablabla". So, I am guessing the variable is correctly assigned to the custom variable. So, is my way of trying to print the variable inside the template file wrong or am I missing something else? Thank you for your help.
    – consuela
    Commented Apr 26, 2015 at 22:50
  • 1
    template_preprocess_views_view preprocesses variables for views-view.tpl.php. It looks like your view has a template based on views-view-list.tpl.php. Try using mytheme_preprocess_views_view_list instead of mytheme_preprocess_views_view.
    – calebtr
    Commented Apr 27, 2015 at 6:32

1 Answer 1

1

I'm not fully sure what I'm going to tell you is completely true, it is just my experience of how Views works.

From what I know, Every preprocess function has access to a specific output template file. As an example variables of template_preprocess_views_view_fields are available in views-view-fields--AnyThingElse.tpl.php template file, so the domain access of variables of template_preprocess_views_view_fields is views-view-fields--AnyThingElse.tpl.php.

In Theme Information there are three levels of output styles:

  1. Display output
  2. Style output
  3. Row style output

The views-view-list--mycustomviewname--block_1.tpl.php is a TPL file in the second level (Style output) but I guess the mycustomtheme_preprocess_views_view domain of access of its variables is in the first level (Display output) TPL files.

To make sure, select one of the TPL files from Display output and check if the variable is set in there.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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