2

If found this, but I can't manage to apply it to my case. Here goes. (I'm new to PHP coding and Drupal.)

I want to add a HTML link into each row of my unformatted views template. For each row I want the link for the relevant piece of content.

I used the devel dpm to reveal the variables in the unformatted.tpl.php. But I can't find a variable for the link field. (I've added the Content: Link field in views and it's hidden.)

My understanding at the moment is I'll need to set the variable in the relevant preprocess function, which might look like this:

 function mytheme_preprocess_views_view_unformatted(&$variables) {
    $link = Location of link field variable
}

And then use it in the views template:

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
    <?php print $link; ?>
  </div>
<?php endforeach; ?>

But I can't find the link information and I can't find similar examples so I've probably gone badly wrong. How would I do that and is my basic understanding ok?

0

Ok so I've managed to get it working with:

<?php print $view->style_plugin->row_tokens[$id]['[view_node]']; ?>

I tried to use:

<?php print $view->style_plugin->row_tokens[$rows['id']]['[view_node]']; ?>

But it kept throwing undefined index errors, even though it was valid path from DPM.

rows (array) id (value)

id (the variable I used in the top working solution) didn't show in my DPM. Is that because I was calling it for the views unformatted tpl and id is a global variable?

0

Previous answer didn't worked for me, as the row_tokens array was empty for some reason. Though I managed to access the field using the following:

$view->style_plugin->row_plugin->nodes[$view->result[$id]->nid]->my_field_name

Here $id is the row index, and can be used to get the nid using the $view->result array.

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.