class MediaThemeSuggestionsHooks

Theme suggestions for media.

Hierarchy

Expanded class hierarchy of MediaThemeSuggestionsHooks

File

core/modules/media/src/Hook/MediaThemeSuggestionsHooks.php, line 12

Namespace

Drupal\media\Hook
View source
class MediaThemeSuggestionsHooks {
    
    /**
     * Implements hook_theme_suggestions_HOOK().
     */
    public function themeSuggestionsMedia(array $variables) : array {
        $suggestions = [];
        
        /** @var \Drupal\media\MediaInterface $media */
        $media = $variables['elements']['#media'];
        $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
        $suggestions[] = 'media__' . $sanitized_view_mode;
        $suggestions[] = 'media__' . $media->bundle();
        $suggestions[] = 'media__' . $media->bundle() . '__' . $sanitized_view_mode;
        // Add suggestions based on the source plugin ID.
        $source = $media->getSource();
        if ($source instanceof DerivativeInspectionInterface) {
            $source_id = $source->getBaseId();
            $derivative_id = $source->getDerivativeId();
            if ($derivative_id) {
                $source_id .= '__derivative_' . $derivative_id;
            }
        }
        else {
            $source_id = $source->getPluginId();
        }
        $suggestions[] = "media__source_{$source_id}";
        // If the source plugin uses oEmbed, add a suggestion based on the provider
        // name, if available.
        if ($source instanceof OEmbedInterface) {
            $provider_id = $source->getMetadata($media, 'provider_name');
            if ($provider_id) {
                $provider_id = \Drupal::transliteration()->transliterate($provider_id);
                $provider_id = preg_replace('/[^a-z0-9_]+/', '_', mb_strtolower($provider_id));
                $suggestions[] = end($suggestions) . "__provider_{$provider_id}";
            }
        }
        return $suggestions;
    }

}

Members

Title Sort descending Modifiers Object type Summary
MediaThemeSuggestionsHooks::themeSuggestionsMedia public function Implements hook_theme_suggestions_HOOK().

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