2

I create a about-us with Basic Page content type, and with path alias aliased it to \about-us( nid = 2),Now in template file I want to get path alias

<li><a href="{{  ??????? }}">{{ 'About Us'|t }}</a></li>

in Drupal 7 I got it with url("node/2") but what is equivalent in Drupal 8? what snippet I have to use to get path alias of nid 2 in template file ?

  • template file of the about us page or something else? – No Sssweat Jun 30 '16 at 7:32
  • @NoSssweat in front page template , it is not current page – Yusef Mohamadi Jun 30 '16 at 7:33
  • ok that makes more sense, why not just put <a href="/about-us"> instead of using some function to get it? – No Sssweat Jun 30 '16 at 7:34
  • @NoSssweat you right but I want learn how can I work with url path alias in template in drupal 8( maybe I change node path alias in future than I have to change tempalate link too). – Yusef Mohamadi Jun 30 '16 at 7:36
13

You can use path('entity.node.canonical', {'node': dd.nid}) in the template file.

The markup in the template file would look like this.

<li><a href="{{path('entity.node.canonical', {'node': dd.nid})}}">{{ 'About Us'|t }}</a></li>

I haven't tried it, but it should work.

4

In Summary if you want to generate Relative Url use path and if you want to generate Absolute Url use url So the solution for this question is for relative URL

 path('entity.node.canonical', {'node': node.nid}) 

and for absolute URL

 url('entity.node.canonical', {'node': node.nid}) 

Also, I've provided some details about the path, url and link functions to help other guys received this question ( as I received mine after 1 year)

url($name, $parameters, $options)

Generate an absolute URL given a route name and parameters:

<a href="{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>

Generate an absolute URL to the current url:

<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>

Generate an absolute URL to the front page:

<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>

path($name, $parameters, $options)

Generates a [relative] URL path given a route name and parameters.

{# Link to frontpage view. #}
<a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>

{# Link to user entity/profile page. #}
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>

{# Link to node page. #}
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'View node page'|t }}</a>

The url and path function are defined in close parallel to those found in \Symfony\Bridge\Twig\Extension\RoutingExtension.

link($text, $url, $attributes)

This helper function accepts as first parameter the text and as second parameter the url

Examples:

{{ link(item.title, item.url, { 'class':['foo', 'bar', 'baz']} ) }}

file_url($uri)

This helper function accepts a relative path from the root and creates a relative URI path to the file.

{{ file_url(node.field_example_image.entity.uri.value) }}
  • are you supposed to replace dd in this {'node': dd.nid} – paulcap1 Feb 27 '18 at 0:23
  • This now works for me. {{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_site_structure.entity.tid.value}) }} – paulcap1 Feb 27 '18 at 0:51
  • @paulcap1 thank you for your feedback, I think this {{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_site_structure.entity.tid.value}) }} works for you. – Yusef Mohamadi Feb 27 '18 at 6:31
0

I don't like sticking node ids in templates (it's just asking for a user down the line to remove the node and end up re-creating the page with a new node id).

I've done this and it seems ok:

<a href="{{ path('<front>') ~ 'some/path/here' }}">My Link</a>

Note that by default the path returned by will be / so you don't want a leading slash in the text you append.

Someone more experienced than me may be able to chime in if it's a bad idea to use front as your base path.

EDIT: Obviously I'm aware this approach has it's issues too (what if some user changes the alias for the page and breaks the links in the template?). However, users have much easier control over aliases than they do node IDs. Delete a node and there's no way for a mere mortal to create a new node with the same ID again. Mess up an alias, and the regular (content editor) user can put it back right again without having to phone a developer.

-3

You would have to do it in this way:

$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/2');

Create this variable in the template file and use this path-alias variable where ever you want the alias of the node.

Source - https://www.drupal.org/node/1853148

  • Is there any solution in template file? – Yusef Mohamadi Jun 30 '16 at 7:39
  • 2
    you can't use php on Twig =( – No Sssweat Jun 30 '16 at 7:39
  • 4
    I want a solution in twig file – Yusef Mohamadi Jun 30 '16 at 7:40

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.