class Media

Same name in this branch
  1. 10 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media
  2. 10 core/modules/media/src/Plugin/views/wizard/Media.php \Drupal\media\Plugin\views\wizard\Media
Same name in other branches
  1. 9 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media
  2. 9 core/modules/media/src/Plugin/views/wizard/Media.php \Drupal\media\Plugin\views\wizard\Media
  3. 9 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media
  4. 8.9.x core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media
  5. 8.9.x core/modules/media/src/Plugin/views/wizard/Media.php \Drupal\media\Plugin\views\wizard\Media
  6. 11.x core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media
  7. 11.x core/modules/media/src/Plugin/views/wizard/Media.php \Drupal\media\Plugin\views\wizard\Media
  8. 11.x core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media

CKEditor 5 Media plugin.

Provides drupal-media element and options provided by the CKEditor 5 build.

@internal Plugin classes are internal.

Hierarchy

Expanded class hierarchy of Media

302 string references to 'Media'
344b943c-b231-4d73-9669-0b0a2be12aa5.yml in core/tests/fixtures/default_content/media/344b943c-b231-4d73-9669-0b0a2be12aa5.yml
core/tests/fixtures/default_content/media/344b943c-b231-4d73-9669-0b0a2be12aa5.yml
AddFormBase::buildEntityFormElement in core/modules/media_library/src/Form/AddFormBase.php
Builds the sub-form for setting required fields on a new media item.
AddFormBase::getAddedMediaItems in core/modules/media_library/src/Form/AddFormBase.php
Get all added media items from the form state.
AddFormBase::processInputValues in core/modules/media_library/src/Form/AddFormBase.php
Creates media items from source field input values.
AddFormBase::removeButtonSubmit in core/modules/media_library/src/Form/AddFormBase.php
Submit handler for the remove button.

... See full list

File

core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php, line 28

Namespace

Drupal\ckeditor5\Plugin\CKEditor5Plugin
View source
class Media extends CKEditor5PluginDefault implements ContainerFactoryPluginInterface, CKEditor5PluginConfigurableInterface, CKEditor5PluginElementsSubsetInterface {
    use DynamicPluginConfigWithCsrfTokenUrlTrait;
    use CKEditor5PluginConfigurableTrait;
    
    /**
     * The entity display repository.
     *
     * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
     */
    protected $entityDisplayRepository;
    
