我在(父项)段落中有一个实体引用字段,该字段引用了多个子段落。
是否可以访问子级(被引用段落)树枝模板中引用段落的字段值?
实际上,我只是在计算引用项目的树枝模板之一本身中的引用项目总数。因此,如果您愿意,我想将其兄弟姐妹数+1。
我知道我可以在模块中对此进行预处理,但是我想知道是否可以在树枝中进行此处理。
答案 0 :(得分:0)
由于没有对此问题的回答,我不得不假设这在Twig中是不可能的,但是我想通过模块快速分享上述解决方法...
getParentEntity()
是你的朋友。
使引用的元素计数可用的简短示例...
/* implements hook_preprocess_paragraph() (paragraph type product_teaser) */
function mymodule_preprocess_paragraph__product_teaser(&$variables) {
/* makes paragraph siblings count (+ 1/self) available to template */
$siblings_total = 1;
$paragraph = $variables['paragraph'];
$parent_paragraph = $paragraph->getParentEntity();
if ( ( isset($parent_paragraph) ) && ( $parent_paragraph->hasField('field_paragraph_reference') ) ) {
$field_content = $parent_paragraph->get('field_paragraph_reference')->getValue();
if ( isset($field_content[0]['target_id']) ) {
$siblings_total = count($field_content);
}
}
$variables['mymodule_theming_sources']['siblings_total'] = $siblings_total;
}
答案 1 :(得分:0)
在树枝上
{% set paragraph_parent = paragraph.getParentEntity() %}
{% set width = paragraph_parent.field_width.0.value %}
<div class="{{ width }}">{{ content.field_images }}</div>