Merge pull request #796 from bolt/bugfix/ignore-set-field-in-collection

Do not persist set field for set in collection
This commit is contained in:
Bob den Otter
2019-12-17 09:57:09 +01:00
committed by GitHub
3 changed files with 30 additions and 27 deletions

View File

@@ -291,7 +291,15 @@ class ContentEditController extends TwigAwareController implements BackendZone
foreach ($formData['sets'] as $setName => $set) {
foreach ($set as $hash => $setFields) {
$setDefinition = $content->getDefinition()->get('fields')->get($setName);
$this->updateSet($content, $setName, $setDefinition, $hash, $setFields, $locale);
$this->updateSetItems($content, $setDefinition, $hash, $setFields, $locale);
if ($content->hasField($setName)) {
$field = $content->getField($setName);
$field->setValue($hash);
} else {
$field = Field::factory($setDefinition, $setName);
$field->setValue($hash);
$content->addField($field);
}
}
}
}
@@ -311,8 +319,7 @@ class ContentEditController extends TwigAwareController implements BackendZone
if ($collectionItemDefinition['type'] === 'set') {
// if this is a set field, create fields for each field within the set
foreach ($collectionItemValue as $hash => $fieldValue) {
$fieldDBname = $collection . '::' . $collectionItemName;
$this->updateSet($content, $fieldDBname, $collectionItemDefinition, $hash, $fieldValue, $locale);
$this->updateSetItems($content, $collectionItemDefinition, $hash, $fieldValue, $locale);
}
} else {
// if this is any other field
@@ -367,7 +374,7 @@ class ContentEditController extends TwigAwareController implements BackendZone
return $content;
}
private function updateSet(?Content $content, string $setName, ContentType $setDefinition, string $hash, array $setFields, ?string $locale): void
private function updateSetItems(?Content $content, ContentType $setDefinition, string $hash, array $setFields, ?string $locale): void
{
foreach ($setFields as $setFieldChildName => $setFieldChildValue) {
$setFieldChildDBName = $hash . '::' . $setFieldChildName;
@@ -384,15 +391,6 @@ class ContentEditController extends TwigAwareController implements BackendZone
$this->updateField($setFieldChildField, $setFieldChildValue, $locale);
}
if ($content->hasField($setName)) {
$field = $content->getField($setName);
$field->setValue($hash);
} else {
$field = Field::factory($setDefinition, $setName);
$field->setValue($hash);
$content->addField($field);
}
}
private function getFieldToUpdate(Content $content, string $fieldName, $fieldDefinition = ''): Field

View File

@@ -199,13 +199,16 @@ class ContentFixtures extends BaseFixture implements DependentFixtureInterface,
$field = $this->loadCollectionField($content, $field, $fieldType, $contentType, $preset, $translationRepository);
} elseif ($fieldType['type'] === 'set') {
$field = $this->loadSetField($content, $field, $contentType, $preset, $translationRepository);
$ignoreField = true;
} else {
$field->setValue($this->getValuesforFieldType($name, $fieldType, $contentType['singleton']));
}
}
$field->setSortorder($sortorder++ * 5);
$content->addField($field);
if (! isset($ignoreField)) {
$content->addField($field);
}
if (isset($fieldType['localize']) && $fieldType['localize']) {
foreach ($contentType['locales'] as $locale) {

View File

@@ -32,21 +32,23 @@ class CollectionField extends Field implements FieldInterface
$i = 0;
foreach ($thisFieldValues as $thisFieldValue) {
$fieldDBname = $this->getName() . '::' . $thisFieldValue['field_name'];
$field = $this->getContent()->getField($fieldDBname);
// The field value persists ALL the values for the same type collection items (e.g. all 'ages') in an array
// To display the value for the current item, we set the value for the specific key only
// As $this->getValue() is called multiple times, clone the object to ensure $field->setValue() is called once per instance
$field = clone $field;
$field->setName($thisFieldValue['field_name']);
$field->setDefinition($thisFieldValue['field_name'], $this->getDefinition()->get('fields')[$thisFieldValue['field_name']]);
if ($thisFieldValue['field_type'] !== 'set') {
//all collection item fields, except sets, have a different value than if they were outside of a collection
if ($thisFieldValue['field_type'] === 'set') {
$field = new SetField();
$field->setContent($this->getContent());
$field->setValue($thisFieldValue['field_reference']);
$field->setDefinition($thisFieldValue['field_name'], $this->getDefinition()->get('fields')[$thisFieldValue['field_name']]);
$field->setName($thisFieldValue['field_name']);
} else {
$fieldDBname = $this->getName() . '::' . $thisFieldValue['field_name'];
$field = $this->getContent()->getField($fieldDBname);
//The field value persists ALL the values for the same type collection items (e.g. all 'ages') in an array
//To display the value for the current item, we set the value for the specific key only
//As $this->getValue() is called multiple times, clone the object to ensure $field->setValue() is called once per instance
$field = clone $field;
$field->setName($thisFieldValue['field_name']);
$field->setValue($field->getValue()[$thisFieldValue['field_reference']]);
$field->setDefinition($thisFieldValue['field_name'], $this->getDefinition()->get('fields')[$thisFieldValue['field_name']]);
}
$result['fields'][$i] = $field;
$i++;
}