diff options
Diffstat (limited to 'core/modules/media/src')
-rw-r--r-- | core/modules/media/src/Hook/MediaRequirementsHooks.php | 99 | ||||
-rw-r--r-- | core/modules/media/src/Install/Requirements/MediaRequirements.php | 40 | ||||
-rw-r--r-- | core/modules/media/src/MediaSourceBase.php | 3 |
3 files changed, 141 insertions, 1 deletions
diff --git a/core/modules/media/src/Hook/MediaRequirementsHooks.php b/core/modules/media/src/Hook/MediaRequirementsHooks.php new file mode 100644 index 000000000000..f431134b6f4b --- /dev/null +++ b/core/modules/media/src/Hook/MediaRequirementsHooks.php @@ -0,0 +1,99 @@ +<?php + +namespace Drupal\media\Hook; + +use Drupal\Core\Entity\EntityDisplayRepositoryInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Extension\Requirement\RequirementSeverity; +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Url; +use Drupal\image\Plugin\Field\FieldType\ImageItem; +use Drupal\media\Entity\MediaType; + +/** + * Requirements checks for Media module. + */ +class MediaRequirementsHooks { + + use StringTranslationTrait; + + public function __construct( + protected readonly AccountInterface $currentUser, + protected readonly ModuleHandlerInterface $moduleHandler, + protected readonly EntityDisplayRepositoryInterface $entityDisplayRepository, + ) {} + + /** + * Implements hook_runtime_requirements(). + */ + #[Hook('runtime_requirements')] + public function runtime(): array { + $requirements = []; + foreach (MediaType::loadMultiple() as $type) { + // Load the default display. + $display = $this->entityDisplayRepository->getViewDisplay('media', $type->id()); + + // Check for missing source field definition. + $source_field_definition = $type->getSource()->getSourceFieldDefinition($type); + if (empty($source_field_definition)) { + $requirements['media_missing_source_field_' . $type->id()] = [ + 'title' => $this->t('Media'), + 'description' => $this->t('The source field definition for the %type media type is missing.', + [ + '%type' => $type->label(), + ] + ), + 'severity' => RequirementSeverity::Error, + ]; + continue; + } + + // When a new media type with an image source is created we're + // configuring the default entity view display using the 'large' image + // style. Unfortunately, if a site builder has deleted the 'large' image + // style, we need some other image style to use, but at this point, we + // can't really know the site builder's intentions. So rather than do + // something surprising, we're leaving the embedded media without an + // image style and adding a warning that the site builder might want to + // add an image style. + // @see Drupal\media\Plugin\media\Source\Image::prepareViewDisplay + if (!is_a($source_field_definition->getItemDefinition()->getClass(), ImageItem::class, TRUE)) { + continue; + } + + $component = $display->getComponent($source_field_definition->getName()); + if (empty($component) || $component['type'] !== 'image' || !empty($component['settings']['image_style'])) { + continue; + } + + $action_item = ''; + if ($this->moduleHandler->moduleExists('field_ui') && $this->currentUser->hasPermission('administer media display')) { + $url = Url::fromRoute('entity.entity_view_display.media.default', [ + 'media_type' => $type->id(), + ])->toString(); + $action_item = new TranslatableMarkup('If you would like to change this, <a href=":display">add an image style to the %field_name field</a>.', + [ + '%field_name' => $source_field_definition->label(), + ':display' => $url, + ]); + } + $requirements['media_default_image_style_' . $type->id()] = [ + 'title' => $this->t('Media'), + 'description' => new TranslatableMarkup('The default display for the %type media type is not currently using an image style on the %field_name field. Not using an image style can lead to much larger file downloads. @action_item', + [ + '%field_name' => $source_field_definition->label(), + '@action_item' => $action_item, + '%type' => $type->label(), + ] + ), + 'severity' => RequirementSeverity::Warning, + ]; + } + + return $requirements; + } + +} diff --git a/core/modules/media/src/Install/Requirements/MediaRequirements.php b/core/modules/media/src/Install/Requirements/MediaRequirements.php new file mode 100644 index 000000000000..9fa100ab974d --- /dev/null +++ b/core/modules/media/src/Install/Requirements/MediaRequirements.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\media\Install\Requirements; + +use Drupal\Core\Extension\InstallRequirementsInterface; +use Drupal\Core\Extension\Requirement\RequirementSeverity; +use Drupal\Core\File\FileSystemInterface; + +/** + * Install time requirements for the media module. + */ +class MediaRequirements implements InstallRequirementsInterface { + + /** + * {@inheritdoc} + */ + public static function getRequirements(): array { + $requirements = []; + $destination = 'public://media-icons/generic'; + \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS); + $is_writable = is_writable($destination); + $is_directory = is_dir($destination); + if (!$is_writable || !$is_directory) { + if (!$is_directory) { + $error = t('The directory %directory does not exist.', ['%directory' => $destination]); + } + else { + $error = t('The directory %directory is not writable.', ['%directory' => $destination]); + } + $description = t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']); + $description = $error . ' ' . $description; + $requirements['media']['description'] = $description; + $requirements['media']['severity'] = RequirementSeverity::Error; + } + return $requirements; + } + +} diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php index f7197963cca8..181ebed86253 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -321,7 +321,8 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac if ($tries) { $id .= '_' . $tries; - // Ensure the suffixed field name does not exceed the maximum allowed length. + // Ensure the suffixed field name does not exceed the maximum allowed + // length. if (strlen($id) > EntityTypeInterface::ID_MAX_LENGTH) { $id = substr($base_id, 0, (EntityTypeInterface::ID_MAX_LENGTH - strlen('_' . $tries))) . '_' . $tries; } |