You are here

public function ShareMessageViewBuilder::viewMultiple in Share Message 8

Builds the render array for the provided entities.

Parameters

array $entities: An array of entities implementing EntityInterface to view.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

A render array for the entities, indexed by the same keys as the entities array passed in $entities.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view Comments and passing a Node which is not the one the comments belongs to, or not passing one, and having the comments node not be available for loading.

Overrides EntityViewBuilder::viewMultiple

1 call to ShareMessageViewBuilder::viewMultiple()
ShareMessageViewBuilder::view in src/Entity/Handler/ShareMessageViewBuilder.php
Builds the render array for the provided entity.

File

src/Entity/Handler/ShareMessageViewBuilder.php, line 25

Class

ShareMessageViewBuilder
Render controller for nodes.

Namespace

Drupal\sharemessage\Entity\Handler

Code

public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
  if (empty($entities)) {
    return [];
  }
  $build = [];
  foreach ($entities as $entity) {

    /* @var \Drupal\sharemessage\ShareMessageInterface $entity */

    // EntityViewController expects the entity to be in #sharemessage.
    $cacheability_metadata = CacheableMetadata::createFromObject($entity);
    $build[$entity
      ->id()] = [
      '#sharemessage' => $entity,
      '#contextual_links' => [
        'sharemessage' => [
          'route_parameters' => [
            'sharemessage' => $entity
              ->id(),
          ],
        ],
      ],
    ];
    $cacheability_metadata
      ->addCacheableDependency(\Drupal::config('sharemessage.settings'));
    $cacheability_metadata
      ->applyTo($build[$entity
      ->id()]);
    $context = $entity
      ->getContext($view_mode);
    $is_overridden = \Drupal::request()->query
      ->get('smid') && \Drupal::config('sharemessage.settings')
      ->get('message_enforcement');

    // Add OG Tags to the page if there are none added yet and a corresponding
    // view mode was set (or it was altered into such a view mode in getContent()).
    $og_view_modes = [
      'full',
      'only_og_tags',
      'no_attributes',
    ];
    if (!$is_overridden && in_array($context['view_mode'], $og_view_modes)) {
      $build[$entity
        ->id()]['#attached']['html_head'] = $this
        ->mapHeadElements($entity
        ->buildOGTags($context));
    }

    // Add twitter card meta tags if setting is active.
    if (\Drupal::config('sharemessage.settings')
      ->get('add_twitter_card')) {
      $twitter_tags = $entity
        ->buildTwitterCardTags($context);
      $build[$entity
        ->id()]['#attached']['html_head'] = array_merge($build[$entity
        ->id()]['#attached']['html_head'], $twitter_tags);
    }
    if ($entity
      ->hasPlugin() && $view_mode != 'only_og_tags') {
      $attributes_view_modes = [
        'full',
        'attributes_only',
      ];
      $plugin_attributes = in_array($context['view_mode'], $attributes_view_modes);
      $build[$entity
        ->id()][$entity
        ->getPluginID()] = $entity
        ->getPlugin()
        ->build($context, $plugin_attributes);
    }
  }
  return $build;
}