Commit 7814f11c authored by Christian Fritsch's avatar Christian Fritsch
Browse files

Issue #3264437 by chr.fritsch: Do not store the entire form context in the form state

parent 705c2a40
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -13,5 +13,12 @@
        "drupal/inline_entity_form": "^1.0",
        "drupal/paragraphs": "^1.0",
        "drupal/entity_reference_revisions": "^1.0"
    },
    "extra": {
        "patches": {
            "drupal/inline_entity_form": {
                "Changes are lost when collapsing a paragraphs subform including an inline_entity_form": "https://www.drupal.org/files/issues/2021-04-26/2901158-16_0.patch"
            }
        }
    }
}

drupalci.yml

0 → 100644
+38 −0
Original line number Diff line number Diff line
build:
  environment:
    startcontainers:
      runcontainers:
    create_db:
      dbcreate:
  codebase:
    assemble_codebase:
      checkout_core:
      checkout.contrib:
      fetch:
      patch:
      composer.core_install:
      gather_dependencies:
      update_build:
      yarn_install:
      start_phantomjs:
  assessment:
    validate_codebase:
      phplint:
      container_composer:
      host_command:
        commands:
          - 'cd modules/contrib/inline_entity_form; sudo -u www-data curl "https://www.drupal.org/files/issues/2021-04-26/2901158-16_0.patch" | sudo -u www-data patch -p1'
      csslint:
        halt-on-fail: true
      eslint:
        halt-on-fail: false
      phpcs:
        halt-on-fail: true
    testing:
      run_tests.standard:
        types: 'PHPUnit-Unit,PHPUnit-Kernel,PHPUnit-Functional'
      run_tests.js:
        concurrency: 15
        types: 'PHPUnit-FunctionalJavascript'
      # There are no nightwatch tests yet.
      # nightwatchjs:
+8 −35
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace Drupal\entity_reference_actions;

use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\MessageCommand;
@@ -57,13 +56,6 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
   */
  protected $requestStack;

  /**
   * The UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuidGenerator;

  /**
   * The HTTP kernel service.
   *
@@ -101,16 +93,13 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
   *   The current user.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   * @param \Drupal\Component\Uuid\UuidInterface $uuidGenerator
   *   The UUID generator service.
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The HTTP kernel service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, RequestStack $requestStack, UuidInterface $uuidGenerator, HttpKernelInterface $httpKernel) {
  public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, RequestStack $requestStack, HttpKernelInterface $httpKernel) {
    $this->entityTypeManager = $entityTypeManager;
    $this->currentUser = $currentUser;
    $this->requestStack = $requestStack;
    $this->uuidGenerator = $uuidGenerator;
    $this->httpKernel = $httpKernel;
  }

@@ -118,7 +107,7 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('entity_type.manager'), $container->get('current_user'), $container->get('request_stack'), $container->get('uuid'), $container->get('http_kernel'));
    return new static($container->get('entity_type.manager'), $container->get('current_user'), $container->get('request_stack'), $container->get('http_kernel'));
  }

  /**
@@ -170,10 +159,6 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
    $items = $context['items'];
    $field_definition = $items->getFieldDefinition();

    $uuid = 'entity_reference_actions-' . $this->uuidGenerator->generate();

    $form_state->set($uuid, $context);

    $element['entity_reference_actions_messages'] = [
      '#type' => 'container',
      '#attributes' => ['data-entity-reference-actions-messages' => ''],
@@ -181,7 +166,8 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {

    $element['entity_reference_actions'] = [
      '#type' => 'simple_actions',
      '#uuid' => $uuid,
      '#target_ids' => $items->getValue(),
      '#target_type' => $items->getSettings()['target_type'],
      '#attached' => [
        'library' => [
          'core/drupal.dialog.ajax',
@@ -196,9 +182,8 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
    $bulk_options = $this->getBulkOptions();
    foreach ($bulk_options as $id => $label) {
      $element['entity_reference_actions'][$id] = [
        '#type' => 'submit',
        '#type' => 'button',
        '#limit_validation_errors' => [$element['widget']['#parents']],
        '#submit' => [],
        '#id' => $field_definition->getName() . '_' . $id . '_button',
        '#name' => $field_definition->getName() . '_' . $id . '_button',
        '#value' => $label,
@@ -227,26 +212,14 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {

    $button = $form_state->getTriggeringElement();

    $parents = array_slice($button['#array_parents'], 0, -2);
    // The field name we are acting on, deep from the form structure.
    $field_name = end($parents);

    $parents = array_slice($parents, 0, -1);
    $parents = array_slice($button['#array_parents'], 0, -1);
    $values = NestedArray::getValue($form, $parents);

    $context = $form_state->get($values[$field_name]['entity_reference_actions']['#uuid']);

    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    $items = $context['items'];

    $context['widget']->extractFormValues($items, $values, $form_state);

    $action = $this->actions[end($button['#array_parents'])];

    $ids = array_filter(!$items->isEmpty() ? array_column($items->getValue(), 'target_id') : []);
    $ids = array_filter(array_column($values['#target_ids'], 'target_id'));

    $entities = $this->entityTypeManager->getStorage($items->getSettings()['target_type'])
      ->loadMultiple($ids);
    $entities = $this->entityTypeManager->getStorage($values['#target_type'])->loadMultiple($ids);

    $commands = [];
    $entities = array_filter($entities, function ($entity) use ($action, &$commands) {
+12 −0
Original line number Diff line number Diff line
@@ -130,6 +130,15 @@ class SubformTest extends WebDriverTestBase {

    $this->drupalGet($entity->toUrl('edit-form'));

    // Add an image to the autocomplete field.
    $image2 = Media::create([
      'name' => 'image 2',
      'bundle' => $this->mediaImageType->id(),
      'published' => TRUE,
    ]);
    $image2->save();
    $this->getSession()->getPage()->find('css', '[data-drupal-selector="edit-field-reference-0-inline-entity-form-field-media-1-target-id"]')->setValue('image 2');

    $this->getSession()->getPage()->find('css', 'li.dropbutton-toggle button')->click();
    $this->getSession()->getPage()->pressButton('Unpublish all media items');

@@ -140,6 +149,9 @@ class SubformTest extends WebDriverTestBase {
    $this->mediaImage = Media::load($this->mediaImage->id());
    $this->assertFalse($this->mediaImage->isPublished());

    $image2 = Media::load($image2->id());
    $this->assertFalse($image2->isPublished());

    $this->getSession()->getPage()->pressButton('Save');
    $entity = EntityTest::load($entity->id());