2016-10-20 6 views
3

これは私の設定です。私はカスタムブロックタイプを持っていて、参照エンティティフィールドを持っています。 「製品」のというコンテンツタイプを参照しています。商品コンテンツタイプには、タクソノミ語の語彙「シリーズ」への参照エンティティフィールドがあります。Drupal 8は、参照されたエンティティのフィールド値を親の子孫で取得します

シリーズタクソノミーには、カスタムブロックの商品フィールドテーマの価値を得るために必要なフィールドが含まれています。私は基本的に5つの製品のブロックを持っています、各製品はシリーズに属しています。私のテーマでは、シリーズのフィールド値を各製品に適用する必要があります。

この値を厳密にtwigから取得する方法はありますか?私はそれに到達しようとするために無数のコンビネーションチェーンを試しました。

製品を取り巻くblock-bundle-product_series_block.html.twigファイルがあります。私は、データシリーズのHTML属性で使用するためのシリーズを取得したいフィールド-products.html.twig -

<div {{ attributes.addClass(classes) }} 
    data-ng-controller="ProductsController as vm"> 
    <div class="container"> 
    {{ title_prefix }} 
    {% if label %} 
     <h2{{ title_attributes }}>{{ label }}</h2> 
    {% endif %} 
    {{ title_suffix }} 
    <div class="product-holder"> 
     {% block content %} 
     {{ content }} 
     {% endblock %} 
    </div> 
    </div> 
</div> 

はその後、それは私のフィールドに行きます。

<div{{ attributes.addClass(classes, 'field__items') }} data-series="{{ cant_figure_this_out }}"> 
    {% for item in items %} 
    <div{{ item.attributes.addClass('field__item item') }}>{{ item.content }}</div> 
    {% endfor %} 
</div> 

答えて

2

1 /あなたはこのような何かをノードテンプレートレベルでそれを得ることができます:

node.field_serie.0.get('entity').getTarget().getValue().getName() 

は、あなたが配列としてそれをしたい場合は...:

{% set series = [] %} 
{% for key, item in node.field_serie %} 
    {% set series = series|merge([item.get('entity').getTarget().getValue().getName()]) %} 
{% endfor %} 

2 /フィールドテンプレートレベルで取得することもできます:

{% set series = [] %} 
{% for key, item in item['content']['#node'].field_serie %} 
    {% set series = series|merge([item.get('entity').getTarget().getValue().getName()]) %} 
{% endfor %} 

3 /そして、あなたは(これは、おそらく多くの作業を必要とする)のようなものを使用することができます。

attributes.setAttribute('data-series', series|join(',')|escape 
+0

ありがとうございました! 2つ目の方法は、私が必要としていたものを助けました。 – SkankHunt42

関連する問題