Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Skip validation when creating a new elemental block #1172

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/GraphQL/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObjectSchema;
use SilverStripe\ORM\ValidationResult;

/**
* Class BaseElement
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 5 additions & 2 deletions src/Services/ReorderElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -32,6 +34,7 @@ public function __construct(BaseElement $element)
));
}

$this->elementIsNew = $elementIsNew;
$this->setElement($element);
}

Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Behat/features/validation-failure.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ 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 1
And I should see "Alice's Block" as the title for block 2
Loading