【问题标题】:Access to link #options key on twig template访问树枝模板上的链接 #options 键
【发布时间】:2017-07-12 07:19:28
【问题描述】:

我怎样才能访问树枝模板上数组的#options 键?

我正在尝试检查语言是否为当前语言,然后将其设置为活动。

我的代码如下:

<ul class="headerbar__text--language navbar-right">
{%- for key, item in links -%}
  {%
    set link_class = [
      item.set_active_class ? 'active',
    ]
  %}
  <li{{ item.attributes.addClass(key|clean_class).addClass(link_class) }}>
    {{ dump(item) }}
    {%- if item.link -%}
      {{ item.link }}
    {%- elseif item.text_attributes -%}
      <span{{ item.text_attributes }}>{{ item.text }}</span>
    {%- else -%}
      {{ item.text }}
    {%- endif -%}
  </li>
{%- endfor -%}

但是没有设置类。如您所见,有一个转储,结果如下:

array (size=4)
'link' => 
    array (size=5)
      '#type' => string 'link' (length=4)
      '#title' => string 'German' (length=6)
      '#options' => 
        array (size=5)
          'language' => 
            object(Drupal\language\Entity\ConfigurableLanguage)[1623]
              ...
          'attributes' => 
            array (size=1)
              ...
          'query' => 
            array (size=0)
              ...
          'ajax' => null
          'set_active_class' => boolean true
      '#url' => 
        object(Drupal\Core\Url)[1629]
          protected 'urlGenerator' => 
            object(Drupal\Core\Render\MetadataBubblingUrlGenerator)[227]
              ...
          protected 'urlAssembler' => null
          protected 'accessManager' => null
          protected 'routeName' => string '<front>' (length=7)
          protected 'routeParameters' => 
            array (size=0)
              ...
          protected 'options' => 
            array (size=0)
              ...
          protected 'external' => boolean false
          protected 'unrouted' => boolean false
          protected 'uri' => null
          protected 'internalPath' => string '' (length=0)
          protected '_serviceIds' => 
            array (size=0)
              ...
      '#ajax' => null
  'text' => string 'German' (length=6)
  'text_attributes' => 
    object(Drupal\Core\Template\Attribute)[1646]
      protected 'storage' => 
        array (size=1)
          'class' => 
            object(Drupal\Core\Template\AttributeArray)[1647]
              ...
  'attributes' => 
    object(Drupal\Core\Template\Attribute)[1649]
      protected 'storage' => 
        array (size=3)
          'hreflang' => 
            object(Drupal\Core\Template\AttributeString)[1650]
              ...
          'data-drupal-link-system-path' => 
            object(Drupal\Core\Template\AttributeString)[1651]
              ...
          'class' => 
            object(Drupal\Core\Template\AttributeArray)[1959]
              ...

我尝试使用link['#options']link.optionslink.set_active_class 访问它,但它们都返回NULL。

那么我如何能够访问这些属性?

感谢您的建议!

【问题讨论】:

    标签: symfony drupal twig drupal-8


    【解决方案1】:

    根据转储的结构,这应该可以工作:

    {%  set link_class = item.link['#options'].set_active_class ? 'active' %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 2012-07-27
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2013-08-07
      • 2012-05-02
      相关资源
      最近更新 更多