Skip to content

Commit

Permalink
feature #6132 [BUGFIX] Perform "shouldBeDisplayedFor" check for globa…
Browse files Browse the repository at this point in the history
…l actions (like "new") (a-r-m-i-n)

This PR was merged into the 4.x branch.

Discussion
----------

[BUGFIX] Perform "shouldBeDisplayedFor" check for global actions (like "new")

Fixes: #6131

Commits
-------

b59d0e0 [BUGFIX] Perform "shouldBeDisplayedFor" check for global actions (like "new")
  • Loading branch information
javiereguiluz committed Mar 1, 2024
2 parents 6f72112 + b59d0e0 commit 984a990
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/actions.rst
Expand Up @@ -149,6 +149,8 @@ to users::
->add(Crud::PAGE_INDEX, $viewInvoice);
}

Note: When using ``displayIf()`` for global actions like "new", no entity instance is passed to closure function.

Disabling Actions
-----------------

Expand Down
4 changes: 2 additions & 2 deletions src/Dto/ActionDto.php
Expand Up @@ -239,9 +239,9 @@ public function setTranslationParameters(array $translationParameters): void
$this->translationParameters = $translationParameters;
}

public function shouldBeDisplayedFor(EntityDto $entityDto): bool
public function shouldBeDisplayedFor(?EntityDto $entityDto): bool
{
return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable, $entityDto->getInstance());
return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable, $entityDto?->getInstance());
}

public function setDisplayCallable(callable $displayCallable): void
Expand Down
4 changes: 4 additions & 0 deletions src/Factory/ActionFactory.php
Expand Up @@ -93,6 +93,10 @@ public function processGlobalActions(?ActionConfigDto $actionsDto = null): Actio
continue;
}

if (false === $actionDto->shouldBeDisplayedFor(null)) {
continue;
}

if (Crud::PAGE_INDEX !== $currentPage && $actionDto->isBatchAction()) {
throw new \RuntimeException(sprintf('Batch actions can be added only to the "index" page, but the "%s" batch action is defined in the "%s" page.', $actionDto->getName(), $currentPage));
}
Expand Down

0 comments on commit 984a990

Please sign in to comment.