How to access current entity from configureActions() ?
Hello,
I trying to buld an action in crud controller, similar to the delete action (with 'formaction' html attribute to set the forms action) i need to access somehow the current entity's id but dont know, how.
my code is:
public function configureActions(Actions $actions): Actions
{
$formAction = $this->adminUrlGenerator
->setController(self::class)
->setAction(self::ACTION_SEND_PASSWORD_RESET_LINK)
->setEntityId(123) // <--- here i need the actual entity
->removeReferrer()
->generateUrl();
$sendPasswordResetLink = Action::new('send-password-reset-link', t('Send password reset link'))
->linkToRoute('asd', fn (User $user) => ['id' => $user->getId()])
->setHtmlAttributes([
'formaction' => $formAction,
'data-bs-toggle' => 'modal',
'data-bs-target' => '#modal-send-password-reset-link',
]);
return $actions
->add(Crud::PAGE_INDEX, $sendPasswordResetLink);
}
can somebody help me out with this? thank you
Hi,
On edit function, you have the context:
$context->getEntity()->getInstance();
or in some function
$this->getContext()->getEntity()->getInstance();
Here the context is still empty, but the entityId is in the url.
You can try that:
private $request;
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack;
}
public function configureActions(Actions $actions): Actions
{
$entityId = $this->request->getCurrentRequest()->get('entityId');
if($entityId) {
dump($entityId);
}
...
}
thank you, i dont need the selected user, which is loaded for edit, but every user in the table to fetch their id and put it in a html property of an action link.
I think it’s not possible with the attributes.
Maybe create a feature request to add callable support to setHtmlAttributes() like it’s already possible with linkToRoute(). I think it makes sense to add it. For example if you wanna trigger JavaScript behavior, combine with Stimulus, etc.
Would something like this be good enough ?
private function processAction(string $pageName, ActionDto $actionDto, ?EntityDto $entityDto = null): ActionDto
{
// ...
$actionDto->setTemplatePath($actionDto->getTemplatePath() ?? $defaultTemplatePath);
$actionDto->setLinkUrl($this->generateActionUrl($adminContext->getRequest(), $actionDto, $entityDto));
$htmlAttributes = [];
foreach ($actionDto->getHtmlAttributes() as $attrName => $htmlAttribute) {
if (! \is_callable($htmlAttribute)) {
$htmlAttributes[$attrName] = $htmlAttribute;
continue;
}
if ($entityDto?->getInstance() !== null) {
$htmlAttributes[$attrName] = $htmlAttribute($entityDto->getInstance());
}
}
$actionDto->setHtmlAttributes($htmlAttributes);
// ...
return $actionDto;
}
Seems like an easy feature for me, but I'm very new to this bundle.
Use example :
public function configureActions(Actions $actions): Actions
{
$action = Action::new('test', 'test')
->linkToCrudAction('test')
->setHtmlAttributes([
'data-bs-toggle' => 'modal',
'data-bs-target' => '#reset-password-modal',
'data-user' => static fn (User $u): int => $u->getId(),
])
;
$actions->add(Crud::PAGE_INDEX, $action);
return $actions;
}
Html result
<a
class="dropdown-item action-test"
data-bs-toggle="modal"
data-bs-target="#reset-password-modal"
data-user="6"
data-action-name="test"
>
<span class="action-label">
Test
</span>
</a>
The solution to this has not yet been provided. The only feature that needs to be implemented is to allow setHtmlAttributes accept a callable just like linkToUrl and other related Action methods.
public function configureActions(Actions $actions): Actions
{
$action = Action::new('test', 'test')
->linkToCrudAction('test')
->setHtmlAttributes(function(EntityInstance $user) {
return [
'data-bs-toggle' => 'modal',
'data-bs-target' => '#reset-password-modal',
'data-user' => $user->getId(),
]
})
;
$actions->add(Crud::PAGE_INDEX, $action);
return $actions;
}
Let us keep anticipating with hope that this feature will be added in future.