Skip to content

Commit

Permalink
ENH Skip validation when creating a new elemental block
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Apr 18, 2024
1 parent 1512b17 commit 50421f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/GraphQL/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function resolveAddElementToArea(
$reorderer = Injector::inst()->create(ReorderElements::class, $newElement);
$reorderer->reorder($afterElementID); // also writes the element
} else {
$newElement->write();
$newElement->write(skipValidation: true);
}

return $newElement;
Expand Down
11 changes: 8 additions & 3 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ 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;
Expand Down Expand Up @@ -553,7 +558,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));
Expand Down
9 changes: 9 additions & 0 deletions tests/Behat/features/validation-failure.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 50421f2

Please sign in to comment.