You are here

public function Update400::convertEditorsToMediaLibrary in Lightning Media 8.4

Optionally converts WYSIWYG editors to use the media library.

@update

Parameters

\Symfony\Component\Console\Style\StyleInterface $io: The I/O style.

File

src/Update/Update400.php, line 91

Class

Update400
Contains optional updates targeting Lightning Media 4.0.0.

Namespace

Drupal\lightning_media\Update

Code

public function convertEditorsToMediaLibrary(StyleInterface $io) {
  $storage = $this->entityTypeManager
    ->getStorage('editor');

  /** @var \Drupal\editor\EditorInterface[] $editors */
  $editors = $storage
    ->loadByProperties([
    'editor' => 'ckeditor',
  ]);
  foreach ($editors as $editor) {

    // Check if the editor has the media_browser embed button enabled at all.
    // If it doesn't, there's nothing to do.
    $button_path = $this
      ->getPathToMediaBrowserButton($editor);
    if (empty($button_path)) {
      continue;
    }
    $question = (string) $this
      ->t('Do you want to convert the @editor WYSIWYG editor to use the media library?', [
      '@editor' => $editor
        ->label(),
    ]);
    if ($io
      ->confirm($question)) {

      // Change the 'media_browser' toolbar item to 'drupalmedialibrary'.
      $settings = $editor
        ->getSettings();
      NestedArray::setValue($settings, $button_path, 'DrupalMediaLibrary');
      $editor
        ->setSettings($settings);
      $storage
        ->save($editor);

      // Ensure that the associated filter format, if any, is correctly
      // configured for embedding media.
      if ($editor
        ->hasAssociatedFilterFormat()) {
        $this
          ->setUpFilterFormat($editor
          ->getFilterFormat());
      }
    }
  }
}