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?