Skip to content

Commit

Permalink
feature #345 Allow to use an empty string as the action name (to disp…
Browse files Browse the repository at this point in the history
…lay only the icon) (javiereguiluz)

This PR was squashed before being merged into the master branch (closes #345).

Discussion
----------

Allow to use an empty string as the action name (to display only the icon)

This fixes #343

Commits
-------

3bb2970 Allow to use an empty string as the action name (to display only the icon)
  • Loading branch information
javiereguiluz committed May 30, 2015
2 parents b23b9b5 + 3bb2970 commit e569acd
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DependencyInjection/EasyAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ private function normalizeActionsConfiguration(array $actionsConfiguration, arra
$actionName = $normalizedConfiguration['name'];

// use the special 'action.<action name>' label for the default actions
if (!isset($normalizedConfiguration['label']) && in_array($actionName, array('delete', 'edit', 'new', 'search', 'show', 'list'))) {
// only if the user hasn't defined a custom label (which can also be an empty string)
if (null === $normalizedConfiguration['label'] && in_array($actionName, array('delete', 'edit', 'new', 'search', 'show', 'list'))) {
$normalizedConfiguration['label'] = 'action.'.$actionName;
}

Expand All @@ -336,7 +337,8 @@ private function normalizeActionsConfiguration(array $actionsConfiguration, arra
// option is actually added with the right default value. Otherwise,
// those options would be 'null' and the template would show some issues
if (array_key_exists($actionName, $defaultActionsConfiguration)) {
$normalizedConfiguration = array_filter($normalizedConfiguration); // remove empty/null config options
// remove null config options but maintain empty options (this allows to set an empty label for the action)
$normalizedConfiguration = array_filter($normalizedConfiguration, function($element) { return null !== $element; });
$normalizedConfiguration = array_replace($defaultActionsConfiguration[$actionName], $normalizedConfiguration);
}
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions Resources/doc/tutorials/tips-and-tricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,48 @@ override the default `layout.html.twig` template and empty the
Read the [Advanced Design Customization] [advanced-design-customization]
tutorial to learn how to override default templates.

Removing Action Labels and Displaying Just Icons
------------------------------------------------

By default, the actions showed in the `list` view table only display their
label (`Edit`, `Show`, etc.):

![Action Labels in Entity Listing](../images/easyadmin-listing-actions-label-only.png)

Adding an icon for each action is as easy as defining their `icon` option:

```yaml
easy_admin:
list:
actions:
- { name: 'show', icon: 'search' }
- { name: 'edit', icon: 'pencil' }
# ...
```

This configuration makes the entity listing looks as follow:

![Action Labels and Icons in Entity Listing](../images/easyadmin-listing-actions-label-and-icon.png)

When displaying entities with lots of information, it may be useful to remove
the action label and display just their icons. To do so, define an empty string
for the `label` option or set its value to `false`:

```yaml
easy_admin:
list:
actions:
- { name: 'show', icon: 'search', label: '' }
- { name: 'edit', icon: 'pencil', label: '' }
# if you prefer, set labels to false
# - { name: 'show', icon: 'search', label: false }
# - { name: 'edit', icon: 'pencil', label: false }
# ...
```

This configuration makes the entity listing looks as follow:

![Action Icons in Entity Listing](../images/easyadmin-listing-actions-icon-only.png)

[override-admin-controller]: ./customizing-admin-controller.md
[advanced-design-customization]: ./advanced-design-customization.md
72 changes: 72 additions & 0 deletions Tests/Controller/EmptyActionLabelsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JavierEguiluz\Bundle\EasyAdminBundle\Tests\Controller;

use Symfony\Component\DomCrawler\Crawler;
use JavierEguiluz\Bundle\EasyAdminBundle\Tests\Fixtures\AbstractTestCase;

class EmptyActionLabelsTest extends AbstractTestCase
{
public function setUp()
{
parent::setUp();

$this->initClient(array('environment' => 'empty_action_labels'));
}

public function testBuiltInActionLabels()
{
$crawler = $this->requestListView();

// show action
$this->assertEquals('', trim($crawler->filter('#main table tr:first-child td.actions a')->eq(0)->text()));
$this->assertEquals('fa fa-search', trim($crawler->filter('#main table tr:first-child td.actions a i')->eq(0)->attr('class')));

// edit action
$this->assertEquals('', trim($crawler->filter('#main table tr:first-child td.actions a')->eq(1)->text()));
$this->assertEquals('fa fa-pencil', trim($crawler->filter('#main table tr:first-child td.actions a i')->eq(1)->attr('class')));
}

public function testCustomActionLabels()
{
$crawler = $this->requestListView();

// custom action 1
$this->assertEquals('', trim($crawler->filter('#main table tr:first-child td.actions a')->eq(2)->text()));
$this->assertEquals('fa fa-icon1', trim($crawler->filter('#main table tr:first-child td.actions a i')->eq(2)->attr('class')));

// custom action 2
$this->assertEquals('', trim($crawler->filter('#main table tr:first-child td.actions a')->eq(3)->text()));
$this->assertEquals('fa fa-icon2', trim($crawler->filter('#main table tr:first-child td.actions a i')->eq(3)->attr('class')));
}

public function testFalseActionLabels()
{
$crawler = $this->requestListView();

// custom action with 'false' label used as a string instead of a boolean
$this->assertEquals('false', trim($crawler->filter('#main table tr:first-child td.actions a')->eq(4)->text()));
$this->assertEquals('fa fa-icon3', trim($crawler->filter('#main table tr:first-child td.actions a i')->eq(4)->attr('class')));
}

/**
* @return Crawler
*/
private function requestListView($entityName = 'Category')
{
return $this->getBackendPage(array(
'action' => 'list',
'entity' => $entityName,
'view' => 'list',
));
}
}
24 changes: 24 additions & 0 deletions Tests/Fixtures/App/config/config_empty_action_labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: config.yml }

framework:
# This file overrides the EasyAdmin controller
router: { resource: "%kernel.root_dir%/config/routing_override.yml" }

easy_admin:
entities:
Category:
class: JavierEguiluz\Bundle\EasyAdminBundle\Tests\Fixtures\AppTestBundle\Entity\Category
label: 'Categories'
list:
actions:
# override a built-in action with an empty label
- { name: 'show', label: '', icon: 'search' }
# override a built-in action with a false label
- { name: 'edit', label: false, icon: 'pencil' }
# custom action with an empty label
- { name: 'action1', label: '', icon: 'icon1' }
# custom action with a false label
- { name: 'action2', label: false, icon: 'icon2' }
# if 'false' is used as a string, it should be dispalyed as is
- { name: 'action3', label: 'false', icon: 'icon3' }

0 comments on commit e569acd

Please sign in to comment.