- 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,
+ ]);
+ }
+