0

I need to get value from exact paragraph type in paragraph reference field in node, so I can use it in Twig.

Here's my current code:

function my_theme_preprocess_field(array &$variables, $hook) {
  if ($node = \Drupal::request()->attributes->get('node')) {
    if ($node->getType() == 'page'){
      foreach ($node->get('field_sections') as $paragraph) {
        if ($paragraph->entity->getType() == 'targeted_paragraph_type') {
          $targeted_paragraph = $paragraph->entity;
          $variables['check'] = $targeted_paragraph->field_is_main->value;
        }
      }
    }
  }
}

The issue is that I'm always getting 0 for this field (it's boolean), and that's not a real situation I have. I also tried to get value from some other fields in this paragraph, and I'm always getting same values for each item (it's wrong value).

In this way (like code above), the variable check will always have latest value from list of paragraphs in foreach.

How I can store data for each paragraph separately and have it in Twig for exact paragraph?

The idea is to wrap title in H1 if the field_is_main field is checked, so maybe there's some other approach to do this?

2 Answers 2

1

I think the problem starts with how you get the node reference, which you probably shouldn't get from the request.

If this field template preprocess hook is in a paragraph you get it via:

$paragraph = $variables['element']['#object'];

Every paragraph has a parent, which is probably the node you are looking for:

$node = $paragraph->getParentEntity();

You can then get field values from different levels, starting with the paragraph or the node.

The advantage is by following the references of the nested field and entity objects you get the right field deltas on the way up. If the targeted field is a sibling of the template field you can simply use the referenced paragraph to get the field value of the same paragraph field delta:

$value = $paragraph->field_is_main->value;
0

It's because you are overwriting the value of $variables['check']. You need to create an array and then populate it, like this:

function my_theme_preprocess_field(array &$variables, $hook) {
  if ($node = \Drupal::request()->attributes->get('node')) {
    if ($node->getType() == 'page'){
      $checks = [];
      foreach ($node->get('field_sections') as $paragraph) {
        if ($paragraph->entity->getType() == 'targeted_paragraph_type') {
          $targeted_paragraph = $paragraph->entity;
          $checks[] = $targeted_paragraph->field_is_main->value;
        }
      }
      $variables['check'] = $checks;
    }
  }
}

Edit:
But, if you want only one value (the checked one) in this var, you need to verify and push only the last selected, like this:

function my_theme_preprocess_field(array &$variables, $hook) {
  if ($node = \Drupal::request()->attributes->get('node')) {
    if ($node->getType() == 'page'){
      $checks = [];
      foreach ($node->get('field_sections') as $paragraph) {
        if ($paragraph->entity->getType() == 'targeted_paragraph_type') {
          $targeted_paragraph = $paragraph->entity;
          if ($targeted_paragraph->field_is_main->value) {
            $checks[] = $targeted_paragraph->[FIELD_STRING]->value;
          }
        }
      }
      $variables['checks'] = $checks;          
    }
  }
}

Now on your twig template:

<h1>
{%- for title in checks -%}
  {{ title }}
{%- endfor -%}
</h1>
10
  • Thanks for the answer! So, I'm using this variable in title field template of paragraph, if it's true then I print content of title field in one way, if not I print it in different way. How to identify what item from array check is what paragraph (I have multiple paragraphs of this type on page)?
    – mixerowsky
    Commented Apr 24, 2023 at 13:11
  • They are displayed on the same page? Commented Apr 24, 2023 at 13:14
  • Yes, but there's different paragraph types, so I have multiple paragraphs that I'm targeting, and also others between them. Now when I dump check variable in Twig, I have array $checks for each paragraph of this type on page.
    – mixerowsky
    Commented Apr 24, 2023 at 13:18
  • I updated question, maybe it's more clear now what I'm trying to accomplish.
    – mixerowsky
    Commented Apr 24, 2023 at 13:25
  • Ok, edited the awser too, let me know if is that you wanna do. Commented Apr 24, 2023 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.