To have a variable in a field template you need a field preprocess hook:
use Drupal\paragraphs\ParagraphInterface; function mysubtheme_preprocess_field(&$variables) { $paragraph = $variables['element']['#object']; if ($paragraph instanceof ParagraphInterface) { if ($paragraph->getType() === 'sidebar_button_repeater') { if (!$paragraph->field_background_color->isEmpty()) { $variables['color'] = $paragraph->get('field_background_color')->value; } } } } Nested Paragraphs
If the field is in the parent paragraph of a nested paragraph:
$parent = $paragraph->getParentEntity(); And get the field value from $parent:
function mysubtheme_preprocess_field(&$variables) { $paragraph = $variables['element']['#object']; if ($paragraph instanceof ParagraphInterface) { if ($paragraph->getType() === 'child_type') { $parent = $paragraph->getParentEntity(); if ($parent instanceof ParagraphInterface) { if ($parent->getType() === 'parent_type') { if (!$parent->field_background_color->isEmpty()) { $variables['color'] = $parent->get('field_background_color')->value; } } } } } } Why instanceof?
In the commentcomments it was asked why to use instanceof?
In older Drupal versions you would use:
$entity_type = $variables['element']['#entity_type']; $bundle = $variables['element']['#bundle']; if ($entity_type === 'paragraph' && $bundle === 'my_bundle') { // This works but your IDE doesn't understand it: $parent = $paragraph->getParentEntity(); ... } In OOP code:
if ($paragraph instanceof ParagraphInterface) { if ($paragraph->getType() === 'my_bundle') { $parent = $paragraph->getParentEntity(); ... } } Now it's safe to use methods defined in ParagraphInterface. The IDE will help you with autocomplete and by marking unknown methods causing run-time errors.