class MediaLibrary

Same name in other branches
  1. 10 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/MediaLibrary.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\MediaLibrary
  2. 11.x core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/MediaLibrary.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\MediaLibrary

CKEditor 5 Media Library plugin.

Provides media library support and options for the CKEditor 5 build.

@internal Plugin classes are internal.

Hierarchy

Expanded class hierarchy of MediaLibrary

1 string reference to 'MediaLibrary'
ckeditor5.ckeditor5.yml in core/modules/ckeditor5/ckeditor5.ckeditor5.yml
core/modules/ckeditor5/ckeditor5.ckeditor5.yml

File

core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/MediaLibrary.php, line 25

Namespace

Drupal\ckeditor5\Plugin\CKEditor5Plugin
View source
class MediaLibrary extends CKEditor5PluginDefault implements ContainerFactoryPluginInterface {
    use StringTranslationTrait;
    
    /**
     * The media type entity storage.
     *
     * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
     */
    protected $mediaTypeStorage;
    
    /**
     * MediaLibrary constructor.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     */
    public function __construct(array $configuration, string $plugin_id, CKEditor5PluginDefinition $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->mediaTypeStorage = $entity_type_manager->getStorage('media_type');
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor) : array {
        $media_type_ids = $this->mediaTypeStorage
            ->getQuery()
            ->execute();
        // Making the title for editor drupal media embed translatable.
        $static_plugin_config['drupalMedia']['dialogSettings']['title'] = $this->t('Add or select media');
        if ($editor->hasAssociatedFilterFormat()) {
            $media_embed_filter = $editor->getFilterFormat()
                ->filters()
                ->get('media_embed');
            // Optionally limit the allowed media types based on the MediaEmbed
            // setting. If the setting is empty, do not limit the options.
            if (!empty($media_embed_filter->settings['allowed_media_types'])) {
                $media_type_ids = array_intersect_key($media_type_ids, $media_embed_filter->settings['allowed_media_types']);
            }
        }
        if (in_array('image', $media_type_ids, TRUE)) {
            // Move image to first position.
            // This workaround can be removed once this issue is fixed:
            // @see https://www.drupal.org/project/drupal/issues/3073799
            array_unshift($media_type_ids, 'image');
            $media_type_ids = array_unique($media_type_ids);
        }
        $state = MediaLibraryState::create('media_library.opener.editor', $media_type_ids, reset($media_type_ids), 1, [
            'filter_format_id' => $editor->getFilterFormat()
                ->id(),
        ]);
        $library_url = Url::fromRoute('media_library.ui')->setOption('query', $state->all())
            ->toString(TRUE)
            ->getGeneratedUrl();
        $dynamic_plugin_config = $static_plugin_config;
        $dynamic_plugin_config['drupalMedia']['libraryURL'] = $library_url;
        return $dynamic_plugin_config;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
MediaLibrary::$mediaTypeStorage protected property The media type entity storage.
MediaLibrary::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MediaLibrary::getDynamicPluginConfig public function Allows a plugin to modify its static configuration. Overrides CKEditor5PluginDefault::getDynamicPluginConfig
MediaLibrary::__construct public function MediaLibrary constructor. Overrides CKEditor5PluginDefault::__construct
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.