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.

Comments

imranweb’s picture

Make sure you are using theme name instead of module name for naming the preprocess function in template.php file.
e.g. mymodule_ should be replace by mytheme_

Please check whether control goes into the if condition block.
Otherwise it should work after clearing the cache.

-Imran

shabana.navas’s picture

Yeah, sorry about naming it "mymodule", but, it is appropriately named with the theme name.

Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba

shabana.navas’s picture

I actually saw those posts and if you see the code that I posted, you can see that I have the workaround function for d7 in template.php to access my specific view. I can actually access $vars in this function:

function mytheme_preprocess_views_view_fields__badge_owners__page_1()

And I can add the data to vars and at the end of the function when I dpm($vars), I see the data added. However, in my views-view--badge-owners.tpl.php file, when I have print $badge or print $an_extra_variable, it doesn't display anything. So, basically, whatever is being added in template.php is not being passed to my tpl function.

Shabana

Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba

shabana.navas’s picture

My question was partially answered here:

http://drupal.stackexchange.com/questions/55049/cannot-access-data-passe...

However, I still am not able to display the title. For some reason, this line isn't working:

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

Any thoughts....

Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba

imranweb’s picture

Can u try this?

$view->display['page_1']->handler->handlers['argument']['type']->options['title'] = 'Your custom page title';

shabana.navas’s picture

Unfortunately, that didn't work. However, the person that solved the $vars problem also gave me the answer to this title problem. Basically, doing this:

$vars['view']->set_title(t('Badge: @badge_name', array('@badge_name' => $badge->name)));

solved it. Nevertheless, thanks so much for your help. Really appreciate it.

Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba