diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index 0d897d1821573dbad3b85ff8ca888db9c6ccb812..5e76e8752801290900b0e6c1424aee8116d21ae7 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -25777,12 +25777,6 @@ 'count' => 1, 'path' => __DIR__ . '/modules/media/media.module', ]; -$ignoreErrors[] = [ - 'message' => '#^Function media_theme_suggestions_media\\(\\) has no return type specified\\.$#', - 'identifier' => 'missingType.return', - 'count' => 1, - 'path' => __DIR__ . '/modules/media/media.module', -]; $ignoreErrors[] = [ 'message' => '#^Method Drupal\\\\media\\\\Controller\\\\MediaFilterController\\:\\:checkCsrf\\(\\) has no return type specified\\.$#', 'identifier' => 'missingType.return', diff --git a/core/modules/media/media.install b/core/modules/media/media.install index 4cda7d35939104d914a911f9db713fa7df1fa819..db6e3563ad116f2914854fabe6ed5e6e3357d984 100644 --- a/core/modules/media/media.install +++ b/core/modules/media/media.install @@ -8,48 +8,13 @@ use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\FileExists; use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\Hook\Attribute\StopProceduralHookScan; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Url; use Drupal\image\Plugin\Field\FieldType\ImageItem; use Drupal\media\Entity\MediaType; use Drupal\user\RoleInterface; -/** - * Implements hook_install(). - */ -function media_install(): void { - $source = \Drupal::service('extension.list.module')->getPath('media') . '/images/icons'; - $destination = \Drupal::config('media.settings')->get('icon_base_uri'); - /** @var \Drupal\Core\File\FileSystemInterface $file_system */ - $file_system = \Drupal::service('file_system'); - $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); - - $files = $file_system->scanDirectory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); - foreach ($files as $file) { - // When reinstalling the media module we don't want to copy the icons when - // they already exist. The icons could be replaced (by a contrib module or - // manually), so we don't want to replace the existing files. Removing the - // files when we uninstall could also be a problem if the files are - // referenced somewhere else. Since showing an error that it was not - // possible to copy the files is also confusing, we silently do nothing. - if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) { - try { - $file_system->copy($file->uri, $destination, FileExists::Error); - } - catch (FileException) { - // Ignore and continue. - } - - } - } - - // Grant the "view media" permission to all users by default. - if (\Drupal::moduleHandler()->moduleExists('user')) { - user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']); - user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']); - } -} - /** * Implements hook_requirements(). */ @@ -144,6 +109,43 @@ function media_requirements($phase) { return $requirements; } +/** + * Implements hook_install(). + */ +#[StopProceduralHookScan] +function media_install(): void { + $source = \Drupal::service('extension.list.module')->getPath('media') . '/images/icons'; + $destination = \Drupal::config('media.settings')->get('icon_base_uri'); + /** @var \Drupal\Core\File\FileSystemInterface $file_system */ + $file_system = \Drupal::service('file_system'); + $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); + + $files = $file_system->scanDirectory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); + foreach ($files as $file) { + // When reinstalling the media module we don't want to copy the icons when + // they already exist. The icons could be replaced (by a contrib module or + // manually), so we don't want to replace the existing files. Removing the + // files when we uninstall could also be a problem if the files are + // referenced somewhere else. Since showing an error that it was not + // possible to copy the files is also confusing, we silently do nothing. + if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) { + try { + $file_system->copy($file->uri, $destination, FileExists::Error); + } + catch (FileException) { + // Ignore and continue. + } + + } + } + + // Grant the "view media" permission to all users by default. + if (\Drupal::moduleHandler()->moduleExists('user')) { + user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']); + user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']); + } +} + /** * Implements hook_update_last_removed(). */ diff --git a/core/modules/media/media.module b/core/modules/media/media.module index b23be0e5f5c4f695ea784f780cb707a73714f9a6..5e14e6867d115d59bf27835b68f3773e27e0eacb 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -4,54 +4,12 @@ * @file */ -use Drupal\Component\Plugin\DerivativeInspectionInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Hook\Attribute\StopProceduralHookScan; use Drupal\Core\Render\Element; use Drupal\Core\Render\Element\RenderElementBase; use Drupal\Core\Template\Attribute; use Drupal\Core\Url; -use Drupal\media\Plugin\media\Source\OEmbedInterface; - -/** - * 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; -} /** * Prepares variables for media templates. @@ -65,6 +23,7 @@ function media_theme_suggestions_media(array $variables) { * - name: The label for the media item. * - view_mode: View mode; e.g., 'full', 'teaser', etc. */ +#[StopProceduralHookScan] function template_preprocess_media(array &$variables): void { $variables['media'] = $variables['elements']['#media']; $variables['view_mode'] = $variables['elements']['#view_mode']; diff --git a/core/modules/media/src/Hook/MediaThemeSuggestionsHooks.php b/core/modules/media/src/Hook/MediaThemeSuggestionsHooks.php new file mode 100644 index 0000000000000000000000000000000000000000..8bdfc5824c38a68e094fd3f917e9c3ffd2b6e956 --- /dev/null +++ b/core/modules/media/src/Hook/MediaThemeSuggestionsHooks.php @@ -0,0 +1,56 @@ +<?php + +namespace Drupal\media\Hook; + +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Component\Plugin\DerivativeInspectionInterface; +use Drupal\media\Plugin\media\Source\OEmbedInterface; + +/** + * Theme suggestions for media. + */ +class MediaThemeSuggestionsHooks { + + /** + * Implements hook_theme_suggestions_HOOK(). + */ + #[Hook('theme_suggestions_media')] + 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; + } + +} diff --git a/core/modules/media_library/media_library.module b/core/modules/media_library/media_library.module index 0e12f0311178c96ccce7aecd301f8750f9e9a989..11c359007c9fe113d65cfe4685ce8afebd877f25 100644 --- a/core/modules/media_library/media_library.module +++ b/core/modules/media_library/media_library.module @@ -85,29 +85,6 @@ function media_library_preprocess_views_view_fields(&$variables): void { } } -/** - * Alter the bulk form to add a more accessible label. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @todo Remove in https://www.drupal.org/node/2983454 - */ -function media_library_form_views_form_media_library_page_alter(array &$form, FormStateInterface $form_state): void { - if (isset($form['media_bulk_form']) && isset($form['output'])) { - /** @var \Drupal\views\ViewExecutable $view */ - $view = $form['output'][0]['#view']; - foreach (Element::getVisibleChildren($form['media_bulk_form']) as $key) { - if (isset($view->result[$key])) { - $media = $view->field['media_bulk_form']->getEntity($view->result[$key]); - $form['media_bulk_form'][$key]['#title'] = $media ? t('Select @label', ['@label' => $media->label()]) : ''; - } - } - } -} - /** * Form #after_build callback for media_library view's exposed filters form. */ diff --git a/core/modules/media_library/media_library.services.yml b/core/modules/media_library/media_library.services.yml index 9a0253f15798a664b5d1778c91f3cd8a1b2c76b4..510769de9d6d526d70b937e94ca9aa714a52d541 100644 --- a/core/modules/media_library/media_library.services.yml +++ b/core/modules/media_library/media_library.services.yml @@ -1,3 +1,6 @@ +parameters: + media_library.hooks_converted: true + services: _defaults: autoconfigure: true diff --git a/core/modules/media_library/src/Hook/MediaLibraryHooks.php b/core/modules/media_library/src/Hook/MediaLibraryHooks.php index 0bf9fdf9a2a57a37a082a6ae66152f97e3659017..62ca5efffca84d658b0157ed32b87605c3bb1215 100644 --- a/core/modules/media_library/src/Hook/MediaLibraryHooks.php +++ b/core/modules/media_library/src/Hook/MediaLibraryHooks.php @@ -18,6 +18,7 @@ use Drupal\Core\Url; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\Render\Element; /** * Hook implementations for media_library. @@ -207,6 +208,27 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id } } + /** + * Implements hook_form_FORM_ID_alter(). + * + * Alter the bulk form to add a more accessible label. + * + * @todo Remove in https://www.drupal.org/node/2983454 + */ + #[Hook('form_views_form_media_library_page_alter')] + public function formViewsFormMediaLibraryPageAlter(array &$form, FormStateInterface $form_state, $form_id) : void { + if (isset($form['media_bulk_form']) && isset($form['output'])) { + /** @var \Drupal\views\ViewExecutable $view */ + $view = $form['output'][0]['#view']; + foreach (Element::getVisibleChildren($form['media_bulk_form']) as $key) { + if (isset($view->result[$key])) { + $media = $view->field['media_bulk_form']->getEntity($view->result[$key]); + $form['media_bulk_form'][$key]['#title'] = $media ? t('Select @label', ['@label' => $media->label()]) : ''; + } + } + } + } + /** * Implements hook_field_ui_preconfigured_options_alter(). */