Cannot access data passed to $vars in theme function I have got a problem in my template.php file, where I am trying to add data to $vars. I basically have a preprocess function for a view and in it I am trying to pass some extra info to $vars. However, the view is not getting those variables. This is what I have in my template.php file:

/**
  * Generic preprocess that is still working on D7
  */
function mytheme_preprocess_views_view_fields(&amp;$vars) {
  if (isset($vars['view']-&gt;name)) {
    $function = __FUNCTION__ . '__' . $vars['view']-&gt;name . '__' . $vars['view']-&gt;current_display;
    if (function_exists($function)) {
     $function($vars);
    }
  }
}


/**
* preprocess variables for the badge owners view
*/
//function mytheme_preprocess_views_view__badge_owners (&amp;$vars) {
function mytheme_preprocess_views_view_fields__badge_owners__page_1(&amp;$vars) {
  $bid = $vars['view']-&gt;args[0];

  //Get the badge info
  $badge = db_query("
    SELECT ubb.bid, ubb.weight, ubb.name, ubb.image, ubb.href,
    ubb.unhideable, ubb.fixedweight, ubb.doesnotcounttolimit, ubb.tid
    FROM {user_badges_badges} ubb
    WHERE ubb.bid = :bid", array(
      ':bid' =&gt; $bid
  ))-&gt;fetchObject();

  $vars['bid'] = $bid;
  $vars['name'] = $badge-&gt;name;
  $vars['badge'] = theme('user_badge', array('badge' =&gt; $badge));
  $vars['an_extra_variable'] = 'hope this shows up in $vars';

  drupal_set_title(t('Badge: @badge_name', array('@badge_name' =&gt; $badge-&gt;name)));
}


This is what I have in my views-view--badge-owners.tpl.php file:

&lt;?php
/**
* Variables available:
* - $css_name: A css-safe version of the view name.
* - $header: The view header
* - $footer: The view footer
* - $rows: The results of the view query, if any
* - $empty: The empty text to display if the view is empty
* - $pager: The pager next/prev links to display, if any
* - $exposed: Exposed widget form/info to display
* - $feed_icon: Feed icon to display, if any
* - $more: A link to view more, if any
* - $admin_links: A rendered list of administrative links
* - $admin_links_raw: A list of administrative links suitable for theme('links')
*
* @ingroup views_templates
*/
?&gt;
&lt;div class="view view-&lt;?php print $css_name; ?&gt; view-id-&lt;?php print $name; ?&gt; view-display-id-&lt;?php print $display_id; ?&gt; view-dom-id-&lt;?php print $dom_id; ?&gt;"&gt;
  &lt;?php if (isset($admin_links)): ?&gt;
    &lt;div class="views-admin-links views-hide"&gt;
      &lt;?php print $admin_links; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;div class="view-header clearfix"&gt;
  &lt;div class="mytheme-badge-owners-badge"&gt;&lt;?php print $badge;?&gt;&lt;/div&gt;
&lt;/div&gt;

  &lt;?php if ($exposed): ?&gt;
    &lt;div class="view-filters"&gt;
      &lt;?php print $exposed; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($attachment_before): ?&gt;
    &lt;div class="attachment attachment-before"&gt;
      &lt;?php print $attachment_before; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($rows): ?&gt;
    &lt;div class="view-content"&gt;
      &lt;?php print $rows; ?&gt;
    &lt;/div&gt;
  &lt;?php elseif ($empty): ?&gt;
    &lt;div class="view-empty"&gt;
      &lt;?php print $empty; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($pager): ?&gt;
    &lt;?php print $pager; ?&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($attachment_after): ?&gt;
    &lt;div class="attachment attachment-after"&gt;
      &lt;?php print $attachment_after; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($more): ?&gt;
    &lt;?php print $more; ?&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($footer): ?&gt;
    &lt;div class="view-footer"&gt;
      &lt;?php print $footer; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

  &lt;?php if ($feed_icon): ?&gt;
    &lt;div class="feed-icon"&gt;
      &lt;?php print $feed_icon; ?&gt;
    &lt;/div&gt;
  &lt;?php endif; ?&gt;

&lt;/div&gt; &lt;?php /* class view */ ?&gt;


Any help would be appreciated. Thanks.
OK, it looks as though you're using the $badge variables in a more general parent/container views template, when the $badge variables are added in a child template preprocess function. The tpl variables aren't available at that container point.

The solution is to implement 

hook_preprocess_views_view 

and add the badge data vars there instead.

e.g 

THEMENAME_preprocess_views_view(&amp;$vars){
  if (isset($vars['view']-&gt;name)) {
    switch($vars['view']-&gt;name){
      case 'badge_owners':
        if($vars['view']-&gt;current_display == 'page_1'){
          // Rename current function to this
          THEMENAME_preprocess_views_view_badge_owners_page_1($vars);
        }
      break;
    }
  }
}