You are here

public function Update360::cloneMediaBrowser in Lightning Media 8.4

Same name and namespace in other branches
  1. 8.3 src/Update/Update360.php \Drupal\lightning_media\Update\Update360::cloneMediaBrowser()

Changes the media browser to display in a modal dialog.

@update

File

src/Update/Update360.php, line 90

Class

Update360
Contains optional updates targeting Lightning Media 3.6.0.

Namespace

Drupal\lightning_media\Update

Code

public function cloneMediaBrowser(StyleInterface $io) {

  /** @var \Drupal\embed\EmbedButtonInterface $button */
  $button = $this->embedButtonStorage
    ->load('media_browser');

  /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
  $browser = $this->entityBrowserStorage
    ->load('media_browser');

  // If either the embed button or the media browser doesn't exist, there's
  // nothing we can really do here.
  if (empty($button) || empty($browser)) {
    return;
  }

  // If the media browser isn't using an iFrame, then there's nothing we need
  // to do here anyway.
  if ($browser
    ->getDisplay()
    ->getPluginId() !== 'iframe') {
    return;
  }

  // If the embed button doesn't use the media browser, we can assume that
  // the configuration has deviated too far from what we ship for us to bother
  // with the update.
  if ($button
    ->getTypePlugin()
    ->getConfigurationValue('entity_browser') !== $browser
    ->id()) {
    return;
  }
  $variables = [
    '@browser' => $browser
      ->label(),
  ];
  $question = (string) $this
    ->t('Do you want to display the @browser entity browser in a modal dialog? This will create a duplicate of the entity browser, specifically for use by CKEditor.', $variables);
  if ($io
    ->confirm($question) === FALSE) {
    return;
  }
  $clone_id = $io
    ->ask((string) $this
    ->t('Enter the machine name of the CKEditor-only duplicate.'), 'ckeditor_media_browser');
  $clone_label = $io
    ->ask((string) $this
    ->t('Enter the label of the CKEditor-only duplicate.'), (string) $this
    ->t('@browser (CKEditor)', $variables));
  $clone = $browser
    ->createDuplicate()
    ->setName($clone_id)
    ->setLabel($clone_label);
  $this->entityBrowserStorage
    ->save($clone);

  // Ensure that media roles can access the new entity browser.
  $permissions = [
    "access {$clone_id} entity browser pages",
  ];
  user_role_grant_permissions('media_creator', $permissions);
  user_role_grant_permissions('media_manager', $permissions);
  if ($this->contentRoleManager) {
    $this->contentRoleManager
      ->grantPermissions('creator', $permissions);
  }

  // For reasons unknown, $button->getTypePlugin()->setConfigurationValue()
  // does not work for this.
  $settings = [
    'entity_browser' => $clone
      ->id(),
  ];
  $button
    ->set('type_settings', $settings + $button
    ->getTypeSettings());
  $this->embedButtonStorage
    ->save($button);
  $browser
    ->setDisplay('modal')
    ->getDisplay()
    ->setConfiguration([
    'width' => '',
    'height' => '',
    'link_text' => (string) $this
      ->t('Add media'),
    'auto_open' => FALSE,
  ]);
  $this->entityBrowserStorage
    ->save($browser);
}