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 @@
{{ "Field Settings" | t('app') }}
{% endif %} {% endif %} -{{ forms.textField({ - label: "New Row Label" | t('super-table'), - instructions: "Enter the text you want to appear in the button to create a new row." | t('super-table'), - id: 'selectionLabel', - name: 'selectionLabel', - value: supertableField.selectionLabel, - placeholder: supertableField.defaultSelectionLabel(), - errors: supertableField.getErrors('selectionLabel') -}) }} +
+ {{ forms.textField({ + label: "New Row Label" | t('super-table'), + instructions: "Enter the text you want to appear in the button to create a new row." | t('super-table'), + id: 'selectionLabel', + name: 'selectionLabel', + value: supertableField.selectionLabel, + placeholder: supertableField.defaultSelectionLabel(), + errors: supertableField.getErrors('selectionLabel') + }) }} -{{ forms.textField({ - label: "Min Rows" | t('super-table'), - instructions: "The minimum number of rows the field must have." | t('super-table'), - id: 'minRows', - name: 'minRows', - value: supertableField.minRows, - size: 3, - errors: supertableField.getErrors('minRows') -}) }} + {{ forms.textField({ + label: "Min Rows" | t('super-table'), + instructions: "The minimum number of rows the field must have." | t('super-table'), + id: 'minRows', + name: 'minRows', + value: supertableField.minRows, + size: 3, + errors: supertableField.getErrors('minRows') + }) }} -{{ forms.textField({ - label: "Max Rows" | t('super-table'), - instructions: "The maximum number of rows the field is allowed to have." | t('super-table'), - id: 'maxRows', - name: 'maxRows', - value: supertableField.maxRows, - size: 3, - errors: supertableField.getErrors('maxRows') -}) }} + {{ forms.textField({ + label: "Max Rows" | t('super-table'), + instructions: "The maximum number of rows the field is allowed to have." | t('super-table'), + id: 'maxRows', + name: 'maxRows', + value: supertableField.maxRows, + size: 3, + errors: supertableField.getErrors('maxRows') + }) }} +
{{ forms.lightswitchField({ label: "Static field" | t('super-table'), @@ -301,4 +303,5 @@
{{ "Field Settings" | t('app') }}
id: 'staticField', name: 'staticField', on: supertableField.staticField, + reverseToggle: 'row-settings', }) }}