function MediaEmbed::renderMedia

Same name and namespace in other branches
  1. 9 core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()
  2. 8.9.x core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::renderMedia()
  3. 10 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 215

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) {
    // Due to render caching and delayed calls, filtering happens later
    // in the rendering process through a '#pre_render' callback, so we
    // need to generate a counter for the media entity that is being embedded.
    // @see \Drupal\filter\Element\ProcessedText::preRenderText()
    $recursive_render_id = $media->uuid();
    if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
        static::$recursiveRenderDepth[$recursive_render_id]++;
    }
    else {
        static::$recursiveRenderDepth[$recursive_render_id] = 1;
    }
    // Protect ourselves from recursive rendering: return an empty render array.
    if (static::$recursiveRenderDepth[$recursive_render_id] > EntityReferenceEntityFormatter::RECURSIVE_RENDER_LIMIT) {
        $this->loggerFactory
            ->get('media')
            ->error('During rendering of embedded media: recursive rendering detected for %entity_id. Aborting rendering.', [
            '%entity_id' => $media->id(),
        ]);
        return [];
    }
    $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.