function MediaEmbed::renderMedia

Same name and namespace in other branches
  1. 10 core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()
  2. 9 core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()
  3. 8.9.x core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()
  4. main core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()

Builds the render array for the given media entity in the given langcode.

Parameters

\Drupal\media\MediaInterface $media: A media entity to render.

string $view_mode: The view mode to render it in.

string $langcode: Language code in which the media entity should be rendered.

Return value

array A render array.

1 call to MediaEmbed::renderMedia()
MediaEmbed::process in core/modules/media/src/Plugin/Filter/MediaEmbed.php
Performs the filter processing.

File

core/modules/media/src/Plugin/Filter/MediaEmbed.php, line 183

Class

MediaEmbed
Provides a filter to embed media items using a custom tag.

Namespace

Drupal\media\Plugin\Filter

Code

protected function renderMedia(MediaInterface $media, $view_mode, $langcode) {
  $build = $this->entityTypeManager
    ->getViewBuilder('media')
    ->view($media, $view_mode, $langcode);
  // Allows other modules to treat embedded media items differently.
  $build['#embed'] = TRUE;
  // There are a few concerns when rendering an embedded media entity:
  // - entity access checking happens not during rendering but during routing,
  //   and therefore we have to do it explicitly here for the embedded entity.
  $build['#access'] = $media->access('view', NULL, TRUE);
  // - caching an embedded media entity separately is unnecessary; the host
  //   entity is already render cached.
  unset($build['#cache']['keys']);
  // - Contextual Links do not make sense for embedded entities; we only allow
  //   the host entity to be contextually managed.
  $build['#pre_render'][] = static::class . '::disableContextualLinks';
  // - default styling may break captioned media embeds; attach asset library
  //   to ensure captions behave as intended. Do not set this at the root
  //   level of the render array, otherwise it will be attached always,
  //   instead of only when #access allows this media to be viewed and hence
  //   only when media is actually rendered.
  $build[':media_embed']['#attached']['library'][] = 'media/filter.caption';
  return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.