function Media::configureViewModes

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media::configureViewModes()
  2. 11.x core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media::configureViewModes()

Configures allowed view modes.

Parameters

\Drupal\editor\EditorInterface $editor: A configured text editor object.

Return value

array An array containing view modes, style configuration, and toolbar configuration.

1 call to Media::configureViewModes()
Media::getDynamicPluginConfig in core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php
Allows a plugin to modify its static configuration.

File

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

Class

Media
CKEditor 5 Media plugin.

Namespace

Drupal\ckeditor5\Plugin\CKEditor5Plugin

Code

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,
    ];
}

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