| name | drupal-paragraphs |
| description | Drupal Paragraphs module — accessing paragraph entities, rendering, altering paragraph forms, and patterns for custom paragraph behavior. |
Drupal Paragraphs
Accessing Paragraphs from a Node
use Drupal\paragraphs\Entity\Paragraph;
$items = $node->get('field_content')->referencedEntities();
foreach ($items as $paragraph) {
$bundle = $paragraph->bundle();
$value = $paragraph->get('field_text')->value;
}
$paragraph = $node->get('field_content')->first()?->entity;
Creating Paragraphs Programmatically
$paragraph = Paragraph::create([
'type' => 'text_block',
'field_title' => 'My Title',
'field_body' => [
'value' => 'Body content',
'format' => 'basic_html',
],
]);
$paragraph->save();
$node->get('field_content')->appendItem([
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
]);
$node->save();
Altering Paragraph Forms
#[Hook('field_widget_single_element_paragraphs_form_alter')]
public function fieldWidgetParagraphsFormAlter(array &$element, FormStateInterface $form_state, array $context): void {
$paragraph = $element['#paragraph'];
if ($paragraph->bundle() === 'text_block') {
$element['subform']['field_title']['#access'] = FALSE;
}
}
Rendering Paragraphs
$view_builder = $this->entityTypeManager->getViewBuilder('paragraph');
$build = $view_builder->view($paragraph, 'default');
Paragraph in Twig
{# Render paragraph field #}
{{ content.field_content }}
{# Iterate paragraphs #}
{% for item in content.field_content['#items'] %}
{{ item.entity.field_text.value }}
{% endfor %}
Common Bundle Check Pattern
foreach ($node->get('field_content')->referencedEntities() as $paragraph) {
switch ($paragraph->bundle()) {
case 'text_block':
break;
case 'image_block':
break;
}
}