You are here

public function ParagraphViewBuilder::buildMultiple in Paragraphs 8

Builds multiple entities' views; augments entity defaults.

This function is assigned as a #pre_render callback in ::viewMultiple().

By delaying the building of an entity until the #pre_render processing in drupal_render(), the processing cost of assembling an entity's renderable array is saved on cache-hit requests.

Parameters

array $build_list: A renderable array containing build information and context for an entity view.

Return value

array The updated renderable array.

Overrides EntityViewBuilder::buildMultiple

See also

\Drupal\Core\Render\RendererInterface::render()

File

src/ParagraphViewBuilder.php, line 17

Class

ParagraphViewBuilder
Render controller for paragraphs.

Namespace

Drupal\paragraphs

Code

public function buildMultiple(array $build_list) {
  $build_list = parent::buildMultiple($build_list);

  // Allow enabled behavior plugin to alter the rendering.
  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_value) {
      $plugin_value
        ->view($build_list[$key], $build['#paragraph'], $display, $build['#view_mode']);
    }
    $build_list[$key]['#attached']['library'][] = 'paragraphs/drupal.paragraphs.unpublished';
  }
  return $build_list;
}