class MediaRequirementsHooks
Requirements checks for Media module.
Hierarchy
- class \Drupal\media\Hook\MediaRequirementsHooks uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of MediaRequirementsHooks
File
-
core/
modules/ media/ src/ Hook/ MediaRequirementsHooks.php, line 19
Namespace
Drupal\media\HookView source
class MediaRequirementsHooks {
use StringTranslationTrait;
public function __construct(AccountInterface $currentUser, ModuleHandlerInterface $moduleHandler, EntityDisplayRepositoryInterface $entityDisplayRepository) {
}
/**
* Implements 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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
MediaRequirementsHooks::runtime | public | function | Implements hook_runtime_requirements(). | |
MediaRequirementsHooks::__construct | public | function | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.