Skip to content

Commit

Permalink
feature #4186 Allow to render the same field more than once in index/…
Browse files Browse the repository at this point in the history
…detail pages (javiereguiluz)

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

Discussion
----------

Allow to render the same field more than once in index/detail pages

It's an alternative to #4178 and fixes #4178.

This allows to display the same field any number of times, but only in the pages controlled by EasyAdmin (index/detail). In the other pages (edit/new) you can't do that because they are controlled by Symfony Forms and they don't allow to render the same field twice in a single form.

Commits
-------

0cacb3d Allow to render the same field more than once in index/detail pages
  • Loading branch information
javiereguiluz committed Feb 5, 2021
2 parents f4be6be + 0cacb3d commit 423c7f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
30 changes: 23 additions & 7 deletions src/Collection/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private function __construct(iterable $fields)
public function __clone()
{
foreach ($this->fields as $fieldName => $fieldDto) {
$this->fields[$fieldName] = clone $fieldDto;
$this->fields[$fieldDto->getUniqueId()] = clone $fieldDto;
}
}

Expand All @@ -38,24 +38,40 @@ public static function new(iterable $fields): self
return new self($fields);
}

public function get(string $fieldName): ?FieldDto
public function get(string $fieldUniqueId): ?FieldDto
{
return $this->fields[$fieldName] ?? null;
return $this->fields[$fieldUniqueId] ?? null;
}

/**
* It returns the first field with the given name or null if none found.
* Some pages (index/detail) can render the same field more than once.
* In those cases, this method always returns the first field occurrence.
*/
public function getByName(string $fieldName): ?FieldDto
{
foreach ($this->fields as $field) {
if ($fieldName === $field->getProperty()) {
return $field;
}
}

return null;
}

public function set(FieldDto $newOrUpdatedField): void
{
$this->fields[$newOrUpdatedField->getProperty()] = $newOrUpdatedField;
$this->fields[$newOrUpdatedField->getUniqueId()] = $newOrUpdatedField;
}

public function unset(FieldDto $removedField): void
{
unset($this->fields[$removedField->getProperty()]);
unset($this->fields[$removedField->getUniqueId()]);
}

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

public function first(): ?FieldDto
Expand Down Expand Up @@ -124,7 +140,7 @@ private function processFields(iterable $fields): array

$dto = $field->getAsDto();
$dto->setFieldFqcn(\get_class($field));
$dtos[$dto->getProperty()] = $dto;
$dtos[$dto->getUniqueId()] = $dto;
}

return $dtos;
Expand Down
7 changes: 2 additions & 5 deletions src/Dto/FieldDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class FieldDto

public function __construct()
{
$this->uniqueId = new Ulid();
$this->cssClass = '';
$this->templateName = 'crud/field/text';
$this->assets = new AssetsDto();
Expand All @@ -65,11 +66,7 @@ public function __clone()

public function getUniqueId(): string
{
if (null !== $this->uniqueId) {
return $this->uniqueId;
}

return $this->uniqueId = new Ulid();
return $this->uniqueId;
}

public function isFormDecorationField(): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/crud/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<div class="content-panel">
<div class="content-panel-header {{ collapsible ? 'collapsible' }} {{ field.help is not empty ? 'with-help' }}">
{% if collapsible %}
<a href="#content-{{ field.property }}" data-toggle="collapse" class="content-panel-collapse {{ collapsed ? 'collapsed' }}" aria-expanded="{{ collapsed ? 'false' : 'true' }}" aria-controls="content-{{ field.property }}">
<a href="#content-{{ field.uniqueId }}" data-toggle="collapse" class="content-panel-collapse {{ collapsed ? 'collapsed' }}" aria-expanded="{{ collapsed ? 'false' : 'true' }}" aria-controls="content-{{ field.uniqueId }}">
<i class="fas fw fa-chevron-right collapse-icon"></i>
{% endif %}

Expand All @@ -123,7 +123,7 @@
{% endif %}
</div>

<div id="content-{{ field.property }}" class="content-panel-body without-footer without-padding {{ collapsible ? 'collapse' }} {{ not collapsed ? 'show'}}">
<div id="content-{{ field.uniqueId }}" class="content-panel-body without-footer without-padding {{ collapsible ? 'collapse' }} {{ not collapsed ? 'show'}}">
<dl class="datalist">
{% endmacro %}

Expand Down

0 comments on commit 423c7f3

Please sign in to comment.