Skip to content

Commit

Permalink
bug #6621 Fix action callables (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Fix action callables

This morning, when updating some app to the latest version, I faced an issue in an action with the label `Reset`. Let's fix it.

Commits
-------

54d6112 Fix action callables
  • Loading branch information
javiereguiluz committed Dec 4, 2024
2 parents 41c158c + 54d6112 commit cd931e9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Factory/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function processActionLabel(ActionDto $actionDto, ?EntityDto $entityDto,
return;
}

if (\is_callable($label)) {
if (\is_callable($label) && $label instanceof \Closure) {
$label = \call_user_func_array($label, array_filter([$entityDto?->getInstance()]));

if (!\is_string($label) && !$label instanceof TranslatableInterface) {
Expand Down
7 changes: 4 additions & 3 deletions tests/Controller/ActionsCrudControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public function testDynamicLabels()
{
$crawler = $this->client->request('GET', $this->generateIndexUrl());

static::assertSame('Action 5: Category 0', $crawler->filter('a.dropdown-item:contains("Action 5")')->text());
static::assertSame('Action 6: Category 0', $crawler->filter('a.dropdown-item:contains("Action 6")')->text());
static::assertSame('Action 7: Category 0', $crawler->filter('a.dropdown-item:contains("Action 7")')->text());
static::assertSame('Action 5: Category 0', $crawler->filter('a.dropdown-item[data-action-name="action5"]')->text());
static::assertSame('Action 6: Category 0', $crawler->filter('a.dropdown-item[data-action-name="action6"]')->text());
static::assertSame('Action 7: Category 0', $crawler->filter('a.dropdown-item[data-action-name="action7"]')->text());
static::assertSame('Reset', $crawler->filter('a.dropdown-item[data-action-name="action8"]')->text());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function configureActions(Actions $actions): Actions

$action7 = Action::new('action7')->linkToCrudAction('')->setLabel(fn (Category $category) => t('Action %number%: %name%', ['%number%' => 7, '%name%' => $category->getName()]));

// this tests that the 'Reset' label is interpreted as a string and not as a callable to the PHP reset() function
$action8 = Action::new('action8')->linkToCrudAction('')->setLabel('Reset');

return $actions
->add(Crud::PAGE_INDEX, $action1)
->add(Crud::PAGE_INDEX, $action2)
Expand All @@ -48,6 +51,7 @@ public function configureActions(Actions $actions): Actions
->add(Crud::PAGE_INDEX, $action5)
->add(Crud::PAGE_INDEX, $action6)
->add(Crud::PAGE_INDEX, $action7)
->add(Crud::PAGE_INDEX, $action8)
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setIcon('fa fa-fw fa-plus')->setLabel(false);
})
Expand Down

0 comments on commit cd931e9

Please sign in to comment.