Skip to content
Snippets Groups Projects
Commit 6d0a54f6 authored by catch's avatar catch
Browse files

Issue #3471675 by amateescu, smustgrave, rkoller: RuntimeException when trying...

Issue #3471675 by amateescu, smustgrave, rkoller: RuntimeException when trying to add a new language
parent c706bf19
Branches
Tags
3 merge requests!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!579Issue #2230909: Simple decimals fail to pass validation
Pipeline #493317 passed with warnings
Pipeline: drupal

#493340

    Pipeline: drupal

    #493335

      Pipeline: drupal

      #493324

        ......@@ -48523,6 +48523,30 @@
        'count' => 1,
        'path' => __DIR__ . '/modules/workspaces/tests/src/Functional/WorkspaceEntityDeleteTest.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\workspaces\\\\Functional\\\\WorkspaceFormValidationTest\\:\\:isLabelInContentOverview\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        'count' => 1,
        'path' => __DIR__ . '/modules/workspaces/tests/src/Functional/WorkspaceFormValidationTest.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\workspaces\\\\Functional\\\\WorkspaceFormValidationTest\\:\\:setupWorkspaceSwitcherBlock\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        'count' => 1,
        'path' => __DIR__ . '/modules/workspaces/tests/src/Functional/WorkspaceFormValidationTest.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\workspaces\\\\Functional\\\\WorkspaceFormValidationTest\\:\\:switchToLive\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        'count' => 1,
        'path' => __DIR__ . '/modules/workspaces/tests/src/Functional/WorkspaceFormValidationTest.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\workspaces\\\\Functional\\\\WorkspaceFormValidationTest\\:\\:switchToWorkspace\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        'count' => 1,
        'path' => __DIR__ . '/modules/workspaces/tests/src/Functional/WorkspaceFormValidationTest.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\workspaces\\\\Functional\\\\WorkspaceMenuLinkContentIntegrationTest\\:\\:isLabelInContentOverview\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        ......@@ -297,7 +297,7 @@ public function entityQueryTagLatestTranslatedAffectedRevisionAlter(QueryInterfa
        *
        * Alters entity forms to disallow concurrent editing in multiple workspaces.
        */
        #[Hook('form_alter')]
        #[Hook('form_alter', order: Order::First)]
        public function entityFormAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
        if (!$form_state->getFormObject() instanceof EntityFormInterface) {
        return;
        ......
        ......@@ -29,20 +29,24 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id
        return;
        }
        // Add a validation step for every form if we are in a workspace.
        $this->addWorkspaceValidation($form);
        // If a form hasn't already been marked as safe or not to submit in a
        // workspace, check the generic interfaces.
        if (!$form_state->has('workspace_safe')) {
        $form_object = $form_state->getFormObject();
        $workspace_safe = $form_object instanceof WorkspaceSafeFormInterface
        || ($form_object instanceof WorkspaceDynamicSafeFormInterface && $form_object->isWorkspaceSafeForm($form, $form_state));
        // If a form has already been marked as safe or not to submit in a
        // workspace, we don't have anything else to do.
        if ($form_state->has('workspace_safe')) {
        return;
        $form_state->set('workspace_safe', $workspace_safe);
        }
        $form_object = $form_state->getFormObject();
        $workspace_safe = $form_object instanceof WorkspaceSafeFormInterface
        || ($form_object instanceof WorkspaceDynamicSafeFormInterface && $form_object->isWorkspaceSafeForm($form, $form_state));
        $form_state->set('workspace_safe', $workspace_safe);
        // Add a validation step for every other form.
        if ($form_state->get('workspace_safe') !== TRUE) {
        $form['workspace_safe'] = [
        '#type' => 'value',
        '#value' => FALSE,
        ];
        $this->addWorkspaceValidation($form);
        }
        }
        /**
        ......@@ -59,8 +63,14 @@ protected function addWorkspaceValidation(array &$element): void {
        }
        }
        if (isset($element['#validate'])) {
        if (isset($element['#submit'])) {
        $element['#validate'][] = [static::class, 'validateDefaultWorkspace'];
        // Ensure that the workspace validation is always shown, even when the
        // form element is limiting validation errors.
        if (isset($element['#limit_validation_errors']) && $element['#limit_validation_errors'] !== FALSE) {
        $element['#limit_validation_errors'][] = ['workspace_safe'];
        }
        }
        }
        ......@@ -68,8 +78,8 @@ protected function addWorkspaceValidation(array &$element): void {
        * Validation handler which sets a validation error for all unsupported forms.
        */
        public static function validateDefaultWorkspace(array &$form, FormStateInterface $form_state): void {
        if ($form_state->get('workspace_safe') !== TRUE) {
        $form_state->setError($form, new TranslatableMarkup('This form can only be submitted in the default workspace.'));
        if ($form_state->get('workspace_safe') !== TRUE && isset($form_state->getCompleteForm()['workspace_safe'])) {
        $form_state->setErrorByName('workspace_safe', new TranslatableMarkup('This form can only be submitted in the default workspace.'));
        }
        }
        ......
        <?php
        declare(strict_types=1);
        namespace Drupal\Tests\workspaces\Functional;
        use Drupal\Tests\BrowserTestBase;
        use Drupal\workspaces\Entity\Workspace;
        /**
        * Tests Workspaces form validation.
        *
        * @group workspaces
        */
        class WorkspaceFormValidationTest extends BrowserTestBase {
        use WorkspaceTestUtilities;
        /**
        * {@inheritdoc}
        */
        protected static $modules = ['block', 'form_test', 'workspaces'];
        /**
        * {@inheritdoc}
        */
        protected $defaultTheme = 'stark';
        /**
        * {@inheritdoc}
        */
        protected function setUp(): void {
        parent::setUp();
        $this->drupalLogin($this->drupalCreateUser(['administer workspaces']));
        $this->setupWorkspaceSwitcherBlock();
        }
        /**
        * Tests partial form validation through #limit_validation_errors.
        */
        public function testValidateLimitErrors(): void {
        $stage = Workspace::load('stage');
        $this->switchToWorkspace($stage);
        $edit = [
        'test' => 'test1',
        'test_numeric_index[0]' => 'test2',
        'test_substring[foo]' => 'test3',
        ];
        $path = 'form-test/limit-validation-errors';
        // Submit the form by pressing all the 'Partial validate' buttons.
        $this->drupalGet($path);
        $this->submitForm($edit, 'Partial validate');
        $this->assertSession()->pageTextContains('This form can only be submitted in the default workspace.');
        $this->drupalGet($path);
        $this->submitForm($edit, 'Partial validate (numeric index)');
        $this->assertSession()->pageTextContains('This form can only be submitted in the default workspace.');
        $this->drupalGet($path);
        $this->submitForm($edit, 'Partial validate (substring)');
        $this->assertSession()->pageTextContains('This form can only be submitted in the default workspace.');
        // Now test full form validation.
        $this->drupalGet($path);
        $this->submitForm($edit, 'Full validate');
        $this->assertSession()->pageTextContains('This form can only be submitted in the default workspace.');
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment