From 65a8c122f785414c451eed38cb8e2c68e1be23fd Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Fri, 13 Dec 2019 16:31:15 +0100 Subject: [PATCH 1/5] makes set return empty array if definition is null --- src/Entity/Field/SetField.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Entity/Field/SetField.php b/src/Entity/Field/SetField.php index 1ba24b55..55f36623 100644 --- a/src/Entity/Field/SetField.php +++ b/src/Entity/Field/SetField.php @@ -32,8 +32,10 @@ class SetField extends Field implements FieldInterface $hash = $this->getHash(); $fieldDefinitions = $this->getDefinition()->get('fields'); $result = []; - $i = 0; + if(! is_array($fieldDefinitions)) return $result; + + $i = 0; foreach ($fieldDefinitions as $fieldName => $fieldDefinition) { $currentSetFieldName = $hash . '::' . $fieldName; if ($this->getContent() && $this->getContent()->hasField($currentSetFieldName)) { From 07baae090ef284d1ce86b48b18cae254a0d68d1e Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Fri, 13 Dec 2019 16:43:14 +0100 Subject: [PATCH 2/5] csfix --- src/Entity/Field/SetField.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Entity/Field/SetField.php b/src/Entity/Field/SetField.php index 55f36623..35f1a8e3 100644 --- a/src/Entity/Field/SetField.php +++ b/src/Entity/Field/SetField.php @@ -33,7 +33,9 @@ class SetField extends Field implements FieldInterface $fieldDefinitions = $this->getDefinition()->get('fields'); $result = []; - if(! is_array($fieldDefinitions)) return $result; + if (! is_array($fieldDefinitions)) { + return $result; + } $i = 0; foreach ($fieldDefinitions as $fieldName => $fieldDefinition) { From 0c518106a74f20858e4fc9243f197ea4054c8c2d Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Fri, 13 Dec 2019 17:18:38 +0100 Subject: [PATCH 3/5] some fix --- .../Backend/ContentEditController.php | 24 ++++++++-------- src/DataFixtures/ContentFixtures.php | 3 +- src/Entity/Field/CollectionField.php | 28 ++++++++++--------- src/Entity/Field/SetField.php | 6 +--- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/Controller/Backend/ContentEditController.php b/src/Controller/Backend/ContentEditController.php index cda7ccc0..21d1fcf9 100644 --- a/src/Controller/Backend/ContentEditController.php +++ b/src/Controller/Backend/ContentEditController.php @@ -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 diff --git a/src/DataFixtures/ContentFixtures.php b/src/DataFixtures/ContentFixtures.php index 58b92a41..0907fd26 100644 --- a/src/DataFixtures/ContentFixtures.php +++ b/src/DataFixtures/ContentFixtures.php @@ -199,13 +199,14 @@ 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) { diff --git a/src/Entity/Field/CollectionField.php b/src/Entity/Field/CollectionField.php index 80584807..26464fe3 100644 --- a/src/Entity/Field/CollectionField.php +++ b/src/Entity/Field/CollectionField.php @@ -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++; } diff --git a/src/Entity/Field/SetField.php b/src/Entity/Field/SetField.php index 35f1a8e3..1ba24b55 100644 --- a/src/Entity/Field/SetField.php +++ b/src/Entity/Field/SetField.php @@ -32,12 +32,8 @@ class SetField extends Field implements FieldInterface $hash = $this->getHash(); $fieldDefinitions = $this->getDefinition()->get('fields'); $result = []; - - if (! is_array($fieldDefinitions)) { - return $result; - } - $i = 0; + foreach ($fieldDefinitions as $fieldName => $fieldDefinition) { $currentSetFieldName = $hash . '::' . $fieldName; if ($this->getContent() && $this->getContent()->hasField($currentSetFieldName)) { From b8ce2219d118ca4f0d8cf5f41932e014d9ce2cca Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Fri, 13 Dec 2019 17:19:34 +0100 Subject: [PATCH 4/5] cxfix --- src/DataFixtures/ContentFixtures.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/DataFixtures/ContentFixtures.php b/src/DataFixtures/ContentFixtures.php index 0907fd26..9f5e3c2d 100644 --- a/src/DataFixtures/ContentFixtures.php +++ b/src/DataFixtures/ContentFixtures.php @@ -206,7 +206,9 @@ class ContentFixtures extends BaseFixture implements DependentFixtureInterface, } $field->setSortorder($sortorder++ * 5); - if(!isset($ignoreField)) $content->addField($field); + if (! isset($ignoreField)) { + $content->addField($field); + } if (isset($fieldType['localize']) && $fieldType['localize']) { foreach ($contentType['locales'] as $locale) { From 5f0231d28a436b0f5da00e1811643502489dd356 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Tue, 17 Dec 2019 09:12:59 +0100 Subject: [PATCH 5/5] fix comments! :eyes: --- src/Entity/Field/CollectionField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Entity/Field/CollectionField.php b/src/Entity/Field/CollectionField.php index 26464fe3..1926c927 100644 --- a/src/Entity/Field/CollectionField.php +++ b/src/Entity/Field/CollectionField.php @@ -41,9 +41,9 @@ class CollectionField extends Field implements FieldInterface } 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 + //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']]);