0

I want to inject Font Awesome icons to user menu based on the route/link of each item.

But it looks like there is no way to get the path grom the url object. I tried everything I could think of but nothing worked. It's like the url object won't give any data in Twig.

This is what I am trying to do in menu template for user acocunt:

<li{{ item.attributes.addClass(classes) }}>
                    {% set uri = path(item.url.routeName) %}
                    <pre>{{ dump(item) }}</pre>
                    <pre>{{ dump(uri) }}</pre>
                    {% if uri == 'user' %}
                        <i class="fa fa-user"></i>
                    {% elseif uri == 'user/logout' %}
                        <i class="fa fa-sign-out"></i>
                    {% elseif uri == 'user/register' %}
                        <i class="fa fa-user-plus"></i>
                    {% elseif uri == 'user/login' %}
                        <i class="fa fa-sign-in"></i>
                    {% endif %}
                    {{ link(item.title, item.url) }}
                    {% if item.below %}
                        {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
                    {% endif %}
                </li>
2

OK, so it looks like there is nothing for this in core so I had to create my own twig function.

<?php

/**
 * @file
 * Contains \Drupal\mymodule\TwigExtension\MymoduleTwigExtension.
 */

namespace Drupal\mymodule\TwigExtension;

use Drupal\Core\Template\TwigExtension;
use Twig_SimpleFunction;
use Drupal\Core\Url;

/**
 * My awesome twig extension.
 */
class MymoduleTwigExtension extends TwigExtension {

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new Twig_SimpleFunction('url_path', array($this, 'getPathFromUrl'))
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'mymodule_core';
  }

  /**
   * Gets a path from Url object.
   *
   * @param \Drupal\Core\Url $url
   *   The URL object to retrieve the path from.
   *
   * @return string
   *   Path to where the Url object is poiting to.
   */
  public function getPathFromUrl(Url $url) {
    return $url->toString();
  }

}

And this is the twig code:

<li{{ item.attributes.addClass(classes) }}>
                    {% set uri = url_path(item.url) %}
                    {% set item_title = {'#markup': item.title} %}
                    {% if uri == '/user' %}
                        {% set item_title = item_title|merge({'#prefix': '<i class="fa fa-user"></i>'}) %}
                    {% elseif uri == '/user/logout' %}
                        {% set item_title = item_title|merge({'#prefix': '<i class="fa fa-sign-out"></i>'}) %}
                    {% elseif uri == '/user/register' %}
                        {% set item_title = item_title|merge({'#prefix': '<i class="fa fa-user-plus"></i>'}) %}
                    {% elseif uri == '/user/login' %}
                        {% set item_title = item_title|merge({'#prefix': '<i class="fa fa-sign-in"></i>'}) %}
                    {% endif %}
                    {{ link(item_title, item.url) }}
                    {% if item.below %}
                        {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
                    {% endif %}
                </li>

Edit: I will change this to route names since using paths is not a good idea in d8.

2

{% set uri = item.url.toString %} will also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy