diff options
author | Lee Rowlands <lee.rowlands@previousnext.com.au> | 2025-01-03 13:42:46 +1000 |
---|---|---|
committer | Lee Rowlands <lee.rowlands@previousnext.com.au> | 2025-01-03 13:42:46 +1000 |
commit | 18a39f8c03631799ac05735c9bc1afaf08a6caa8 (patch) | |
tree | 2c618dff55967e1427b0e0e5a4577b45e137d4fc /core/modules/media | |
parent | 55d9324fa26942fc093e35b1f1295f40852f7a14 (diff) | |
download | drupal-18a39f8c03631799ac05735c9bc1afaf08a6caa8.tar.gz drupal-18a39f8c03631799ac05735c9bc1afaf08a6caa8.zip |
Issue #3495508 by nicxvan: Mark Media and Media Library modules' procedural hooks as converted
Diffstat (limited to 'core/modules/media')
-rw-r--r-- | core/modules/media/media.install | 74 | ||||
-rw-r--r-- | core/modules/media/media.module | 45 | ||||
-rw-r--r-- | core/modules/media/src/Hook/MediaThemeSuggestionsHooks.php | 56 |
3 files changed, 96 insertions, 79 deletions
diff --git a/core/modules/media/media.install b/core/modules/media/media.install index 4cda7d35939..db6e3563ad1 100644 --- a/core/modules/media/media.install +++ b/core/modules/media/media.install @@ -8,6 +8,7 @@ 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; @@ -15,42 +16,6 @@ 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(). */ function media_requirements($phase) { @@ -145,6 +110,43 @@ function media_requirements($phase) { } /** + * 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(). */ function media_update_last_removed(): int { diff --git a/core/modules/media/media.module b/core/modules/media/media.module index b23be0e5f5c..5e14e6867d1 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 00000000000..8bdfc5824c3 --- /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; + } + +} |