UPDATED ON 11/06/2014: Better naming used for Twig functions

Since we got some feedback asking us how to generate routes to nodes by internal name, we decided to extend the NodeTwigExtension with some extra functions that allow you to easily do just that :

  • get_node_by_internal_name will fetch the node with the specified internal name and locale
  • get_path_by_internal_name will return the path for the node with the specified internal name and locale (similar to the Twig path function)
  • get_url_by_internal_name will return the url for the node with the specified internal name and locale (similar to the Twig url function)

Usage is quite simple :

<a href="{{ get_path_by_internal_name('internal_name', app.request.locale) }}">Path</a>
<a href="{{ get_path_by_internal_name('internal_name', app.request.locale, {}, true) }}">Relative path</a>
<a href="{{ get_url_by_internal_name('internal_name', app.request.locale) }}">Full url</a>
<a href="{{ get_url_by_internal_name('internal_name', app.request.locale, {}, true) }}">Schemaless link</a>

If you also need extra info from the node - you could use :

{% set node = get_node_by_internal_name('internal_name', app.request.locale) %}
{% if node is not empty %}
    {% set nodeTrans = node.getNodeTranslation(app.request.locale) %}
    <a href="{{ path('_slug', { 'url': nodeTrans.slug, '_locale': app.request.locale }) }}">Path</a>
    <a href="{{ url('_slug', { 'url': nodeTrans.slug, '_locale': app.request.locale }) }}">Url</a>
{% endif %}

If you want, you can pass extra url parameters by supplying them as 3rd parameter to get_path_by_internal_name or get_url_by_internal_name.

We hope these Twig functions will prove to be helpful!