1

I have created a view that is grouped by taxonomy term and it displays exactly how I want it. But what I need to do is add the term ID that the view is grouped by as a class, I have the following custom views template created but the term ID is not available as a variable to use.

The template is views-view-unformatted--products.tpl.php where I have printed the title as the class of the wrapper I need to add the term ID instead (the title is actually the term name as this is what the view is grouped by.)

<div class=<?php print '$title)'; ?>>
  <?php if (!empty($title)): ?>
    <h3 class="quick-view-closed"><?php print $title; ?></h3>
  <?php endif; ?>
  <div class="product-list" hidden>
    <?php foreach ($rows as $id => $row): ?>
      <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
        <?php print $row; ?>
      </div>
    <?php endforeach; ?>
  </div>
</div>
0

2 Answers 2

2

I haven't time to actually investigate the details of this right now, but if you enable the devel module, put

<?php dpm(get_defined_vars()); ?>

somewhere in your template file somewhere, and then refresh the page twice you will get a list of available variables which you can then click on to expand and drill down into the arrays and objects.

If you still cannot find the tid anywhere to use, try adding the tid as a hidden field to your view, then it will definitely be in there somewhere for you to use.

3
  • The problem is that the this template doesn't include all the fields as variables, it only includes the rendered row. Commented Oct 16, 2013 at 13:43
  • But I imagine it also includes things like the whole view object, which contains all the fields and all the results and everything.
    – rooby
    Commented Oct 16, 2013 at 13:44
  • It is but unfortunately it's that large it is difficult to extract the information from it without looping which I want to avoid, my solution is below Commented Oct 16, 2013 at 14:10
2

What I had to do is use the term ID to group the results of the view and then in the views template use the function taxonomy_term_load() and then render the template as below.

<?php $binder_title = taxonomy_term_load($title); ?>
<div class=<?php print '"binder binder-' . $title . '"'; ?>>
  <?php if (!empty($title)): ?>
    <h3 class="quick-view-closed"><?php print $binder_title->name; ?></h3>
  <?php endif; ?>
  <div class="product-list" hidden>
    <?php foreach ($rows as $id => $row): ?>
      <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
        <?php print $row; ?>
      </div>
    <?php endforeach; ?>
  </div>
</div>
2
  • But taxonomy_term_load() takes a tid right, so is $title a tid?
    – rooby
    Commented Oct 16, 2013 at 14:16
  • Yeah I changed the view to group by tid rather than term title then used that to pull the name through. Commented Oct 16, 2013 at 14:39

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.