3

I have an entity reference field inside a (parent-)paragraph, which references multiple child-paragraphs.

Is it possible to access field values of the referencing paragraph in the children's (referenced paragraph's) twig templates?

Actually I'm just trying to count the total referenced items within one of the referenced item's twig templates itself. So I want to count its siblings + 1, if you want.

I'm aware of the fact that I could preprocess this in a module, but I would like to know if this is possible in twig.

c1u31355
  • 420
  • 5
  • 10
  • I think this is not really Twig-related since the existing variables witthin each template are Drupal- or module-specific, aren't they? Also I don't get the question "what I have tried so far", since I'm only asking if there is a variable available (in the templates of the referenced entities), which holds the referencing entities fields or something. – c1u31355 Sep 14 '18 at 10:13
  • `paragraph.field_paragraph_reference.getvalue|length` gives me the quantity within the referencing element - but I have to kind of pass it to be accessable within the referenced paragraphs. I found this question to be kind of related (https://drupal.stackexchange.com/questions/242601/drupal-8-access-a-template-variable-from-another-template-or-theme-hook), but the offered solution doesn't work in any way, please correct me if I'm wrong. – c1u31355 Sep 14 '18 at 10:14

3 Answers3

6

In twig:

{% set paragraph_parent = paragraph.getParentEntity() %}
{% set width = paragraph_parent.field_width.0.value %}

<div class="{{ width }}">{{ content.field_images }}</div>
Stef Van Looveren
  • 302
  • 1
  • 5
  • 15
2

Another method using twig with 2 simple lines. It works fine.

{% set parent = paragraph._referringItem.parent.parent.entity %}

{{ parent.title.value }}
Thirsty Six
  • 399
  • 1
  • 6
  • 13
  • Thanks. This answer helped me figure out testing things like `paragraph._referringItem.parent.name == 'field_my_field_name` (whereas`getParentEntity` goes straight up to the host entity, but in one of my use cases, I needed to know which _field_ in the host entity -- among multiple potential ones -- the paragraph was in). – Max Starkenburg Aug 16 '23 at 20:14
1

Since there is no response to this question I have to assume that this is not possible in Twig, but wanted to quick share the mentioned workaround via module...

getParentEntity()

is your friend.

A short example for making the referenced element's count available...

/* 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;

}
c1u31355
  • 420
  • 5
  • 10
  • You can add this code as a `twig` function or filter to access it inside a template nonetheless – DarkBee Sep 15 '18 at 23:38
  • This function is to make a variable available to the specific paragraph type's twig template. Call via `{{ mymodule_theming_sources.siblings_total }}`. My original question was if the data is already available in Twig. – c1u31355 Sep 16 '18 at 16:59
  • 1
    Instead of hard coding field_paragraph_reference you may use `$parent_field_name = $paragraph->get('parent_field_name')->value;` – LarS Oct 14 '18 at 23:29