0

I have a theme using a views tpl file that is making it impossible for a View to link to node content using the node’s aliases' that i’ve set up. For example it is linking to the default node/36 instead of projects/projectname.

the view contains a grid of ‘thumbnail like content’ wherein each image links to it’s corresponding node. the views template file is renderering links as shown below:

<a style=" background-image: url('<?php print $thumbnail;?>'); " 
class="w-inline-block portfolio-photo portfolio-1" 
href="<?php print drupal_get_path_alias('node/').$row->nid;?>">

It’s hardcoding /node/ as a directory and use the function $row->nid

Is there an alias equivalent to nid? Instead of node/36 it the url should be project/projectname

To rewrite the above code using pseudo code it would be…

<a style=" background-image: url('<?php print $thumbnail;?>');"
class="w-inline-block portfolio-photo portfolio-1" 
href="<?php print drupal_get_path_alias(‘projects/‘).$row->alias;?>”>

1 Answer 1

2

You should use the l() function:

Formats an internal or external URL link as an HTML anchor tag.

This function correctly handles aliased paths and adds an 'active' class attribute to links that point to the current page (for theming), so all internal links output by modules should be generated by this function if possible.

So:

<?php print l('Link text', 
              'node/' . $row->nid, 
               array(
                 'attributes' => array(
                   'class' => array(
                     'w-inline-block',
                     'portfolio-photo',
                     'portfolio-1')))); ?>

You have to supply classes and any other attributes to the l() function.

Note that the first l() parameter (the 'Link text') can be HTML. For this you have to pass in the options array an element HTML set to TRUE. The above example can be:

<?php print l('<img src="/someimg">Some text</img>', 
              'node/' . $row->nid, 
               array(
                 'attributes' => array(
                   'class' => array(
                     'w-inline-block',
                     'portfolio-photo',
                     'portfolio-1')
                   )
                 'html' => TRUE,
                 )
             ); ?>

Also, as noted by the documentation fragmento above, the l() functions handles the aliases. This is because internally drupal_get_path_alias() function is used.

3
  • i'm editing here because i'm was in error with my first comment. there is no link text but instead the anchor has a background image. how do i get that into this function?
    – TopTomato
    Commented Feb 24, 2016 at 22:54
  • The first l() parameter can be HTML (so it can be an a tag). Also, l() function uses url() function to generate the link. url() internally uses drupal_get_path_alias().
    – sanzante
    Commented Feb 24, 2016 at 22:59
  • I've updated my answer.
    – sanzante
    Commented Feb 24, 2016 at 23:06

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.