Skip to content

Commit

Permalink
Merge branch 'craft-4' of https://github.com/verbb/super-table into c…
Browse files Browse the repository at this point in the history
…raft-4
  • Loading branch information
engram-design committed May 24, 2023
2 parents 58d9a10 + 2c4e0a9 commit 99b6540
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
17 changes: 17 additions & 0 deletions src/elements/SuperTableBlockElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
// =========================================================================
Expand Down
5 changes: 4 additions & 1 deletion src/fields/SuperTableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
57 changes: 30 additions & 27 deletions src/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,40 +265,43 @@ <h5>{{ "Field Settings" | t('app') }}</h5>
{% 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')
}) }}
<div id="row-settings">
{{ 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')
}) }}
</div>

{{ forms.lightswitchField({
label: "Static field" | t('super-table'),
instructions: "A static field will only display a single row of fields." | t('super-table'),
id: 'staticField',
name: 'staticField',
on: supertableField.staticField,
reverseToggle: 'row-settings',
}) }}

0 comments on commit 99b6540

Please sign in to comment.