Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / paragraphs / src / ParagraphViewBuilder.php
index a485c26a0e888c35dc6dbdcc61f28b5f0e42a80e..1ebc521bde34b66170375b3efb10c9e0bec8c55b 100644 (file)
@@ -4,6 +4,7 @@ namespace Drupal\paragraphs;
 
 use Drupal\Core\Entity\Entity\EntityViewDisplay;
 use Drupal\Core\Entity\EntityViewBuilder;
+use Drupal\Core\Render\Element;
 
 /**
  * Render controller for paragraphs.
@@ -17,11 +18,24 @@ class ParagraphViewBuilder extends EntityViewBuilder {
     $build_list = parent::buildMultiple($build_list);
 
     // Allow enabled behavior plugin to alter the rendering.
-    foreach ($build_list as $key => $value) {
-      $display = EntityViewDisplay::load('paragraph.' . $value['#paragraph']->bundle() . '.' . $value['#view_mode']) ?: EntityViewDisplay::load('paragraph.' . $value['#paragraph']->bundle() . '.default');
-      $paragraph_type = $value['#paragraph']->getParagraphType();
+    foreach (Element::children($build_list) as $key) {
+      $build = $build_list[$key];
+      $display = EntityViewDisplay::load('paragraph.' . $build['#paragraph']->bundle() . '.' . $build['#view_mode']) ?: EntityViewDisplay::load('paragraph.' . $build['#paragraph']->bundle() . '.default');
+      $paragraph_type = $build['#paragraph']->getParagraphType();
+
+      // In case we use paragraphs type with no fields the EntityViewDisplay
+      // might not be available yet.
+      if (!$display) {
+        $display = EntityViewDisplay::create([
+          'targetEntityType' => 'paragraph',
+          'bundle' => $build['#paragraph']->bundle(),
+          'mode' => 'default',
+          'status' => TRUE,
+        ]);
+      }
+
       foreach ($paragraph_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin_value) {
-        $plugin_value->view($build_list[$key], $value['#paragraph'], $display, $value['#view_mode']);
+        $plugin_value->view($build_list[$key], $build['#paragraph'], $display, $build['#view_mode']);
       }
     }
     return $build_list;