    /**
     * Media 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\EntityDisplayRepositoryInterface $entity_display_repository
     *   The entity display repository.
     */
    public function __construct(array $configuration, string $plugin_id, CKEditor5PluginDefinition $plugin_definition, EntityDisplayRepositoryInterface $entity_display_repository) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->entityDisplayRepository = $entity_display_repository;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_display.repository'));
    }
    
    /**
     * Configures allowed view modes.
     *
     * @param \Drupal\editor\EditorInterface $editor
     *   A configured text editor object.
     *
     * @return array
     *   An array containing view modes, style configuration,
     *   and toolbar configuration.
     */
    private function configureViewModes(EditorInterface $editor) {
        $element_style_configuration = [];
        $toolbar_configuration = [];
        $media_embed_filter = $editor->getFilterFormat()
            ->filters('media_embed');
        $media_bundles = MediaType::loadMultiple();
        $bundles_per_view_mode = [];
        $all_view_modes = $this->entityDisplayRepository
            ->getViewModeOptions('media');
        $allowed_view_modes = $media_embed_filter->settings['allowed_view_modes'];
        $default_view_mode = $media_embed_filter->settings['default_view_mode'];
        // @todo Remove in https://www.drupal.org/project/drupal/issues/3277049.
        // This is a workaround until the above issue is fixed to prevent the
        // editor from crashing because the frontend expects the default view mode
        // to exist in drupalElementStyles.
        if (!array_key_exists($default_view_mode, $allowed_view_modes)) {
            $allowed_view_modes[$default_view_mode] = $default_view_mode;
        }
        // Return early since there is no need to configure if there
        // are less than 2 view modes.
        if ($allowed_view_modes < 2) {
            return [];
        }
        // Configure view modes.
        foreach (array_keys($media_bundles) as $bundle) {
            $allowed_view_modes_by_bundle = $this->entityDisplayRepository
                ->getViewModeOptionsByBundle('media', $bundle);
            foreach (array_keys($allowed_view_modes_by_bundle) as $view_mode) {
                // Get the bundles that have this view mode enabled.
                $bundles_per_view_mode[$view_mode][] = $bundle;
            }
        }
        // Limit to view modes allowed by filter.
        $bundles_per_view_mode = array_intersect_key($bundles_per_view_mode, $allowed_view_modes);
        // Configure view mode element styles.
        foreach (array_keys($all_view_modes) as $view_mode) {
            if (array_key_exists($view_mode, $bundles_per_view_mode)) {
                $specific_bundles = $bundles_per_view_mode[$view_mode];
                if ($view_mode == $default_view_mode) {
                    $element_style_configuration[] = [
                        'isDefault' => TRUE,
                        'name' => $default_view_mode,
                        'title' => $all_view_modes[$view_mode],
                        'attributeName' => 'data-view-mode',
                        'attributeValue' => $view_mode,
                        'modelElements' => [
                            'drupalMedia',
                        ],
                        'modelAttributes' => [
                            'drupalMediaType' => array_keys($media_bundles),
                        ],
                    ];
                }
                else {
                    $element_style_configuration[] = [
                        'name' => $view_mode,
                        'title' => $all_view_modes[$view_mode],
                        'attributeName' => 'data-view-mode',
                        'attributeValue' => $view_mode,
                        'modelElements' => [
                            'drupalMedia',
                        ],
                        'modelAttributes' => [
                            'drupalMediaType' => $specific_bundles,
                        ],
                    ];
                }
            }
        }
        $items = [];
        foreach (array_keys($allowed_view_modes) as $view_mode) {
            $items[] = "drupalElementStyle:viewMode:{$view_mode}";
        }
        $default_item = 'drupalElementStyle:viewMode:' . $default_view_mode;
        if (!empty($allowed_view_modes)) {
            // Configure toolbar dropdown menu.
            $toolbar_configuration = [
                'name' => 'drupalMedia:viewMode',
                'display' => 'listDropdown',
                'defaultItem' => $default_item,
                'defaultText' => 'View mode',
                'items' => $items,
            ];
        }
        return [
            $element_style_configuration,
            $toolbar_configuration,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor) : array {
        $dynamic_plugin_config = $static_plugin_config;
        $dynamic_plugin_config['drupalMedia']['previewURL'] = Url::fromRoute('media.filter.preview')->setRouteParameter('filter_format', $editor->getFilterFormat()
            ->id())
            ->toString(TRUE)
            ->getGeneratedUrl();
        [
            $element_style_configuration,
            $toolbar_configuration,
        ] = self::configureViewModes($editor);
        $dynamic_plugin_config['drupalElementStyles']['viewMode'] = $element_style_configuration;
        if ($this->getConfiguration()['allow_view_mode_override']) {
            $dynamic_plugin_config['drupalMedia']['toolbar'][] = $toolbar_configuration;
        }
        $dynamic_plugin_config['drupalMedia']['metadataUrl'] = self::getUrlWithReplacedCsrfTokenPlaceholder(Url::fromRoute('ckeditor5.media_entity_metadata')->setRouteParameter('editor', $editor->id()));
        $dynamic_plugin_config['drupalMedia']['previewCsrfToken'] = \Drupal::csrfToken()->get('X-Drupal-MediaPreview-CSRF-Token');
        return $dynamic_plugin_config;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getElementsSubset() : array {
        $subset = $this->getPluginDefinition()
            ->getElements();
        $view_mode_override_enabled = $this->getConfiguration()['allow_view_mode_override'];
        if (!$view_mode_override_enabled) {
            $subset = array_diff($subset, [
                '<drupal-media data-view-mode>',
            ]);
        }
        return $subset;
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return [
            'allow_view_mode_override' => FALSE,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $form['allow_view_mode_override'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Allow the user to override the default view mode'),
            '#default_value' => $this->configuration['allow_view_mode_override'],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
        $form_value = $form_state->getValue('allow_view_mode_override');
        $form_state->setValue('allow_view_mode_override', (bool) $form_value);
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        $this->configuration['allow_view_mode_override'] = $form_state->getValue('allow_view_mode_override');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CKEditor5PluginConfigurableTrait::getConfiguration public function
CKEditor5PluginConfigurableTrait::setConfiguration public function
DynamicPluginConfigWithCsrfTokenUrlTrait::getUrlWithReplacedCsrfTokenPlaceholder private static function Gets the given URL with all placeholders replaced.
Media::$entityDisplayRepository protected property The entity display repository.
Media::buildConfigurationForm public function
Media::configureViewModes private function Configures allowed view modes.
Media::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Media::defaultConfiguration public function
Media::getDynamicPluginConfig public function Allows a plugin to modify its static configuration. Overrides CKEditor5PluginDefault::getDynamicPluginConfig
Media::getElementsSubset public function Returns a configured subset of the elements supported by this plugin. Overrides CKEditor5PluginElementsSubsetInterface::getElementsSubset
Media::submitConfigurationForm public function
Media::validateConfigurationForm public function
Media::__construct public function Media 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

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