Skip to content

Commit

Permalink
Update FieldFillListener.php
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Valchev committed Jun 10, 2020
1 parent c0bb6d8 commit d1dc85b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Event/Listener/FieldFillListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Event\Listener;

use Bolt\Configuration\Content\FieldType;
use Bolt\Entity\Field;
use Bolt\Entity\Field\CollectionField;
use Bolt\Entity\Field\SetField;
Expand Down Expand Up @@ -43,7 +44,8 @@ public function postLoad(LifecycleEventArgs $args): void

public function fillSet(SetField $entity): void
{
$fields = $this->fields->findAllByParent($entity);
$definition = $entity->getDefinition();
$fields = $this->intersectFieldsAndDefinition($this->fields->findAllByParent($entity), $definition);

/** @var Field $field */
foreach ($fields as $field) {
Expand All @@ -56,14 +58,22 @@ public function fillSet(SetField $entity): void

public function fillCollection(CollectionField $entity): void
{
$fields = $this->fields->findAllByParent($entity);
$definition = $entity->getDefinition();
$fields = $this->intersectFieldsAndDefinition($this->fields->findAllByParent($entity), $definition);

/** @var Field $field */
foreach ($fields as $field) {
$definition = $entity->getDefinition()->get('fields')[$field->getName()] ?? new Collection();
$field->setDefinition($field->getName(), $definition);
$fieldDefiniton = $entity->getDefinition()->get('fields')[$field->getName()] ?? new Collection();
$field->setDefinition($field->getName(), $fieldDefiniton);
}

$entity->setValue($fields);
}

private function intersectFieldsAndDefinition(array $fields, FieldType $definition): array
{
return collect($fields)->filter(function (Field $field) use ($definition) {
return $definition->get('fields')->has($field->getName());
})->toArray();
}
}

0 comments on commit d1dc85b

Please sign in to comment.