5

So basically i want to add a link to original node around each row in views. I have added the following code to the style and row template override in views. Both come up with the same result.

<?php $image = file_create_url($row->field_field_display_image[0]['rendered']['#item']['uri']); ?>
<a href="www.google.com"/>
  <div class="block__inner" style="background-image: url('<?php echo $image; ?>');">
    <?php foreach ($fields as $id => $field): ?>
      <?php if (!empty($field->separator)): ?>
      <?php print $field->separator; ?>
    <?php endif; ?>
  </div>
</a>

Now the issue that i am having is that the link is added to each field element that is outputted for some reason, and not just the link around the whole row and each field.

I cannot use custom field as that is making the same issue. It seems views is auto closing the a tag.

Any known workarounds?

2
  • This issue always bugged me, still does. The only workaround I have is to use jQuery. I don't get it, a display: block anchor is a perfectly valid wrapper element imho.
    – Beebee
    Jan 19, 2015 at 14:33
  • I've literally spent hours trying to debug this, seems like something that should quite easily work, and am hoping there is a way other than jquery. May raise a bug on drupal.org issue page
    – Key
    Jan 19, 2015 at 14:59

3 Answers 3

8

I usually remove all default formatting from every field, then hide all of my fields from display apart from the last field in views, and also remove all formatting, making the hidden fields plain text where possible.

Then in that last fields settings, rewrite the results, and input all of the fields as tokens from the replacement patterns.

Then instead of div elements, I use span which plays better inside an anchor, and just add unique classes for each span element.

So an example rewrite results might be...

<a href="[path]">
<span class="title">[title]</span>
<span class="body">[body]</span>
</a>

This should not suffer the same issue if you go to each hidden field, and under the style fieldset, make sure than no default wrapper or classes are applied, and also that each field is plain text. This will strip all formatting and place it inside the rewritten span elements. Like @BeeBee says, div and p will break the anchor wrapper.

4
  • found this has the same issue, but i need to use rewriting of another field, if one is empty, which means this option isn't a possibility for me.
    – Key
    Jan 19, 2015 at 14:56
  • 1
    Spans inside an anchor is fine @Collins . It's the block elements such as div and p that cause anchors to auto-close, because someone in Views or Drupal team decided anchor tags are not valid wrappers, even if they're set to display:block
    – Beebee
    Jan 19, 2015 at 15:09
  • 1
    This should not suffer the same issue if you go to each hidden field, and under the style fieldset, make sure than no default wrapper or classes are applied, and also that each field is plain text. This will strip all formatting and place it inside the rewritten span elements. Like @BeeBee says, div and p will break the anchor wrapper.
    – Collins
    Jan 20, 2015 at 11:47
  • This is a decent workaround!
    – Beebee
    Jan 20, 2015 at 14:18
2

Override views template for Style output

<?php

/**
 * @file
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php $i = 0;?>
<?php foreach ($rows as $id => $row): ?>
  <a href="node/<?php print $view->result[$i]->nid;?>">
  <?php $i++;?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
  </a>
<?php endforeach; ?>

Make sure there are no inner links (title with link to original content for instance).

1
  • no need for the $i I think, $view->result[$id] should work
    – Melissa
    Jul 14, 2016 at 20:43
1

I have found that the best control for changing markup on views (by row) is to set the view to be an unformatted list.

See here: https://api.drupal.org/api/views/theme!views-view-unformatted.tpl.php/7

Also, test carefully when you wrap elements in your anchor tag. with older versions of IE specifically, but any browser really. anchors don't play well with a bunch of other markup inside them.

1
  • Unformatted list is what i almost always use for views, am still having the issue and using Chrome.
    – Key
    Jan 19, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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