I tried to play with media_theme_suggestions_media, but it did not work in my case - seems that Drupal is not going into the hook_theme_suggestions_media
for example:

media-oembed-iframe--provider-vimeo.html
media-oembed-iframe--provider-youtube.html

I know I may have something wrong with media__source_ but It's not showing
the suggestion in the twig debug to know the right format

Thanks for all your work on the media module

Opened this issue as a follow up on the following lin
https://www.drupal.org/project/drupal/issues/2998091#comment-13013994
from
#2998091: Remote videos overflow their containing element

/**
 * Implements hook_theme_suggestions_HOOK().
 */
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;
}

Comments

RajabNatshah created an issue. See original summary.

rajab natshah’s picture

Title: Not working source plugin uses oEmbed suggestions with the provider name, if available » Source plugin uses oEmbed suggestions is not working with the provider name, if available
rajab natshah’s picture

Noticed that
when the media bundle is image
then

if (isset($variables["#media"])) {

}

But for oembed types remote_videos for example

if ($variables['theme_hook_original'] == 'media_oembed_iframe') && isset($variables["media"])) {

}
rajab natshah’s picture

Category: Bug report » Feature request
phenaproxima’s picture

Issue tags: +oembed
phenaproxima’s picture

Issue tags: +Needs tests

Hmm...I don't see how this, in general, could be happening unless you are theming oEmbed resources which (somehow) have no provider associated with them. Even so, this could use some test coverage; it is a blind spot.

rajab natshah’s picture

Thanks Adam for following
Please have a look at
https://www.drupal.org/project/drupal/issues/3044967#comment-13419910

You could find a list of issues, which needed to do this in number of cases
It's nice to have, not must have by core.
I know that the Media Team knows more and better on the subject
following with your direction.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.