From e59c5ae20eedde278c743af2683623df8f2fc3fe Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 18 Apr 2024 16:23:08 +1200 Subject: [PATCH] ENH Skip validation when creating a new elemental block --- src/GraphQL/Resolvers/Resolver.php | 4 ++-- src/Models/BaseElement.php | 14 ++++++++++---- src/Services/ReorderElements.php | 7 +++++-- tests/Behat/features/validation-failure.feature | 9 +++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/GraphQL/Resolvers/Resolver.php b/src/GraphQL/Resolvers/Resolver.php index bcc23190..674609c2 100644 --- a/src/GraphQL/Resolvers/Resolver.php +++ b/src/GraphQL/Resolvers/Resolver.php @@ -94,10 +94,10 @@ public static function resolveAddElementToArea( if ($afterElementID !== null) { /** @var ReorderElements $reorderer */ - $reorderer = Injector::inst()->create(ReorderElements::class, $newElement); + $reorderer = Injector::inst()->create(ReorderElements::class, $newElement, true); $reorderer->reorder($afterElementID); // also writes the element } else { - $newElement->write(); + $newElement->write(skipValidation: true); } return $newElement; diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index e1078558..033a2888 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -35,6 +35,7 @@ use SilverStripe\ORM\CMSPreviewable; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataObjectSchema; +use SilverStripe\ORM\ValidationResult; /** * Class BaseElement @@ -304,13 +305,18 @@ public function canCreate($member = null, $context = array()) return Permission::check('CMS_ACCESS', 'any', $member); } - public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) - { + public function write( + $showDebug = false, + $forceInsert = false, + $forceWrite = false, + $writeComponents = false, + bool $skipValidation = false + ) { // Skips writes for broken blocks, so that we can still publish the page to allow all other blocks to publish. if ($this->ObsoleteClassName) { return $this->ID; } - return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents); + return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents, $skipValidation); } /** @@ -553,7 +559,7 @@ public function getContentForCmsSearch(): string } // Allow projects to update contents of third-party elements. $this->extend('updateContentForCmsSearch', $contents); - + // Use |#| to delimit different fields rather than space so that you don't // accidentally join results of two columns that are next to each other in a table $content = implode('|#|', array_filter($contents)); diff --git a/src/Services/ReorderElements.php b/src/Services/ReorderElements.php index 3da7343b..936587e0 100644 --- a/src/Services/ReorderElements.php +++ b/src/Services/ReorderElements.php @@ -16,12 +16,14 @@ class ReorderElements */ protected $element; + private bool $elementIsNew; + /** * Create reordering service for specified Element * * @param BaseElement $element */ - public function __construct(BaseElement $element) + public function __construct(BaseElement $element, bool $elementIsNew = false) { if (!($element instanceof BaseElement)) { throw new InvalidArgumentException(sprintf( @@ -32,6 +34,7 @@ public function __construct(BaseElement $element) )); } + $this->elementIsNew = $elementIsNew; $this->setElement($element); } @@ -125,7 +128,7 @@ public function reorder($elementToBeAfterID = 0) // Now use the ORM to write a new version of the record that we are directly reordering $element->Sort = $newBlockPosition; - $element->write(); + $element->write(skipValidation: $this->elementIsNew); return $element; } diff --git a/tests/Behat/features/validation-failure.feature b/tests/Behat/features/validation-failure.feature index 1ad6b207..893820e8 100644 --- a/tests/Behat/features/validation-failure.feature +++ b/tests/Behat/features/validation-failure.feature @@ -50,3 +50,12 @@ Feature: Don't lose content when page or block is invalid Then the "Content" field for block 1 should contain "New sample content" And the "Title" field for block 1 should contain "Charlie's Block" And I should see the ".element-editor-header__version-state--draft" element + + Scenario: New blocks don't automatically trigger validation errors + Given I add an extension "DNADesign\Elemental\Tests\Src\ValidationFailedExtension" to the "DNADesign\Elemental\Models\BaseElement" class + And I go to "/admin/pages" + And I left click on "Blocks Page" in the tree + Then I should see a list of blocks + When I press the "Add block" button + Then I press the "Content" button in the add block popover + Then I should see "Untitled Content block" as the title for block 2