Skip to content

Commit

Permalink
Merge 4042a26 into f8de49f
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Mar 31, 2018
2 parents f8de49f + 4042a26 commit 815a390
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@
* Initializes the configuration for all the views of each entity, which is
* needed when some entity relies on the default configuration for some view.
*/
class EmbeddedListSortConfigPass implements ConfigPassInterface
class EmbeddedListViewConfigPass implements ConfigPassInterface
{
private $defaultOpenNewTab;

public function __construct($defaultOpenNewTab)
{
$this->defaultOpenNewTab = $defaultOpenNewTab;
}

public function process(array $backendConfig)
{
$backendConfig = $this->processSortingConfig($backendConfig);
$backendConfig = $this->processOpenNewTabConfig($backendConfig);

return $backendConfig;
}

/**
* @param array $backendConfig
*
* @return array
*/
private function processOpenNewTabConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (!isset($entityConfig['embeddedList']['open_new_tab'])) {
$backendConfig['entities'][$entityName]['embeddedList']['open_new_tab'] = $this->defaultOpenNewTab;
}
}

return $backendConfig;
}
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function getConfigTreeBuilder()
->scalarNode('minimum_role')
->defaultNull()
->end()
->arrayNode('embedded_list')
->addDefaultsIfNotSet()
->children()
->booleanNode('open_new_tab')
->defaultTrue()
->end()
->end()
->end()
->end()
;

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/EasyAdminExtensionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('easy_admin_extension.custom_form_types', $config['custom_form_types']);
$container->setParameter('easy_admin_extension.minimum_role', $config['minimum_role']);
$container->setParameter(
'easy_admin_extension.embedded_list.open_new_tab',
$config['embedded_list']['open_new_tab']
);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<!-- Makes it process just before PropertyConfigPass -->
<tag name="easyadmin.config_pass" priority="41"/>
</service>
<service id="alterphp.easyadmin_extension.config_pass.embedded_list_sort" class="AlterPHP\EasyAdminExtensionBundle\Configuration\EmbeddedListSortConfigPass">
<service id="alterphp.easyadmin_extension.config_pass.embedded_list_view" class="AlterPHP\EasyAdminExtensionBundle\Configuration\EmbeddedListViewConfigPass">
<argument>%easy_admin_extension.embedded_list.open_new_tab%</argument>
<!-- Makes it process just after ViewConfigPass -->
<tag name="easyadmin.config_pass" priority="29"/>
</service>
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/views/default/embedded_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@
{% endblock table_body %}
</tbody>

{% if _entity_config.embeddedList.open_new_tab %}
<tfoot>
<tr>
<td colspan="{{ _columns_count }}" class="text-center">
{% block open_new_tab %}
<a class="btn btn-info btn-xs" title="{{ 'open.new_tab'|trans({}, 'EasyAdminBundle') }}" target="_blank" href="{{ path('easyadmin', _request_parameters|merge({action: 'list'})) }}">
<a class="btn btn-info btn-xs open-new-tab" title="{{ 'open.new_tab'|trans({}, 'EasyAdminBundle') }}" target="_blank" href="{{ path('easyadmin', _request_parameters|merge({action: 'list'})) }}">
<i class="fa fa-external-link-square"></i>
<span>{{ 'open.new_tab'|trans({}, 'EasyAdminBundle') }}</span>
</a>
{% endblock open_new_tab %}
</td>
</tr>
</tfoot>
{% endif %}
</table>
</div>

Expand Down
50 changes: 50 additions & 0 deletions tests/Configuration/EmbeddedListViewConfigPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration;

use AlterPHP\EasyAdminExtensionBundle\Configuration\EmbeddedListViewConfigPass;

class EmbeddedListViewConfigPassTest extends \PHPUnit_Framework_TestCase
{
public function testOpenNewTabOption()
{
$embeddedListViewConfigPass = new EmbeddedListViewConfigPass(true);

$backendConfig = array(
'entities' => array(
'NotSetEntity' => array(
),
'SetTrueEntity' => array(
'embeddedList' => array('open_new_tab' => true)
),
'SetFalseEntity' => array(
'embeddedList' => array('open_new_tab' => false)
),
),
);

$backendConfig = $embeddedListViewConfigPass->process($backendConfig);

$expectedBackendConfig = array(
'entities' => array(
'NotSetEntity' => array(
'embeddedList' => array(
'open_new_tab' => true,
),
),
'SetTrueEntity' => array(
'embeddedList' => array(
'open_new_tab' => true,
),
),
'SetFalseEntity' => array(
'embeddedList' => array(
'open_new_tab' => false,
),
),
),
);

$this->assertSame($backendConfig, $expectedBackendConfig);
}
}
14 changes: 14 additions & 0 deletions tests/Controller/EmbeddedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,18 @@ public function testDefinedSortIsUsedForEmbedddLists()

$this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"] '.$createdAtTh)->count());
}

public function testDefaultOpenNewTabConfigForEmbedddLists()
{
$crawler = $this->getBackendPage(array('entity' => 'Product', 'action' => 'embeddedList'));

$this->assertSame(0, $crawler->filter('.embedded-list .open-new-tab')->count());
}

public function testSetOpenNewTabConfigForEmbedddLists()
{
$crawler = $this->getBackendPage(array('entity' => 'Purchase', 'action' => 'embeddedList'));

$this->assertSame(1, $crawler->filter('.embedded-list .open-new-tab')->count());
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/App/config/config_embedded_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ imports:
easy_admin_extension:
custom_form_types:
embedded_list: AlterPHP\EasyAdminExtensionBundle\Form\Type\EasyAdminEmbeddedListType
embedded_list:
open_new_tab: false

easy_admin:
entities:
Expand All @@ -20,4 +22,5 @@ easy_admin:
Purchase:
class: AppTestBundle\Entity\FunctionalTests\Purchase
embeddedList:
open_new_tab: true
sort: [createdAt, DESC]

0 comments on commit 815a390

Please sign in to comment.