Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Tag List default value and useKey option support #1224

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/backend/classes/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
class FormField
{
/**
* @var int Value returned when the form field should not contribute any save data.
* @var string Value returned when the form field should not contribute any save data.
*/
const NO_SAVE_DATA = -1;
const NO_SAVE_DATA = "\0";

/**
* @var string A special character in yaml config files to indicate a field higher in hierarchy
Expand Down
46 changes: 32 additions & 14 deletions modules/backend/formwidgets/TagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TagList extends FormWidgetBase
//

/**
* @var string Tag separator: space, comma.
* @var string Tag separator: space, comma, semicolon.
*/
public $separator = 'comma';

Expand Down Expand Up @@ -123,23 +123,22 @@ public function getSaveValue($value)
* Returns an array suitable for saving against a relation (array of keys).
* This method also creates non-existent tags.
*/
protected function hydrateRelationSaveValue($names): ?array
protected function hydrateRelationSaveValue($items): ?array
{
if (!$names) {
return $names;
if (empty($items)) {
return [];
}

if (!is_array($names)) {
$names = [$names];
if (!is_array($items)) {
$items = [$items];
}

$relationModel = $this->getRelationModel();
$existingTags = $relationModel
->whereIn($this->nameFrom, $names)
->lists($this->nameFrom, $relationModel->getKeyName())
;
->whereIn(($this->useKey) ? $relationModel->getKeyName() : $this->nameFrom, $items)
->lists($this->nameFrom, $relationModel->getKeyName());

$newTags = $this->customTags ? array_diff($names, $existingTags) : [];
$newTags = $this->customTags ? array_diff($items, $existingTags) : [];

foreach ($newTags as $newTag) {
$newModel = new $relationModel;
Expand All @@ -159,7 +158,24 @@ public function getLoadValue()
$value = parent::getLoadValue();

if ($this->mode === static::MODE_RELATION) {
return $this->getRelationObject()->lists($this->nameFrom);
// Load default if necessary
if (!$this->model->exists) {
$default = $this->formField->getDefaultFromData($this->data ?: $this->model);

if (!is_null($default)) {
return ($this->useKey)
? $this->getRelationModel()->whereIn($this->getRelationModel()->getKeyName(), $default)->lists($this->getRelationModel()->getKeyName())
: $this->getRelationModel()->whereIn($this->nameFrom, $default)->lists($this->nameFrom);
}
}

if (empty($value)) {
return [];
}

return $this->getRelationModel()->whereIn($this->getRelationModel()->getKeyName(), $value)->lists(($this->useKey)
? $this->getRelationModel()->getKeyName()
: $this->nameFrom);
}

return $this->mode === static::MODE_STRING
Expand All @@ -183,7 +199,7 @@ public function getFieldOptions()
// by joining its pivot table. Remove all joins from the query.
$query->getQuery()->getQuery()->joins = [];

return $query->lists($this->nameFrom);
return $query->lists($this->nameFrom, ($this->useKey) ? $this->getRelationModel()->getKeyName() : null);
});
}

Expand Down Expand Up @@ -214,10 +230,12 @@ protected function getCustomSeparators()
protected function getSeparatorCharacter()
{
switch (strtolower($this->separator)) {
case 'comma':
return ',';
case 'semicolon':
return ';';
case 'space':
return ' ';
default:
return ',';
}
}
}
19 changes: 5 additions & 14 deletions modules/backend/formwidgets/taglist/partials/_taglist.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
$selectedValues = is_array($selectedValues) ? $selectedValues : [];
$availableOptions = $useKey ? $fieldOptions : array_unique(array_merge($selectedValues, $fieldOptions));
$availableOptions = $fieldOptions;
$displayOnlyOptions = [];

foreach ($availableOptions as $key => $option) {
Expand All @@ -19,20 +19,11 @@
<li class="taglist__item"><?= e(trans($option)) ?></li>
<?php endforeach ?>
</ul>
<?php if (is_array($field->value)): ?>
<?php foreach ($displayOnlyOptions as $option): ?>
<input
type="hidden"
name="<?= $field->getName() ?>[]"
value="<?= $option ?>">
<?php endforeach ?>
<?php else: ?>
<input
type="hidden"
name="<?= $field->getName() ?>"
value="<?= $field->value ?>">
<?php endif ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed? Isn't it needed for trigger support in preview / readonly contexts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so that's what it's for. I'll re-add.

<?php else: ?>
<input
type="hidden"
name="<?= $field->getName() ?>"
value="">
<select
id="<?= $field->getId() ?>"
name="<?= $field->getName() ?>[]"
Expand Down
Loading