Skip to content

Commit

Permalink
minor #3229 Fixed an edge-case related to fields and form panels (jav…
Browse files Browse the repository at this point in the history
…iereguiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Fixed an edge-case related to fields and form panels

Not a very common edge-case, but I faced it when testing things.

Commits
-------

17a7b9c Fixed an edge-case related to fields and form panels
  • Loading branch information
javiereguiluz committed May 17, 2020
2 parents fa0d07b + 17a7b9c commit 07eb7ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Collection/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public function unset(FieldDto $removedField): void
unset($this->fields[$removedField->getProperty()]);
}

public function prepend(FieldDto $newField): void
{
$this->fields = array_merge([$newField->getProperty() => $newField], $this->fields);
}

public function first(): ?FieldDto
{
if (empty($this->fields)) {
return null;
}

return $this->fields[array_key_first($this->fields)];
}

public function isEmpty(): bool
{
return 0 === \count($this->fields);
}

public function offsetExists($offset): bool
{
return \array_key_exists($offset, $this->fields);
Expand Down
6 changes: 6 additions & 0 deletions src/Dto/FieldDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Provider\UlidProvider;
use function Symfony\Component\String\u;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -61,6 +62,11 @@ public function getUniqueId(): string
return $this->uniqueId = UlidProvider::new();
}

public function isFormDecorationField(): bool
{
return null !== u($this->getCssClass())->indexOf('field-form_panel');
}

public function getFieldFqcn(): string
{
return $this->fieldFqcn;
Expand Down
17 changes: 17 additions & 0 deletions src/Factory/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
Expand All @@ -20,6 +21,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use function Symfony\Component\String\u;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -92,6 +94,21 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo

private function preProcessFields(FieldCollection $fields, EntityDto $entityDto): void
{
if ($fields->isEmpty()) {
return;
}

// this is needed to handle this edge-case: the list of fields include one or more form panels,
// but the first fields of the list don't belong to any panel. We must create an automatic empty
// form panel for those "orphaned fields" so they are displayed as expected
$firstFieldIsAFormPanel = $fields->first()->isFormDecorationField();
foreach ($fields as $fieldName => $fieldDto) {
if (!$firstFieldIsAFormPanel && $fieldDto->isFormDecorationField()) {
$fields->prepend(FormField::addPanel()->getAsDto());
break;
}
}

foreach ($fields as $fieldName => $fieldDto) {
if (Field::class !== $fieldDto->getFieldFqcn()) {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/Field/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function addPanel(?string $label = null, ?string $icon = null)
$field = new self();

return $field
->setFieldFqcn(__CLASS__)
->setProperty('ea_form_panel_'.(UlidProvider::new()))
->setLabel($label)
->setTemplateName('crud/field/form_panel')
Expand Down

0 comments on commit 07eb7ff

Please sign in to comment.