Unverified Commit 77584d1c authored by alexpott's avatar alexpott

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

(cherry picked from commit 9ff7e11b)
parent 6ddb9ed8
......@@ -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);
......
......@@ -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.'));
}
}
......
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment