From b0009197368ed16a870ce99d2779c414cb2e844d Mon Sep 17 00:00:00 2001 From: Thomas Templeton Date: Wed, 28 Aug 2024 20:04:28 +1000 Subject: [PATCH] Fix #932 --- CHANGELOG.md | 1 + src/elements/conditions/BlockCondition.php | 27 ++++++++++++++++++- .../fields/ParentFieldConditionRuleTrait.php | 8 +++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce467923..5d33ed23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Fixed a bug where moving an entry between sections would cause nested Neo content to lose structure data - Fixed an error that occurred when converting a block type to an entry type (including during Neo-to-Matrix conversion) if any block type field layout tabs or elements had condition rules applied +- Fixed an error that occurred when setting parent block condition rules on block type field layout elements - Fixed a style bug that occurred when input blocks had UI elements ## 5.2.2 - 2024-08-20 diff --git a/src/elements/conditions/BlockCondition.php b/src/elements/conditions/BlockCondition.php index 144bfd2a..3ee35f66 100644 --- a/src/elements/conditions/BlockCondition.php +++ b/src/elements/conditions/BlockCondition.php @@ -3,8 +3,10 @@ namespace benf\neo\elements\conditions; use benf\neo\Plugin as Neo; +use craft\db\Table; use craft\elements\conditions\ElementCondition; use craft\elements\conditions\LevelConditionRule; +use craft\helpers\Db; use craft\models\FieldLayout; /** @@ -16,6 +18,25 @@ */ class BlockCondition extends ElementCondition { + /** + * @inheritdoc + */ + public function getBuilderConfig(): array + { + $config = parent::getBuilderConfig(); + + // Ensure UUIDs set on field layouts + if (isset($config['fieldLayouts'])) { + $fieldLayouts = $this->getFieldLayouts(); + + for ($i = 0; $i < count($fieldLayouts); $i++) { + $config['fieldLayouts'][$i]['uid'] = $fieldLayouts[$i]->uid; + } + } + + return $config; + } + /** * @inheritdoc */ @@ -26,8 +47,12 @@ protected function selectableConditionRules(): array // Get all field layouts associated with this object's associated Neo field(s), then temporarily replace this // object's field layouts so we get all possible parent block condition rules + $layoutIds = array_values(Db::idsByUids( + Table::FIELDLAYOUTS, + array_map(fn($layout) => $layout->uid, $this->getFieldLayouts()), + )); $layoutBlockTypes = Neo::$plugin->blockTypes->getByCriteria([ - 'fieldLayoutId' => array_map(fn($layout) => $layout->id, $this->getFieldLayouts()), + 'fieldLayoutId' => $layoutIds, ]); $fieldBlockTypes = Neo::$plugin->blockTypes->getByCriteria([ 'fieldId' => array_values(array_unique(array_map(fn($blockType) => $blockType->fieldId, $layoutBlockTypes))), diff --git a/src/elements/conditions/fields/ParentFieldConditionRuleTrait.php b/src/elements/conditions/fields/ParentFieldConditionRuleTrait.php index 9d78c34a..9feabbed 100644 --- a/src/elements/conditions/fields/ParentFieldConditionRuleTrait.php +++ b/src/elements/conditions/fields/ParentFieldConditionRuleTrait.php @@ -6,6 +6,8 @@ use Craft; use craft\base\ElementInterface; use craft\base\FieldInterface; +use craft\db\Table; +use craft\helpers\Db; use yii\base\InvalidConfigException; /** @@ -71,8 +73,12 @@ protected function fieldInstances(): array $selectedInstanceLabel = null; // Get all of the block type field layouts associated with the Neo field(s) + $layoutIds = array_values(Db::idsByUids( + Table::FIELDLAYOUTS, + array_map(fn($layout) => $layout->uid, $this->getCondition()->getFieldLayouts()), + )); $layoutBlockTypes = Neo::$plugin->blockTypes->getByCriteria([ - 'fieldLayoutId' => array_map(fn($layout) => $layout->id, $this->getCondition()->getFieldLayouts()), + 'fieldLayoutId' => $layoutIds, ]); $fieldBlockTypes = Neo::$plugin->blockTypes->getByCriteria([ 'fieldId' => array_values(array_unique(array_map(fn($blockType) => $blockType->fieldId, $layoutBlockTypes))),