I'm using Drupal 7. I'm creating a view based on profile. It will show a list of all the people. Now I'm overriding the file views-view-unformatted--MYVIEW-page.tpl.php and I would like to add in the class of the row the ID of that user. If I write

global $users;

of course it's giving me the id of the current user and not the uid of that profile.

I would like to create something like this: FIRST EXAMPLE:

    $this_uid = I NEED THIS TOKEN;
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes_array[$id]; print " ".$this_uid;?>">
        <?php print $row; ?>
    <?php endforeach; ?>

I could create a custom text field in the view a put all the field inside the <li> tag like: SECOND EXAMPLE:

<li class="[user]">[name] [gender]</li>

and the file tpl.php will become

<?php foreach ($rows as $id => $row): ?>
            <?php print $row; ?>
        <?php endforeach; ?>

But in this way I will lose the default row classes...

How can i get the first example? Do I need to create a view based on user instead of profile? Or should I use the second example?

Thanks in advance.

2 Answers 2


You should have a $views variable available with an array similar to $rows called $view->result[] that contains the unrendered/raw view results and just as long as you have a field in your output that has the uid in it, you will be able to access it directly here. (It can be a hidden field if you don't want it rendered later on) Put a drupal_set_message('<pre>' . print_r($view->result, true) . '</pre>'); or its equivalent in your template to take a look at it, but basically you should be able to use something like $view->result[$id]->FIELDNAME to access it or any other raw data.

  • ok, I've got this Array ( [0] => stdClass Object ( [users_profile_picture] => 76 [users_profile_uid] => 67 How do I get the users_profile_uid? I've tried different way like $view->result[$id]['users_profile_uid'], $view->result[$id][users_profile_uid], $view->result['id']['users_profile_uid'] etc... I does not work... Commented Apr 18, 2012 at 13:17
  • It works! Sorry I haven't read properly the $view->result so now I wrote $view->result[$id]->users_profile_uid; and it works. Thank you very much indeed. Commented Apr 18, 2012 at 13:23

Use views PHP field, set it as hidden, then use PHP to save needed value in this field. After that, views template will be able to get that value.

Make sure you clear cache and rebuild theme registry.

  • Do you want me to do something like: $VALUE_I_NEED = [user]; and in the view can I use it as I want? Commented Apr 18, 2012 at 10:56
  • May you be more specific? Your method does not seem to work... Commented Apr 18, 2012 at 11:23

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.