Commit f41e8a46 authored by Nejc Koporec's avatar Nejc Koporec Committed by Adam G-H
Browse files

Issue #3287235 by nkoporec, attilatilman, Project Update Bot, Wim Leers,...

Issue #3287235 by nkoporec, attilatilman, Project Update Bot, Wim Leers, jcnventura, phenaproxima, Dave Reid, lhridley: Drupal 10 compatibility
parent c0248621
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
        "drupal/embed": "^1.5"
    },
    "require-dev": {
        "drupal/ckeditor": "^1",
        "drupal/entity_browser": "^2.2"
    }
}
+10 −7
Original line number Diff line number Diff line
<?php

use Drupal\file\FileInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
/**
 * @file
 * Hooks provided by the Entity Embed module.
@@ -40,17 +43,17 @@ function hook_entity_embed_display_plugins_for_context_alter(array &$definitions
  $entity = $contexts['entity'];

  // For video and audio files, limit the available options to the media player.
  if ($entity instanceof \Drupal\file\FileInterface && in_array($entity->bundle(), ['audio', 'video'])) {
  if ($entity instanceof FileInterface && in_array($entity->bundle(), ['audio', 'video'])) {
    $definitions = array_intersect_key($definitions, array_flip(['file:jwplayer_formatter']));
  }

  // For images, use the image formatter.
  if ($entity instanceof \Drupal\file\FileInterface && in_array($entity->bundle(), ['image'])) {
  if ($entity instanceof FileInterface && in_array($entity->bundle(), ['image'])) {
    $definitions = array_intersect_key($definitions, array_flip(['image:image']));
  }

  // For nodes, use the default option.
  if ($entity instanceof \Drupal\node\NodeInterface) {
  if ($entity instanceof NodeInterface) {
    $definitions = array_intersect_key($definitions, array_flip(['entity_reference:entity_reference_entity_view']));
  }
}
@@ -63,7 +66,7 @@ function hook_entity_embed_display_plugins_for_context_alter(array &$definitions
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 */
function hook_entity_embed_context_alter(array &$context, \Drupal\Core\Entity\EntityInterface $entity) {
function hook_entity_embed_context_alter(array &$context, EntityInterface $entity) {
  if (isset($context['overrides']) && is_array($context['overrides'])) {
    foreach ($context['overrides'] as $key => $value) {
      $entity->key = $value;
@@ -79,7 +82,7 @@ function hook_entity_embed_context_alter(array &$context, \Drupal\Core\Entity\En
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 */
function hook_ENTITY_TYPE_embed_context_alter(array &$context, \Drupal\Core\Entity\EntityInterface $entity) {
function hook_ENTITY_TYPE_embed_context_alter(array &$context, EntityInterface $entity) {
  if (isset($context['overrides']) && is_array($context['overrides'])) {
    foreach ($context['overrides'] as $key => $value) {
      $entity->key = $value;
@@ -101,7 +104,7 @@ function hook_ENTITY_TYPE_embed_context_alter(array &$context, \Drupal\Core\Enti
 * @param array $context
 *   The context array.
 */
function hook_entity_embed_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, array &$context) {
function hook_entity_embed_alter(array &$build, EntityInterface $entity, array &$context) {
  // Remove the contextual links.
  if (isset($build['#contextual_links'])) {
    unset($build['#contextual_links']);
@@ -118,7 +121,7 @@ function hook_entity_embed_alter(array &$build, \Drupal\Core\Entity\EntityInterf
 * @param array $context
 *   The context array.
 */
function hook_ENTITY_TYPE_embed_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, array &$context) {
function hook_ENTITY_TYPE_embed_alter(array &$build, EntityInterface $entity, array &$context) {
  // Remove the contextual links.
  if (isset($build['#contextual_links'])) {
    unset($build['#contextual_links']);
+1 −1
Original line number Diff line number Diff line
name: Entity Embed
type: module
description: 'Allows entities to be embedded using a text editor.'
core_version_requirement: ^9.3
core_version_requirement: ^9.3 || ^10
package: Filters
dependencies:
  - drupal:editor
+4 −92
Original line number Diff line number Diff line
@@ -5,97 +5,9 @@
 * Contains install and update functions for Entity Embed.
 */

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\embed\Entity\EmbedButton;
use Drupal\Core\Utility\UpdateException;

/**
 * Convert entity embed buttons to embed buttons.
 *
 * @todo Can we reuse the existing UUID and save before deleting the old button?
 */
function entity_embed_update_8001() {
  $config_factory = \Drupal::configFactory();

  foreach ($config_factory->listAll('entity_embed.embed_button.') as $config_name) {
    $old_embed_button = $config_factory->getEditable($config_name);
    $values = $old_embed_button->getRawData();

    if (EmbedButton::load($values['id'])) {
      throw new UpdateException('Unable to convert entity_embed.embed_button.' . $values['id'] . ' to embed.button.' . $values['id'] . ' since the latter already exists.');
    }

    // Move some data around.
    $values['type_id'] = 'entity';
    $values['type_settings'] = [
      'entity_type' => $values['entity_type'],
      'bundles' => array_keys(array_filter($values['entity_type_bundles'])),
      'display_plugins' => array_keys(array_filter($values['display_plugins'])),
    ];
    $values['icon_uuid'] = $values['button_icon_uuid'];
    unset($values['entity_type']);
    unset($values['entity_type_bundles']);
    unset($values['display_plugins']);
    unset($values['button_icon_uuid']);

    // Save the new embed button and delete the old one.
    $embed_button = EmbedButton::create($values);
    $embed_button->save();
    $old_embed_button->delete();
  }
}

/**
 * Updates the default mode settings.
 */
function entity_embed_update_8002() {
  \Drupal::configFactory()
    ->getEditable('entity_embed.settings')
    ->set('rendered_entity_mode', TRUE)
    ->save();
}

/**
 * Updates allowed HTML for all filter configs that have an Entity Embed button.
 */
function entity_embed_update_8003() {
  $buttons = \Drupal::entityTypeManager()->getStorage('embed_button')->loadMultiple();
  $filter_formats_with_embed_button = [];

  // Get filter formats from editors with entity embed button.
  foreach (\Drupal::entityTypeManager()->getStorage('editor')->loadMultiple() as $editor) {
    foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($editor->getSettings())) as $settings_value) {
      foreach ($buttons as $button) {
        if ($settings_value == $button->id()) {
          $filter_formats_with_embed_button[] = $editor->getFilterFormat();
        }
      }
    }
  }
  foreach ($filter_formats_with_embed_button as $filter_format) {
    foreach ($filter_format->filters()->getAll() as $filter) {
      if (isset($filter->getConfiguration()['settings']['allowed_html'])) {
        $allowed_html = $filter->getConfiguration()['settings']['allowed_html'];
        if (strpos($allowed_html, 'data-entity-embed-settings')) {
          $allowed_html = str_replace('data-entity-embed-settings', 'data-entity-embed-settings data-entity-embed-display-settings', $allowed_html);
          $filter_format->setFilterConfig($filter->getPluginId(), ['settings' => ['allowed_html' => $allowed_html]]);
          $filter_format->save();
        }
      }
    }
  }
}

/**
 * Adds new content entity type to remove dependency on node module.
+ * Implements hook_update_last_removed().
  */
function entity_embed_update_8004() {
  \Drupal::entityDefinitionUpdateManager()->installEntityType(new ContentEntityType([
    'id' => 'entity_embed_fake_entity',
    'label' => new TranslatableMarkup('Fake entity type'),
    'handlers' => [
      'storage' => 'Drupal\\Core\\Entity\\ContentEntityNullStorage',
    ],
  ]));
function entity_embed_update_last_removed() {
  return 8004;
}
+15 −3
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -80,6 +81,13 @@ class EntityEmbedDialog extends FormBase {
   */
  protected $moduleHandler;

  /**
   * The file URL generator.
   *
   * @var \Drupal\Core\File\FileUrlGeneratorInterface
   */
  protected $fileUrlGenerator;

  /**
   * The entity browser settings from the entity embed button.
   *
@@ -102,14 +110,17 @@ class EntityEmbedDialog extends FormBase {
   *   The entity field manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\File\FileUrlGeneratorInterface
   *   The file URL generator.
   */
  public function __construct(EntityEmbedDisplayManager $entity_embed_display_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler) {
  public function __construct(EntityEmbedDisplayManager $entity_embed_display_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler, FileUrlGeneratorInterface $file_url_generator) {
    $this->entityEmbedDisplayManager = $entity_embed_display_manager;
    $this->formBuilder = $form_builder;
    $this->entityTypeManager = $entity_type_manager;
    $this->eventDispatcher = $event_dispatcher;
    $this->entityFieldManager = $entity_field_manager;
    $this->moduleHandler = $module_handler;
    $this->fileUrlGenerator = $file_url_generator;
  }

  /**
@@ -122,7 +133,8 @@ class EntityEmbedDialog extends FormBase {
      $container->get('entity_type.manager'),
      $container->get('event_dispatcher'),
      $container->get('entity_field.manager'),
      $container->get('module_handler')
      $container->get('module_handler'),
      $container->get('file_url_generator')
    );
  }

@@ -415,7 +427,7 @@ class EntityEmbedDialog extends FormBase {
        $entity_label = $entity->toLink($entity->label(), 'canonical', $options)->toString();
      }
      elseif ($entity->getEntityTypeId() == 'file') {
        $entity_label = '<a href="' . file_create_url($entity->getFileUri()) . '" target="_blank">' . $entity->label() . '</a>';
          $entity_label = '<a href="' . $this->fileUrlGenerator->generateAbsoluteString($entity->getFileUri()) . '" target="_blank">' . $entity->label() . '</a>';
      }
      else {
        $entity_label = '<a href="' . $entity->toUrl()->toString() . '" target="_blank">' . $entity->label() . '</a>';
Loading