Skip to main content
added 2 characters in body
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221

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.

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 comment 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.

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 comments 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.

added 105 characters in body
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221

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 comment 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.

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 comment 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') { ... } 

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.

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 comment 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.

why instanceof
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221

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 comment 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') { ... } 

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.

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; } } } } 

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; } } } } } } 

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 comment 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') { ... } 

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.

add complete code example for parent
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221
Loading
parent paragraph
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221
Loading
Source Link
4uk4
  • 102.9k
  • 7
  • 176
  • 221
Loading