Unverified Commit 9ff7e11b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2998565 by amateescu, Sam152, jeqq: Workspaces' form alters break all...

Issue #2998565 by amateescu, Sam152, jeqq: Workspaces' form alters break all AJAX interaction on entity forms
parent 5f2605fe
Loading
Loading
Loading
Loading
+2 −2
Changes for core/modules/workspaces/src/EntityOperations.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -319,7 +319,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, $f
    // be a pending one.
    $active_workspace = $this->workspaceManager->getActiveWorkspace();
    if (!$active_workspace->isDefaultWorkspace()) {
      $form['#entity_builders'][] = [$this, 'entityFormEntityBuild'];
      $form['#entity_builders'][] = [get_called_class(), 'entityFormEntityBuild'];
    }

    /** @var \Drupal\workspaces\WorkspaceAssociationStorageInterface $workspace_association_storage */
@@ -340,7 +340,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, $f
  /**
   * Entity builder that marks all supported entities as pending revisions.
   */
  public function entityFormEntityBuild($entity_type_id, RevisionableInterface $entity, &$form, FormStateInterface &$form_state) {
  public static function entityFormEntityBuild($entity_type_id, RevisionableInterface $entity, &$form, FormStateInterface &$form_state) {
    // Set the non-default revision flag so that validation constraints are also
    // aware that a pending revision is about to be created.
    $entity->isDefaultRevision(FALSE);
+4 −6
Changes for core/modules/workspaces/src/FormOperations.php: 4 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Form\ViewsExposedForm;
use Drupal\workspaces\Form\WorkspaceFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -17,8 +17,6 @@
 */
class FormOperations implements ContainerInjectionInterface {

  use StringTranslationTrait;

  /**
   * The workspace manager service.
   *
@@ -104,16 +102,16 @@ protected function addWorkspaceValidation(array &$element) {
    }

    if (isset($element['#validate'])) {
      $element['#validate'][] = [$this, 'validateDefaultWorkspace'];
      $element['#validate'][] = [get_called_class(), 'validateDefaultWorkspace'];
    }
  }

  /**
   * Validation handler which sets a validation error for all unsupported forms.
   */
  public function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) {
  public static function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) {
    if ($form_state->get('workspace_safe') !== TRUE) {
      $form_state->setError($form, $this->t('This form can only be submitted in the default workspace.'));
      $form_state->setError($form, new TranslatableMarkup('This form can only be submitted in the default workspace.'));
    }
  }

+33 −0
Changes for core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@
namespace Drupal\Tests\workspaces\Kernel;

use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormState;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Form\SiteInformationForm;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
@@ -718,4 +720,35 @@ protected function flattenExpectedValues(array $expected, $entity_type_id) {
    return $flattened;
  }

  /**
   * Tests that entity forms can be stored in the form cache.
   */
  public function testFormCacheForEntityForms() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    $form_builder = $this->container->get('form_builder');

    $form = $this->entityTypeManager->getFormObject('entity_test_mulrevpub', 'default');
    $form->setEntity(EntityTestMulRevPub::create([]));

    $form_state = new FormState();
    $built_form = $form_builder->buildForm($form, $form_state);
    $form_builder->setCache($built_form['#build_id'], $built_form, $form_state);
  }

  /**
   * Tests that non-entity forms can be stored in the form cache.
   */
  public function testFormCacheForRegularForms() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    $form_builder = $this->container->get('form_builder');

    $form_state = new FormState();
    $built_form = $form_builder->getForm(SiteInformationForm::class, $form_state);
    $form_builder->setCache($built_form['#build_id'], $built_form, $form_state);
  }

}