Update400.php in Lightning Media 8.4
File
src/Update/Update400.php
View source
<?php
namespace Drupal\lightning_media\Update;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\editor\EditorInterface;
use Drupal\filter\FilterFormatInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class Update400 implements ContainerInjectionInterface {
use StringTranslationTrait;
private $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function convertMediaReferenceFieldsToMediaLibrary(StyleInterface $io) {
$storage = $this->entityTypeManager
->getStorage('entity_form_display');
foreach ($storage
->loadMultiple() as $display) {
$changed = FALSE;
foreach ($display
->getComponents() as $name => $component) {
if ($component['type'] === 'entity_browser_entity_reference' && $component['settings']['entity_browser'] === 'media_browser') {
$question = $this
->getQuestionForMediaReferenceField($display, $name);
if ($io
->confirm($question)) {
$display
->setComponent($name, [
'type' => 'media_library_widget',
]);
$changed = TRUE;
}
}
if ($changed) {
$storage
->save($display);
}
}
}
}
public function convertEditorsToMediaLibrary(StyleInterface $io) {
$storage = $this->entityTypeManager
->getStorage('editor');
$editors = $storage
->loadByProperties([
'editor' => 'ckeditor',
]);
foreach ($editors as $editor) {
$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)) {
$settings = $editor
->getSettings();
NestedArray::setValue($settings, $button_path, 'DrupalMediaLibrary');
$editor
->setSettings($settings);
$storage
->save($editor);
if ($editor
->hasAssociatedFilterFormat()) {
$this
->setUpFilterFormat($editor
->getFilterFormat());
}
}
}
}
private function getPathToMediaBrowserButton(EditorInterface $editor) {
$settings = $editor
->getSettings();
foreach ($settings['toolbar']['rows'] as $row_key => $row) {
foreach ($row as $group_key => $group) {
$item_key = array_search('media_browser', $group['items'], TRUE);
if ($item_key !== FALSE) {
return [
'toolbar',
'rows',
$row_key,
$group_key,
'items',
$item_key,
];
}
}
}
return [];
}
private function setUpFilterFormat(FilterFormatInterface $format) {
$filters = $format
->filters();
$embed_filter = $filters
->get('media_embed');
if ($embed_filter && $embed_filter->status) {
return;
}
$html_filter = $filters
->get('filter_html');
if ($html_filter && $html_filter->status) {
$configuration = $html_filter
->getConfiguration();
$configuration['settings']['allowed_html'] .= ' <drupal-media data-entity-type data-entity-uuid data-view-mode data-align data-caption alt>';
$format
->setFilterConfig('filter_html', $configuration);
}
$format
->setFilterConfig('media_embed', [
'settings' => [
'default_view_mode' => 'embedded',
'allowed_view_modes' => [],
],
'status' => TRUE,
]);
$this->entityTypeManager
->getStorage('filter_format')
->save($format);
}
private function getQuestionForMediaReferenceField(EntityFormDisplayInterface $display, $name) {
$entity_type = $display
->getTargetEntityTypeId();
$bundle = $display
->getTargetBundle();
$variables = [];
$variables['@field'] = $this->entityTypeManager
->getStorage('field_config')
->load("{$entity_type}.{$bundle}.{$name}")
->getLabel();
$form_mode = $display
->getMode();
if ($form_mode === EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE) {
$variables['@form_mode'] = (string) $this
->t('default');
}
else {
$variables['@form_mode'] = $this->entityTypeManager
->getStorage('entity_form_mode')
->load("{$entity_type}.{$form_mode}")
->label();
}
$entity_type = $this->entityTypeManager
->getDefinition($entity_type);
$bundle_entity_type = $entity_type
->getBundleEntityType();
if ($bundle_entity_type) {
$variables['@bundle_type'] = lcfirst($entity_type
->getBundleLabel());
$variables['@bundle'] = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($bundle)
->label();
$question = $this
->t('Do you want to convert the @field field of the @bundle @bundle_type to use the media library in the @form_mode form mode?', $variables);
}
else {
$variables['@entity_type'] = $this->entityTypeManager
->getDefinition($entity_type)
->getPluralLabel();
$question = $this
->t('Do you want to convert the @field field of @entity_type to use the media library in the @form_mode form mode?', $variables);
}
return (string) $question;
}
}
Classes
Name |
Description |
Update400 |
Contains optional updates targeting Lightning Media 4.0.0. |