Skip to content

Commit

Permalink
feature #3190 Add support for links to a URL (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Add support for links to a URL

<!--
Thanks for your contribution! If you are proposing a new feature that is complex,
please open an issue first so we can discuss about it.

Note: all your contributions adhere implicitly to the MIT license
-->

Commits
-------

7ef801f Add support for links to a URL
  • Loading branch information
javiereguiluz committed May 8, 2020
2 parents 5cb45f6 + 7ef801f commit 3d77e37
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
27 changes: 27 additions & 0 deletions doc/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,33 @@ for it and use ``linkToRoute()``::
}
}

To link to an external URL, use ``linkToUrl()``::

namespace App\Controller\Admin;

use App\Entity\Invoice;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class ProductCrudController extends AbstractCrudController
{
// ...

public function configureActions(Actions $actions): Actions
{
// this action go to the invoice on Stripe
$viewStripeInvoice = Action::new('View invoice', 'fa fa-file-invoice')
->linkToUrl(function (Invoice $entity) {
return 'https://www.stripe.com/invoice/'.$entity->getStripeReference();
});

return $actions
// ...
->add('viewInvoice', $viewStripeInvoice)
;
}
}

Batch Actions
-------------

Expand Down
14 changes: 12 additions & 2 deletions src/Config/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ public function linkToRoute(string $routeName, $routeParameters = []): self
return $this;
}

/**
* @param string|callable $url
*/
public function linkToUrl($url): self
{
$this->dto->setUrl($url);

return $this;
}

/**
* If not defined, actions use the same domain as configured for the entire dashboard.
*/
Expand Down Expand Up @@ -182,8 +192,8 @@ public function getAsDto(): ActionDto
throw new \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 (null === $this->dto->getCrudActionName() && null === $this->dto->getRouteName()) {
throw new \InvalidArgumentException(sprintf('Actions must link to either a route or a CRUD action. Set the "linkToCrudAction()" or "linkToRoute()" method for the "%s" action.', $this->dto->getName()));
if (null === $this->dto->getCrudActionName() && null === $this->dto->getRouteName() && null === $this->dto->getUrl()) {
throw new \InvalidArgumentException(sprintf('Actions must link to either a route, a CRUD action, or a URL. Set the "linkToCrudAction()", "linkToRoute()", or "linkToUrl()" method for the "%s" action.', $this->dto->getName()));
}

return $this->dto;
Expand Down
17 changes: 17 additions & 0 deletions src/Dto/ActionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class ActionDto
private $crudActionName;
private $routeName;
private $routeParameters;
private $url;
private $translationDomain;
private $translationParameters;
private $displayCallable;
Expand Down Expand Up @@ -182,6 +183,22 @@ public function setRouteParameters($routeParameters): void
$this->routeParameters = $routeParameters;
}

/**
* @return string|callable
*/
public function getUrl()
{
return $this->url;
}

/**
* @param string|callable $url
*/
public function setUrl($url): void
{
$this->url = $url;
}

public function getTranslationDomain(): ?string
{
return $this->translationDomain;
Expand Down
8 changes: 8 additions & 0 deletions src/Factory/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt

private function generateActionUrl(string $currentAction, Request $request, ActionDto $actionDto, ?EntityDto $entityDto = null): string
{
if (null !== $url = $actionDto->getUrl()) {
if (\is_callable($url)) {

This comment has been minimized.

Copy link
@alex-bacart

alex-bacart May 8, 2020

IMO it's more readable

return \is_callable($url) ? $url($entityDto->getInstance()) : $url;
return $url($entityDto->getInstance());
}

return $url;
}

if (null !== $routeName = $actionDto->getRouteName()) {
$routeParameters = $actionDto->getRouteParameters();
if (\is_callable($routeParameters)) {
Expand Down

0 comments on commit 3d77e37

Please sign in to comment.