You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #6599 Allow to compute action label dynamically with a callable (hhamon)
This PR was merged into the 4.x branch.
Discussion
----------
Allow to compute action label dynamically with a callable
The goal of this MR is to allow computing dynamic label when adding new custom actions at the top of an entity page (i.e. edit, details, etc.).
For instance, I have an entity model on which I can list and add internal notes. At the top of my entity model details page, I've configured a new custom action that points to another controller that enables to view and add internal notes.
For a sake of improved user experience, I want the action label to display the number of related internal notes that have been added for the entity model I'm currently administrating.
<img width="379" alt="Screenshot 2024-11-28 at 20 32 57" src="https://github.com/user-attachments/assets/55082738-92f9-4d9e-ac2d-767e977b9de7">
<img width="373" alt="Screenshot 2024-11-28 at 20 36 40" src="https://github.com/user-attachments/assets/6e9703cb-02d1-4c18-a6e5-826bcebb181b">
The current implementation only enables to define a static string label for an action. Using a similar approach to the `->displayIf()`, the `Action::new()` and `Action::setLabel()` methods now supports receiving a callable that will be evaluated later by the `ActionFactory` service.
The callable receives the entity model instance and is evaluated only once. The computed label gets stored in the `ActionDto::$label` property automatically.
The good part of using the callable is that the label can be dynamically computed thanks to:
1. The received entity model instance
2. Any extra parameters imported/used by the `Closure` object
3. Any injected service object that the `Closure` has access to within its scope
WDYT?
Commits
-------
d1e8742 Allow to compute action label dynamically with a callable
thrownew \InvalidArgumentException(sprintf('The label and icon of an action cannot be null at the same time. Either set the label, the icon or both for the "%s" action.', $this->dto->getName()));
if (!\is_string($label) && !$labelinstanceof TranslatableInterface) {
298
+
thrownew \RuntimeException(sprintf('Action label callable must return a string or a %s instance but it returned a(n) "%s" value instead.', TranslatableInterface::class, \gettype($label)));
$this->expectExceptionMessage('Action label callable must return a string or a Symfony\Contracts\Translation\TranslatableInterface instance but it returned a(n) "integer" value instead.');
0 commit comments