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

Display the Set field correctly in the Editor when new field is added after saving record #1471

Merged
merged 5 commits into from
Jun 15, 2020
Merged
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
8 changes: 7 additions & 1 deletion src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,13 @@ private function updateField(Field $field, $value, ?string $locale): void

if ($field instanceof SetField) {
foreach ($value as $name => $svalue) {
$child = $field->getValue()[$name];
$child = $field->getValueForEditor()[$name];

if (! $child->getId()) {
$child->setParent($field);
$field->getContent()->addField($child);
}

$child->setDefinition($child->getName(), $field->getDefinition()->get('fields')->get($child->getName()));
$this->updateField($child, $svalue, $locale);
}
Expand Down
45 changes: 27 additions & 18 deletions src/Entity/Field/SetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,8 @@ class SetField extends Field implements FieldInterface, FieldParentInterface, Li

public const TYPE = 'set';

public function getValue(): array
public function getValue(): ?array
{
if (empty($this->fields)) {
// create new ones from the definition
$fieldDefinitions = $this->getDefinition()->get('fields');

if (! is_iterable($fieldDefinitions)) {
return [];
}

$newFields = [];
foreach ($fieldDefinitions as $name => $definition) {
$newFields[] = FieldRepository::factory($definition, $name);
}
$this->setValue($newFields);
}

return $this->fields;
}

Expand Down Expand Up @@ -94,10 +79,34 @@ public function getApiValue()
return $result;
}

public function getValueForEditor(): array
{
$fieldsFromDefinition = $this->getFieldsFromDefinition();

return array_merge($fieldsFromDefinition, $this->getDefaultValue(), $this->getValue());
}

private function getFieldsFromDefinition(): array
{
// create new fields from the definition
$fieldDefinitions = $this->getDefinition()->get('fields');

if (! is_iterable($fieldDefinitions)) {
return [];
}

$fields = [];
foreach ($fieldDefinitions as $name => $definition) {
$fields[$name] = FieldRepository::factory($definition, $name);
}

return $fields;
}

public function getDefaultValue()
{
$defaultValues = parent::getDefaultValue();
$value = $this->getValue();
$defaultValues = parent::getDefaultValue() ?? [];
$value = $this->getFieldsFromDefinition();

foreach ($defaultValues as $name => $default) {
if (array_key_exists($name, $value)) {
Expand Down
2 changes: 1 addition & 1 deletion templates/_partials/fields/set.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends '@bolt/_partials/fields/_base.html.twig' %}

{% block field %}
{% for setChild in field.value %}
{% for setChild in field.valueForEditor %}
{% if in_collection is defined %}
{% set setChildName = name ~ '[' ~ setChild.name ~ ']' %}
{% set setChildId = id ~ '-' ~ setChild.name|default('unnamed') %}
Expand Down