1

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(&$vars) {
  if (isset($vars['view']->name)) {
    $function = __FUNCTION__ . '__' . $vars['view']->name . '__' . $vars['view']->current_display;
    if (function_exists($function)) {
     $function($vars);
    }
  }
}


/**
* preprocess variables for the badge owners view
*/
//function mytheme_preprocess_views_view__badge_owners (&$vars) {
function mytheme_preprocess_views_view_fields__badge_owners__page_1(&$vars) {
  $bid = $vars['view']->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' => $bid
  ))->fetchObject();

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

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

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

<?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
*/
?>
<div class="view view-<?php print $css_name; ?> view-id-<?php print $name; ?> view-display-id-<?php print $display_id; ?> view-dom-id-<?php print $dom_id; ?>">
  <?php if (isset($admin_links)): ?>
    <div class="views-admin-links views-hide">
      <?php print $admin_links; ?>
    </div>
  <?php endif; ?>

  <div class="view-header clearfix">
  <div class="mytheme-badge-owners-badge"><?php print $badge;?></div>
</div>

  <?php if ($exposed): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
    </div>
  <?php endif; ?>

  <?php if ($attachment_before): ?>
    <div class="attachment attachment-before">
      <?php print $attachment_before; ?>
    </div>
  <?php endif; ?>

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

  <?php if ($pager): ?>
    <?php print $pager; ?>
  <?php endif; ?>

  <?php if ($attachment_after): ?>
    <div class="attachment attachment-after">
      <?php print $attachment_after; ?>
    </div>
  <?php endif; ?>

  <?php if ($more): ?>
    <?php print $more; ?>
  <?php endif; ?>

  <?php if ($footer): ?>
    <div class="view-footer">
      <?php print $footer; ?>
    </div>
  <?php endif; ?>

  <?php if ($feed_icon): ?>
    <div class="feed-icon">
      <?php print $feed_icon; ?>
    </div>
  <?php endif; ?>

</div> <?php /* class view */ ?>

Any help would be appreciated. Thanks.

1
  • Have you tried assigning the $badge vars as an array on $vars['view'] instead? e.g $vars['view']->badge = $badge_info_array; – David Thomas Jan 7 '13 at 8:42
1

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(&$vars){
  if (isset($vars['view']->name)) {
    switch($vars['view']->name){
      case 'badge_owners':
        if($vars['view']->current_display == 'page_1'){
          // Rename current function to this
          THEMENAME_preprocess_views_view_badge_owners_page_1($vars);
        }
      break;
    }
  }
}
3
  • Thanks, that is excellent, it is working now! But there is one tiny problem still, setting the title of the view. For some reason, this line is not working: drupal_set_title(t('Badge: @badge_name', array('@badge_name' => $badge->name))); Any thoughts? – SN_26 Jan 7 '13 at 9:09
  • Glad to hear it!, regarding setting the page title, perhaps you can use $vars['view']->set_title($title); there instead? Otherwise may need some way to get the title to hook_preprocess_page – David Thomas Jan 7 '13 at 9:28
  • 1
    Awesome!!!! Thanks, that worked like a charm! Saved me 2 days of work! – SN_26 Jan 7 '13 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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