Skip to content

Commit

Permalink
feature #6168 Add ability to clone fields (psihius)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Add ability to clone fields

`FieldDto` class already has a fully implemented `__clone` method, so this seems like an obvious addition that makes certain things just better :)

Allows to do stuff like this
```
$regularCheckInField = TimeField::new('fieldName')
    ->setLabel(t('Field Label'))
    ->setRequired(true)
    ->setFormat('HH:mm');

return [
    ...,
    (clone $regularCheckInField)->onlyOnIndex(),
    $regularCheckInField
        ->onlyOnForms()
        ->setCssClass('col-md-10 col-xxl-10')
        ->setColumns(2),
    ...,
];
```

Commits
-------

b212d8b Add ability to clone fields
  • Loading branch information
javiereguiluz committed Feb 28, 2024
2 parents e794e9b + b212d8b commit 45e8533
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/Field/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface FieldInterface
public static function new(string $propertyName, ?string /* TranslatableInterface|string|false|null */ $label = null);

public function getAsDto(): FieldDto;

public function __clone(): void;
}
5 changes: 5 additions & 0 deletions src/Field/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ private function __construct()
$this->dto = new FieldDto();
}

public function __clone(): void
{
$this->dto = clone $this->dto;
}

public function setFieldFqcn(string $fieldFqcn): self
{
$this->dto->setFieldFqcn($fieldFqcn);
Expand Down

0 comments on commit 45e8533

Please sign in to comment.