diff --git a/src/elements/SuperTableBlockElement.php b/src/elements/SuperTableBlockElement.php index 46a5f13..1da54c1 100644 --- a/src/elements/SuperTableBlockElement.php +++ b/src/elements/SuperTableBlockElement.php @@ -11,10 +11,13 @@ use craft\base\BlockElementInterface; use craft\base\Element; use craft\base\ElementInterface; +use craft\elements\db\ElementQueryInterface; use craft\helpers\ArrayHelper; use craft\helpers\Db; use craft\models\FieldLayout; +use Illuminate\Support\Collection; + use yii\base\Exception; use yii\base\InvalidConfigException; @@ -444,6 +447,20 @@ protected function cacheTags(): array ]; } + /** + * @inheritdoc + */ + public function getLocalized(): ElementQueryInterface|Collection + { + $query = parent::getLocalized(); + + if ($query instanceof SuperTableBlockQuery && $this->ownerId !== null) { + // Maintain the same ownerId for queried blocks + $query->ownerId($this->ownerId); + } + + return $query; + } // Private Methods // ========================================================================= diff --git a/src/fields/SuperTableField.php b/src/fields/SuperTableField.php index 6c2f125..6883f2a 100644 --- a/src/fields/SuperTableField.php +++ b/src/fields/SuperTableField.php @@ -864,7 +864,10 @@ public function validateBlocks(ElementInterface $element): void if ( $element->getScenario() === Element::SCENARIO_LIVE && - ($this->minRows || $this->maxRows) + ($this->minRows || $this->maxRows) && + // Don't check static fields, since unedited static fields on new entries, or that were newly added to an + // existing entry's field layout, that had minRows/maxRows set would cause the array validator to fail + !$this->staticField ) { $arrayValidator = new ArrayValidator([ 'min' => $this->minRows ?: null, diff --git a/src/services/Service.php b/src/services/Service.php index 7f3f998..a899215 100644 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -731,7 +731,11 @@ public function saveField(SuperTableField $field, ElementInterface $owner): void foreach ($blocks as $block) { $sortOrder++; if ($saveAll || !$block->id || $block->dirty) { - $block->primaryOwnerId = $block->ownerId = $owner->id; + $block->setOwner($owner); + // If the block already has an ID and primary owner ID, don't reassign it + if (!$block->id || !$block->primaryOwnerId) { + $block->primaryOwnerId = $owner->id; + } $block->sortOrder = $sortOrder; $elementsService->saveElement($block, false); diff --git a/src/templates/settings.html b/src/templates/settings.html index 55a7639..855bcb2 100755 --- a/src/templates/settings.html +++ b/src/templates/settings.html @@ -265,35 +265,37 @@