7

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 ?

4
  • template file of the about us page or something else?
    – No Sssweat
    Commented Jun 30, 2016 at 7:32
  • @NoSssweat in front page template , it is not current page
    – Yuseferi
    Commented Jun 30, 2016 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
    Commented Jun 30, 2016 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).
    – Yuseferi
    Commented Jun 30, 2016 at 7:36

4 Answers 4

16

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.

0
15

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) }}
4
  • 1
    are you supposed to replace dd in this {'node': dd.nid} Commented Feb 27, 2018 at 0:23
  • 1
    This now works for me. {{ path('entity.taxonomy_term.canonical', {'taxonomy_term': node.field_site_structure.entity.tid.value}) }} Commented Feb 27, 2018 at 0:51
  • 1
    @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.
    – Yuseferi
    Commented Feb 27, 2018 at 6:31
  • Thank you for this. One small correction. node.nid is not a property. Use the node.id() method instead: {{ path('entity.node.canonical', {'node': node.id()}) }}. If you're working with any entity, it will have an id() method. Commented Jul 12, 2023 at 21:40
1

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.

-4

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

3
  • 1
    Is there any solution in template file?
    – Yuseferi
    Commented Jun 30, 2016 at 7:39
  • 3
    you can't use php on Twig =(
    – No Sssweat
    Commented Jun 30, 2016 at 7:39
  • 5
    I want a solution in twig file
    – Yuseferi
    Commented Jun 30, 2016 at 7:40

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.