Skip to content

Commit

Permalink
bug #3529 Fixed the option to define empty labels when updating actio…
Browse files Browse the repository at this point in the history
…ns (javiereguiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Fixed the option to define empty labels when updating actions

Fixes #3450.

Commits
-------

697427f Fixed the option to define empty labels when updating actions
  • Loading branch information
javiereguiluz committed Jul 11, 2020
2 parents 106c187 + 697427f commit bd3048a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
58 changes: 31 additions & 27 deletions doc/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,34 @@ Adding Custom Actions
---------------------

In addition to the built-in actions provided by EasyAdmin, you can create your
own actions. An action always results in the execution of some method of some of
your controllers. If the method is defined in the CRUD controller, use
``linkToCrudAction()``; if the method is defined somewhere else, define a route
for it and use ``linkToRoute()``::
own actions. First, define the basics of your action (name, label, icon) with
the ``Action`` class constructor::

// the only mandatory argument is the internal name of the action (which is
// used to add the action to some pages, to reorder the action position, etc.)
$viewInvoice = Action::new('viewInvoice');

// the second optional argument is the label visible to end users
$viewInvoice = Action::new('viewInvoice', 'Invoice');
// not defining the label explicitly or setting it to NULL means
// that the label is autogenerated from the name (e.g. 'viewInvoice' -> 'View Invoice')
$viewInvoice = Action::new('viewInvoice', null);
// set the label to FALSE to not display any label for this action (but make sure
// to display an icon for the action; otherwise end users won't see it)
$viewInvoice = Action::new('viewInvoice', false);

// the third optional argument is the full CSS class of a FontAwesome icon
$viewInvoice = Action::new('viewInvoice', 'Invoice', 'fa fa-file-invoice');

Once you've configured the basics, use one of the following methods to define
which method is executed when clicking on the action:

* ``linkToCrudAction()``: to execute some method of the current CRUD controller;
* ``linkToRoute()``: to execute some regular Symfony controller via its route;
* ``linkToUrl()``: to visit an external URL (useful when your action is not
served by your application).

The following example shows all kinds of actions in practice::

namespace App\Controller\Admin;

Expand Down Expand Up @@ -288,36 +312,16 @@ for it and use ``linkToRoute()``::
];
});

return $actions
// ...
->add(Crud::PAGE_DETAIL, $viewInvoice)
->add(Crud::PAGE_DETAIL, $sendInvoice)
;
}
}

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
// this action points to the invoice on Stripe application
$viewStripeInvoice = Action::new('viewInvoice', 'Invoice', 'fa fa-file-invoice')
->linkToUrl(function (Invoice $entity) {
return 'https://www.stripe.com/invoice/'.$entity->getStripeReference();
});

return $actions
// ...
->add(Crud::PAGE_DETAIL, $viewInvoice)
->add(Crud::PAGE_DETAIL, $sendInvoice)
->add(Crud::PAGE_DETAIL, $viewStripeInvoice)
;
}
Expand Down
24 changes: 22 additions & 2 deletions src/Config/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Config;

use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use function Symfony\Component\String\u;

/**
* @author Javier Eguiluz <[email protected]>
Expand Down Expand Up @@ -48,7 +49,7 @@ public static function new(string $name, $label = null, ?string $icon = null): s
$dto->setType(self::TYPE_ENTITY);
$dto->setName($name);
$dto->setCssClass('action-'.$name);
$dto->setLabel($label ?? ucfirst($name));
$dto->setLabel($label ?? self::humanizeString($name));
$dto->setIcon($icon);
$dto->setHtmlElement('a');
$dto->setHtmlAttributes([]);
Expand Down Expand Up @@ -76,7 +77,7 @@ public function createAsBatchAction(): self
*/
public function setLabel($label): self
{
$this->dto->setLabel($label);
$this->dto->setLabel($label ?? self::humanizeString($this->dto->getName()));

return $this;
}
Expand Down Expand Up @@ -188,4 +189,23 @@ public function getAsDto(): ActionDto

return $this->dto;
}

private static function humanizeString(string $string): string
{
$uString = u($string);
$upperString = $uString->upper()->toString();

// this prevents humanizing all-uppercase labels (e.g. 'UUID' -> 'U u i d')
// and other special labels which look better in uppercase
if ($uString->toString() === $upperString) {
return $upperString;
}

return $uString
->replaceMatches('/([A-Z])/', '_$1')
->replaceMatches('/[_\s]+/', ' ')
->trim()
->lower()
->title(true);
}
}

0 comments on commit bd3048a

Please sign in to comment.