Skip to content

Commit

Permalink
Fallback blocks should not lose data once saved
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Feb 26, 2024
1 parent 0749fa6 commit 8591f89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions application/src/Api/Adapter/SitePageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,16 @@ private function hydrateBlocks(array $blockData, SitePage $page, ErrorStore $err
return;
}
if (!is_array($inputBlock['o:data'])) {
$errorStore->addError('o:block', 'Block data must not be a scalar value.'); // @translate
return;
// Attempt to convert the data to an array before returning with
// an error. This is needed in part so fallback blocks don't
// lose their data onced saved.
$blockData = json_decode($inputBlock['o:data'], true);
if (is_array($blockData)) {
$inputBlock['o:data'] = $blockData;
} else {
$errorStore->addError('o:block', 'Block data must not be a scalar value.'); // @translate
return;
}
}

$block->setLayout($inputBlock['o:layout']);
Expand Down
6 changes: 5 additions & 1 deletion application/src/Site/BlockLayout/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Omeka\Stdlib\Message;
use Laminas\Form\Element;
use Laminas\View\Renderer\PhpRenderer;

class Fallback extends AbstractBlockLayout
Expand Down Expand Up @@ -34,7 +35,10 @@ public function getLabel()
public function form(PhpRenderer $view, SiteRepresentation $site,
SitePageRepresentation $page = null, SitePageBlockRepresentation $block = null
) {
return $view->translate('This layout is invalid.');
// Preserve the original data.
$element = new Element\Hidden("o:block[__blockIndex__][o:data]");
$element->setValue(json_encode($block->data()));
return $view->translate('This layout is invalid.') . $view->formElement($element);
}

public function render(PhpRenderer $view, SitePageBlockRepresentation $block)
Expand Down

0 comments on commit 8591f89

Please sign in to comment.