Skip to content

Commit

Permalink
Merge pull request #1429 from bolt/bugfix/collections-not-showing
Browse files Browse the repository at this point in the history
Fix collections visibility
  • Loading branch information
I-Valchev authored Jun 2, 2020
2 parents 7b55fa9 + 42300f3 commit 74e0024
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 59 deletions.
6 changes: 3 additions & 3 deletions src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ private function contentFromPost(?Content $content, Request $request): Content

private function removeFieldChildren(Content $content, FieldParentInterface $field): void
{
foreach ($field->getChildren() as $child) {
if ($child instanceof FieldParentInterface && $child->hasChildren()) {
foreach ($field->getValue() as $child) {
if ($child instanceof FieldParentInterface && ! empty($child->getValue())) {
$this->removeFieldChildren($content, $child);
}

Expand Down Expand Up @@ -435,7 +435,7 @@ private function updateField(Field $field, $value, ?string $locale): void

if ($field instanceof SetField) {
foreach ($value as $name => $svalue) {
$child = $field->getChild($name);
$child = $field->getValue()[$name];
$child->setDefinition($child->getName(), $field->getDefinition()->get('fields')->get($child->getName()));
$this->updateField($child, $svalue, $locale);
}
Expand Down
12 changes: 2 additions & 10 deletions src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,11 @@ public function getApiValue()

public function getValue(): ?array
{
$value = $this->translate($this->getCurrentLocale(), false)->getValue();

// If the field is not translatable, return the value without fallback to defaultLocale
if ($this->isTranslatable()) {
return $value;
}

// If value is empty, get the defaultLocale as fallback.
if (empty($value)) {
$value = $this->translate($this->getDefaultLocale(), false)->getValue();
return $this->translate($this->getCurrentLocale(), false)->getValue();
}

return $value;
return $this->translate($this->getDefaultLocale(), false)->getValue();
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Entity/Field/CollectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ public function setValue($fields): Field
}
}

parent::setValue($fields);
$this->fields = $fields;

return $this;
}

public function getValue(): ?array
{
return $this->fields;
}

public function getDefaultValue()
{
$default = parent::getDefaultValue();
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/Field/SetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SetField extends Field implements FieldInterface, FieldParentInterface

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

Expand All @@ -39,7 +39,7 @@ public function getValue(): array
$this->setValue($newFields);
}

return parent::getValue();
return $this->fields;
}

public function setValue($fields): Field
Expand All @@ -61,7 +61,7 @@ public function setValue($fields): Field
// Sorts the fields in the order specified in the definition
$value = array_merge(array_flip(array_intersect(array_keys($definedFields), array_keys($value))), $value);

parent::setValue($value);
$this->fields = $value;

return $this;
}
Expand Down
8 changes: 2 additions & 6 deletions src/Entity/FieldParentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
*/
interface FieldParentInterface
{
public function getChild(string $fieldName): Field;
public function setLocale(string $locale): Field;

public function hasChild(string $fieldName): bool;

public function hasChildren(): bool;

public function getChildren(): array;
public function getValue(): ?array;
}
40 changes: 7 additions & 33 deletions src/Entity/FieldParentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,18 @@
*/
trait FieldParentTrait
{
abstract public function getContent(): ?Content;

public function getChild(string $fieldName): Field
{
return collect($this->getValue())->filter(function (Field $field) use ($fieldName) {
return $field->getName() === $fieldName;
})->first();
}

public function hasChild(string $fieldName): bool
{
$query = collect($this->getValue())->filter(function (Field $field) use ($fieldName) {
return $field->getName() === $fieldName;
});

return ! $query->isEmpty();
}
/** @var array Field */
private $fields = [];

public function hasChildren(): bool
{
$query = collect($this->getValue())->filter(function (Field $field) {
return $field->getParent() === $this;
});

return ! $query->isEmpty();
}

public function getChildren(): array
{
return $this->getValue();
}
abstract public function getContent(): ?Content;

public function setLocale(?string $locale): Field
{
parent::setLocale($locale);
/** @var Field $child */
foreach ($this->getChildren() as $child) {
$child->setLocale($locale);
foreach ($this->getValue() as $child) {
if ($child instanceof Field) {
$child->setLocale($locale);
}
}

return $this;
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/TranslationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function applyTranslations(Field $field, string $collectionName, $orderId
}
} else {
/** @var Field $child */
foreach ($field->getChildren() as $child) {
foreach ($field->getValue() as $child) {
$this->applyTranslations($child, $collectionName, $orderId);
}
}
Expand All @@ -45,8 +45,8 @@ public function applyTranslations(Field $field, string $collectionName, $orderId
private function getFieldChildrenTranslations(array &$translations, FieldParentInterface $field): void
{
/** @var Field $child */
foreach ($field->getChildren() as $child) {
if ($child instanceof FieldParentInterface && $child->hasChildren()) {
foreach ($field->getValue() as $child) {
if ($child instanceof FieldParentInterface && ! empty($child->getValue())) {
$this->getFieldChildrenTranslations($translations, $child);
}

Expand Down

0 comments on commit 74e0024

Please sign in to comment.