3

I created a view (unformatted) associated with a content type with two simple fields for a block. The view is called "phrases" have generated two .tpl:

a) views-view-unformatted--phrases-block.tpl.php (to style row).

b) views-view-fields--phrases-block.tpl.php (to style the fields).

How to generate the link to the content, because by the variables I do not receive the direction I need.

I have available only the variable "$content", but this is formatted with something like:

<span class = "field-xx-xx"> 
     <a href="path/to/correct/content">Path</a>
</span>

I need to assign a class to the "a tag" , but as I receive all formatting can not.

Example i need:

<div class="my-class"> 
    <a class="my-link-class" href="path/to/correct/content">Path</a>
</div>

As I can generate the correct path to the content?

  • 1
    You can get the node id and then: $url = url('/node/'. $nid); – ropic Apr 28 '16 at 21:24
  • 1
    ugly way to do it, you don't want to use nid's in urls. You want to use aliases. ie: /articles/how-to-use-drupal instead of node/1 – No Sssweat Apr 28 '16 at 22:48
  • 1
    @NoSssweat : why? – Pierre.Vriens Apr 29 '16 at 20:38
  • 1
    @Pierre.Vriens it's not good for SEO and also it just looks amateurish. – No Sssweat Apr 29 '16 at 21:05
  • 1
    @NoSssweat : OK, that helps to understand your comment, merci! – Pierre.Vriens Apr 29 '16 at 21:07
1

In your view, in FIELDS add Content: Path

Then in views-view-fields--phrases-block.tpl.php add:

<div class="my-class"> 
    <a class="my-link-class" href="<?php print $fields["field_path"]->raw; ?>">Path</a>
</div>

if the above doesn't work try:

<a class="my-link-class" href="<?php print $fields["field_path"]->content; ?>">Path</a>

If the 1st and 2nd doesn't work, to make the 2nd one work, get rid off all your html markup, because it's probably printing as href="<span>/yourpath</span>"

Click on your Path field and unchecked add default classes.

enter image description here

In field settings, uncheck add field wrappers.

enter image description here

  • 1
    Thanks , had made a mistake, I was using the wrong (field link) field, but with the "path field" have solved the problem. – chalo Apr 29 '16 at 12:28

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.