Skip to content

Commit

Permalink
Merge branch '4.x' into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Jun 24, 2024
2 parents b3ee603 + c0f8213 commit 472d022
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
- Removed the `neoblocks_owners` table; the Craft 5 `elements_owners` table is used instead
- Removed the `neoblocks.deletedWithOwner` column; the Craft 5 `elements.deletedWithOwner` column is used instead

## 4.2.5 - 2024-06-24

### Fixed
- Fixed a bug where content migrations that set Neo field values could fail to set more than one block in some cases

## 4.2.4 - 2024-06-18

### Fixed
- Fixed a bug where setting Craft's `autosaveDrafts` general config setting to `false` would cause Neo blocks created on new entries to lose their content when saved

## 4.2.3 - 2024-06-18

### Fixed
Expand Down
9 changes: 7 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1584,8 +1584,13 @@ private function _createBlocksFromSerializedData(array $value, ElementInterface
$baseBlockFieldNamespace .= '.blocks';
}
} else {
$newBlockData = $value;
$newSortOrder = array_keys($value);
$newBlockData = [];
$newSortOrder = [];

foreach ($value as $i => $blockData) {
$newBlockData["new$i"] = $blockData;
$newSortOrder[] = "new$i";
}
}

$blocks = [];
Expand Down
36 changes: 23 additions & 13 deletions src/controllers/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function actionRenderBlocks(): Response
$fieldId = $request->getRequiredBodyParam('fieldId');
$siteId = $request->getParam('siteId');
$namespace = $request->getParam('namespace');
$unsavedIds = array_flip($request->getParam('unsavedIds', []));
$nextUnsavedId = 0;

// Remove the ending section from the namespace, since we're adding it back in later
if (($whereToStop = strrpos($namespace, '[')) !== false) {
Expand All @@ -99,6 +101,7 @@ public function actionRenderBlocks(): Response

$field = Craft::$app->getFields()->getFieldById($fieldId);
$renderedBlocks = [];
$autosaveDrafts = Craft::$app->getConfig()->getGeneral()->autosaveDrafts;

foreach ($blocks as $rawBlock) {
$type = Neo::$plugin->blockTypes->getById((int)$rawBlock['type']);
Expand All @@ -118,20 +121,27 @@ public function actionRenderBlocks(): Response
$block->setFieldValues($rawBlock['content']);
}

Craft::$app->getElements()->saveElement($block, false);
if ($autosaveDrafts) {
Craft::$app->getElements()->saveElement($block, false);

// If the owner supports drafts, temporarily save the block's position in the block structure before
// rendering the block template, so the block template shows the correct visible field layout elements
$structure = $elementsService->canCreateDrafts($block->getOwner()) && (isset($rawBlock['prevSiblingId']) || isset($rawBlock['parentId']))
? Neo::$plugin->blocks->getStructure($fieldId, $rawBlock['ownerId'], $siteId)?->getStructure()
: null;
// If the owner supports drafts, temporarily save the block's position in the block structure before
// rendering the block template, so the block template shows the correct visible field layout elements
$structure = $elementsService->canCreateDrafts($block->getOwner()) && (isset($rawBlock['prevSiblingId']) || isset($rawBlock['parentId']))
? Neo::$plugin->blocks->getStructure($fieldId, $rawBlock['ownerId'], $siteId)?->getStructure()
: null;

if ($structure !== null) {
if (isset($rawBlock['prevSiblingId'])) {
$structuresService->moveAfter($structure->id, $block, (int)$rawBlock['prevSiblingId']);
} elseif (isset($rawBlock['parentId'])) {
$structuresService->prepend($structure->id, $block, (int)$rawBlock['parentId']);
if ($structure !== null) {
if (isset($rawBlock['prevSiblingId'])) {
$structuresService->moveAfter($structure->id, $block, (int)$rawBlock['prevSiblingId']);
} elseif (isset($rawBlock['parentId'])) {
$structuresService->prepend($structure->id, $block, (int)$rawBlock['parentId']);
}
}
} else {
while (isset($unsavedIds[$nextUnsavedId])) {
$nextUnsavedId++;
}
$block->unsavedId = $nextUnsavedId++;
}

$html = $view->renderTemplate('neo/block.twig', [
Expand All @@ -140,7 +150,7 @@ public function actionRenderBlocks(): Response
'static' => false,
]);

if ($structure !== null) {
if ($autosaveDrafts && $structure !== null) {
$structuresService->remove($structure->id, $block);
}

Expand All @@ -152,7 +162,7 @@ public function actionRenderBlocks(): Response
'type' => (int)$type->id,
'level' => $block->level,
'enabled' => $block->enabled,
'id' => $block->id,
'id' => $block->id ?? "new{$block->unsavedId}",
'uuid' => $block->uid,
];
}
Expand Down
1 change: 1 addition & 0 deletions src/web/assets/input/src/scripts/BlockType.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default Garnish.Base.extend({
namespace: NS.toFieldName(),
fieldId: this._field?.getId(),
siteId: this._field?.getSiteId(),
unsavedIds: this._field?.getUnsavedIds(),
blocks: [Object.assign({
collapsed: false,
enabled: true,
Expand Down
10 changes: 10 additions & 0 deletions src/web/assets/input/src/scripts/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,15 @@ export default Garnish.Base.extend({
return this._siteId
},

/**
* @since 4.2.4
*/
getUnsavedIds () {
return this.$blocksContainer.find('.ni_block[data-neo-b-id^="new"]')
.map((_, element) => element.getAttribute('data-neo-b-id').substring(3))
.get()
},

_setMatrixClassErrors () {
// TODO: will need probably need to find a method within php instead of JS
// temp solution for now.
Expand Down Expand Up @@ -1090,6 +1099,7 @@ export default Garnish.Base.extend({
},

async _duplicate (data, block) {
data.unsavedIds = this.getUnsavedIds()
try {
this.$form.data('elementEditor')?.pause()
await this._addSpinnerAfter(block)
Expand Down

0 comments on commit 472d022

Please sign in to comment.