You are here

function media_theme_suggestions_media in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/media.module \media_theme_suggestions_media()
  2. 9 core/modules/media/media.module \media_theme_suggestions_media()

Implements hook_theme_suggestions_HOOK().

File

core/modules/media/media.module, line 109
Provides media items.

Code

function media_theme_suggestions_media(array $variables) {
  $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;
}