Score:0

Can't get real field value from paragraph

in flag

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?

Score:1
cn flag

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;
Score:0
nz flag

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>
in flag
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)?
Pipo Bizelli avatar
nz flag
They are displayed on the same page?
in flag
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.
in flag
I updated question, maybe it's more clear now what I'm trying to accomplish.
Pipo Bizelli avatar
nz flag
Ok, edited the awser too, let me know if is that you wanna do.
in flag
In this way, when I get check value in Twig, it will always be 1, and I need to add H1 in field_title template if checkbox is checked in that exactly paragraph. So, it will be always be in H1 no matter if checkbox is on or off, since check will always give 1.
Pipo Bizelli avatar
nz flag
But it can be more than one?
Pipo Bizelli avatar
nz flag
Ok, edited again, check out.
in flag
Yes, this paragraph type is used more than ones on one page. With this update result is the same (always 1), but the type is array, nothing else.
Pipo Bizelli avatar
nz flag
How do you print your twig template? I have added an exemple in the response above.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.