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

FIX Ensure files can be removed from elemental blocks #1267

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: 3 additions & 1 deletion src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public function save(array $data, Form $form): HTTPResponse
// Remove the namespace prefixes that were added by EditFormFactory
$dataWithoutNamespaces = static::removeNamespacesFromFields($data, $element->ID);

// Update and write the data object which will trigger model validation
// Update and write the data object which will trigger model validation.
// Would usually be handled by $form->saveInto($element) but since the field names
// in the form have been namespaced, we need to handle it ourselves.
$element->updateFromFormData($dataWithoutNamespaces);
Comment on lines +142 to 144
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really we should just rename the fields in the form, and then call $form->saveInto($element) - but doing that now would be an API break because someone may have customised the updateFromFormData() method in their custom elements.

if ($element->isChanged()) {
try {
Expand Down
12 changes: 3 additions & 9 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,9 @@ public function getRenderTemplates($suffix = '')
*/
public function updateFromFormData($data)
{
$cmsFields = $this->getCMSFields();

foreach ($data as $field => $datum) {
$field = $cmsFields->dataFieldByName($field);

if (!$field) {
continue;
}

$cmsFields = $this->getCMSFields()->saveableFields();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the saveableFields() explicitly - things like LiteralField don't need to be touched here.

foreach ($cmsFields as $fieldName => $field) {
$datum = $data[$fieldName] ?? null;
Comment on lines +660 to +661
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's data, set to the value in the data - otherwise set to null.

This is still skipping a bunch of stuff that $form->saveInto() would do, but fully replicating that logic isn't in scope here. This change is the least disruptive change required to fix the bug.

$field->setSubmittedValue($datum);
$field->saveInto($this);
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Behat/features/file-upload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@retry @job2
Feature: Files can be saved in and removed from elemental blocks
As a CMS user
I want to attach and remove files from elemental blocks

Background:
Given I add an extension "DNADesign\Elemental\Extensions\ElementalPageExtension" to the "Page" class
And I add an extension "SilverStripe\FrameworkTest\Elemental\Extension\FileElementalExtension" to the "DNADesign\Elemental\Models\ElementContent" class
And I go to "/dev/build?flush"
And a "image" "file1.jpg"
And a "page" "Blocks Page" with a "My title" content element with "My content" content
And the "group" "EDITOR" has permissions "Access to 'Pages' section"
And I am logged in as a member of "EDITOR" group
And I go to "/admin/pages"
And I follow "Blocks Page"

Scenario: Add a file and save the block, then remove the file and save the block
# Add a file to the block
Given I click on the caret button for block 1
Then I should not see "file1"
Given I take a screenshot after every step
When I click "Choose existing" in the "#Form_ElementForm_1 .uploadfield" element
And I press the "Back" HTML field button
And I click on the file named "file1" in the gallery
And I press the "Insert" button
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see a "Saved 'My title' successfully" success toast
# Check we see the file both in the current page load (react state is correct) and after reloading the form
Then I should see "file1"
When I go to "/admin/pages"
And I follow "Blocks Page"
And I click on the caret button for block 1
Then I should see "file1"
# Then remove the file from the block
And I click on the "#Form_ElementForm_1 .uploadfield-item__remove-btn" element
And I press the "View actions" button
And I click on the ".element-editor__actions-save" element
Then I should see a "Saved 'My title' successfully" success toast
# Check we don't see the file anymore
Then I should not see "file1"
When I go to "/admin/pages"
And I follow "Blocks Page"
And I click on the caret button for block 1
Then I should not see "file1"