Allow Closure for `setValue`
Short description of what this feature will allow to do: There are cases such as external files (VichUploaderBundle) where database stores only part of path, but for admin purposes we need to resolve full url.
- We cannot create getter in Entity, because it requires as service.
- We cannot pass \Closure because it does not allow
Example of how to use this feature By passing \Closure, it will allow to format/modify value on list/index action as we need.
Current Workaround
yield UrlField::new('file_path')
->formatValue(fn ($value, $entity) => $this->storage->resolveUri($entity))
->setValue(new class ($this->storage) implements \Stringable {
private array $instances = [];
public function __construct(private readonly StorageInterface $storage)
{
}
public function __toString(): string
{
$trace = debug_backtrace(limit: 3);
$caller = $trace[1];
$args = $caller['args'];
$entityDto = $args[1];
if ($entityDto instanceof EntityDto) {
$this->instances[spl_object_id($entityDto)] = $entityDto->getInstance();
} else {
$caller = $trace[2];
$args = $caller['args'];
$entityDto = $args[0]['entity'];
}
return $this->storage->resolveUri($this->instances[spl_object_id($entityDto)]);
}
})
;
Another example: I want to apply \Doctrine\SqlFormatter\SqlFormatter on SQL code from entity, but I don't want to "keep" this code in entity...
so another sh*tty workaround
yield CodeEditorField::new('executableSelector')
->setHelp('For developers')
->setDisabled()
->setLanguage('sql')
// @see https://github.com/EasyCorp/EasyAdminBundle/issues/5940
->setFormTypeOption('data', new class implements \Stringable {
public function __toString(): string
{
$trace = debug_backtrace(limit: 3);
$caller = $trace[2];
assert(isset($caller['args']));
$args = $caller['args'];
$form = $args[0]['form'];
assert($form instanceof FormView);
$data = $form->parent->vars['data'];
assert($data instanceof Category);
return (new SqlFormatter(new NullHighlighter()))->format($data->getExecutableSelector());
}
})
;
I have almost same issue here https://github.com/EasyCorp/EasyAdminBundle/issues/6176
I think this isn't so obscure issue and it should be possible in some meaningfully easy way.