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

Issue #3348390 by amateescu, s_leu, Grevil: Improve the way entity forms are...

Issue #3348390 by amateescu, s_leu, Grevil: Improve the way entity forms are "disabled" early when an entity is being edited in a workspace

(cherry picked from commit 1754440b)
parent d641512c
Branches
Tags
6 merge requests!11958Issue #3490507 by alexpott, smustgrave: Fix bogus mocking in...,!11769Issue #3517987: Add option to contextual filters to encode slashes in query parameter.,!11185Issue #3477324 by andypost, alexpott: Fix usage of str_getcsv() and fgetcsv() for PHP 8.4,!9944Issue #3483353: Consider making the createCopy config action optionally fail...,!8325Update file Sort.php,!8095Expose document root on install
Pipeline #163449 passed with warnings
Pipeline: drupal

#163464

    Pipeline: drupal

    #163461

      Pipeline: drupal

      #163455

        ......@@ -9,6 +9,8 @@
        use Drupal\Core\Form\FormInterface;
        use Drupal\Core\Routing\RouteMatchInterface;
        use Drupal\Core\StringTranslation\StringTranslationTrait;
        use Drupal\Core\TypedData\TypedDataManagerInterface;
        use Drupal\workspaces\Plugin\Validation\Constraint\EntityWorkspaceConflictConstraint;
        use Drupal\workspaces\WorkspaceInformationInterface;
        use Drupal\workspaces\WorkspaceManagerInterface;
        use Symfony\Component\HttpFoundation\Request;
        ......@@ -24,7 +26,8 @@ class WorkspacesHtmlEntityFormController extends FormController {
        public function __construct(
        protected readonly FormController $entityFormController,
        protected readonly WorkspaceManagerInterface $workspaceManager,
        protected readonly WorkspaceInformationInterface $workspaceInfo
        protected readonly WorkspaceInformationInterface $workspaceInfo,
        protected readonly TypedDataManagerInterface $typedDataManager
        ) {}
        /**
        ......@@ -34,16 +37,42 @@ public function getContentResult(Request $request, RouteMatchInterface $route_ma
        $form_arg = $this->getFormArgument($route_match);
        $form_object = $this->getFormObject($route_match, $form_arg);
        /** @var \Drupal\Core\Entity\EntityInterface $entity */
        $entity = $form_object->getEntity();
        if ($this->workspaceInfo->isEntitySupported($entity)) {
        $active_workspace = $this->workspaceManager->getActiveWorkspace();
        // Prepare a minimal render array in case we need to return it.
        $build['#cache']['contexts'] = $entity->getCacheContexts();
        $build['#cache']['tags'] = $entity->getCacheTags();
        $build['#cache']['max-age'] = $entity->getCacheMaxAge();
        // Prevent entities from being edited if they're tracked in workspace.
        if ($form_object->getOperation() !== 'delete') {
        $constraints = array_values(array_filter($entity->getTypedData()->getConstraints(), function ($constraint) {
        return $constraint instanceof EntityWorkspaceConflictConstraint;
        }));
        if (!empty($constraints)) {
        $violations = $this->typedDataManager->getValidator()->validate(
        $entity->getTypedData(),
        $constraints[0]
        );
        if (count($violations)) {
        $build['#markup'] = $violations->get(0)->getMessage();
        return $build;
        }
        }
        }
        // Prevent entities from being deleted in a workspace if they have a
        // published default revision.
        if ($form_object->getOperation() === 'delete' && $active_workspace && !$this->workspaceInfo->isEntityDeletable($entity, $active_workspace)) {
        $build['#markup'] = $this->t('This @entity_type_label can only be deleted in the Live workspace.', [
        '@entity_type_label' => $entity->getEntityType()->getSingularLabel(),
        ]);
        return $build;
        }
        }
        ......
        ......@@ -8,7 +8,6 @@
        use Drupal\Core\Entity\RevisionableInterface;
        use Drupal\Core\Form\FormStateInterface;
        use Drupal\Core\StringTranslation\StringTranslationTrait;
        use Drupal\workspaces\Plugin\Validation\Constraint\EntityWorkspaceConflictConstraint;
        use Symfony\Component\DependencyInjection\ContainerInterface;
        /**
        ......@@ -319,17 +318,6 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, $f
        if ($this->workspaceManager->hasActiveWorkspace()) {
        $form['#entity_builders'][] = [static::class, 'entityFormEntityBuild'];
        }
        // Run the workspace conflict validation constraint when the entity form is
        // being built so we can "disable" it early and display a message to the
        // user, instead of allowing them to enter data that can never be saved.
        foreach ($entity->validate()->getEntityViolations() as $violation) {
        if ($violation->getConstraint() instanceof EntityWorkspaceConflictConstraint) {
        $form['#markup'] = $violation->getMessage();
        $form['#access'] = FALSE;
        continue;
        }
        }
        }
        /**
        ......
        ......@@ -123,6 +123,10 @@ public function testBlocksInWorkspaces(): void {
        $assert_session->pageTextNotContains($second_block_body);
        $assert_session->pageTextContains('The DEFAULT block body');
        // Check the concurrent editing protection on the Layout Builder form.
        $this->drupalGet('/node/1/layout');
        $assert_session->pageTextContains('The content is being edited in the Stage workspace. As a result, your changes cannot be saved.');
        $stage->publish();
        $this->drupalGet('node/1');
        $assert_session->pageTextNotContains('The DEFAULT block body');
        ......
        ......@@ -94,5 +94,5 @@ services:
        decorates: controller.entity_form
        class: Drupal\workspaces\Controller\WorkspacesHtmlEntityFormController
        public: false
        arguments: ['@.inner', '@workspaces.manager', '@workspaces.information']
        arguments: ['@.inner', '@workspaces.manager', '@workspaces.information', '@typed_data_manager']
        Drupal\workspaces\Controller\WorkspacesHtmlEntityFormController: '@workspaces.controller.entity_form'
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment