You are here

public function DrupalMediaLibrary::getConfig in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php \Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary::getConfig()

Returns the additions to CKEDITOR.config for a specific CKEditor instance.

The editor's settings can be retrieved via $editor->getSettings(), but be aware that it may not yet contain plugin-specific settings, because the user may not yet have configured the form. If there are plugin-specific settings (verify with isset()), they can be found at

$settings = $editor
  ->getSettings();
$plugin_specific_settings = $settings['plugins'][$plugin_id];

Parameters

\Drupal\editor\Entity\Editor $editor: A configured text editor object.

Return value

array A keyed array, whose keys will end up as keys under CKEDITOR.config.

Overrides CKEditorPluginInterface::getConfig

File

core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php, line 110

Class

DrupalMediaLibrary
Defines the "drupalmedialibrary" plugin.

Namespace

Drupal\media_library\Plugin\CKEditorPlugin

Code

public function getConfig(Editor $editor) {

  // If the editor has not been saved yet, we may not be able to create a
  // coherent MediaLibraryState object, which is needed in order to generate
  // the required configuration. But, if we're creating a new editor, we don't
  // need to do that anyway, so just return an empty array.
  if ($editor
    ->isNew()) {
    return [];
  }
  $media_type_ids = $this->mediaTypeStorage
    ->getQuery()
    ->execute();
  if ($editor
    ->hasAssociatedFilterFormat()) {
    if ($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)) {

    // Due to a bug where the active item styling and the focus styling
    // create the visual appearance of two active items, we'll move
    // the 'image' media type to first position, so that the focused item and
    // the active item are the same.
    // 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(),
  ]);
  return [
    'DrupalMediaLibrary_url' => Url::fromRoute('media_library.ui')
      ->setOption('query', $state
      ->all())
      ->toString(TRUE)
      ->getGeneratedUrl(),
    'DrupalMediaLibrary_dialogOptions' => MediaLibraryUiBuilder::dialogOptions(),
  ];
}