Skip to content

Commit

Permalink
Merge 6872e23 into ac10e67
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Feb 1, 2018
2 parents ac10e67 + 6872e23 commit a21e788
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Configuration/EmbeddedListSortConfigPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Configuration;

use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface;

/**
* 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
{
public function process(array $backendConfig)
{
$backendConfig = $this->processSortingConfig($backendConfig);

return $backendConfig;
}

/**
* @param array $backendConfig
*
* @return array
*/
private function processSortingConfig(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (
!isset($entityConfig['embeddedList']['sort'])
&& isset($entityConfig['list']['sort'])
) {
$backendConfig['entities'][$entityName]['embeddedList']['sort'] = $entityConfig['list']['sort'];
} elseif (isset($entityConfig['embeddedList']['sort'])) {
$sortConfig = $entityConfig['embeddedList']['sort'];
if (!is_string($sortConfig) && !is_array($sortConfig)) {
throw new \InvalidArgumentException(sprintf('The "sort" option of the "embeddedList" view of the "%s" entity contains an invalid value (it can only be a string or an array).', $entityName));
}

if (is_string($sortConfig)) {
$sortConfig = array('field' => $sortConfig, 'direction' => 'DESC');
} else {
$sortConfig = array('field' => $sortConfig[0], 'direction' => strtoupper($sortConfig[1]));
}

$backendConfig['entities'][$entityName]['embeddedList']['sort'] = $sortConfig;
}
}

return $backendConfig;
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<!-- 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">
<!-- Makes it process just after ViewConfigPass -->
<tag name="easyadmin.config_pass" priority="29"/>
</service>

<!-- Admin security roles -->
<service id="alterphp.easyadmin_extension.helper.editable_roles" class="AlterPHP\EasyAdminExtensionBundle\Helper\EditableRolesHelper">
Expand Down
20 changes: 20 additions & 0 deletions tests/Controller/EmbeddedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,24 @@ public function testRequestNotNullFilterIsApplied()
$crawler->filter('#main .list-pagination')->text()
);
}

public function testListSortIsUsedForEmbedddLists()
{
$crawler = $this->requestEditView('Category', 1);

$forAttrValue = '/admin/?entity=Product&action=embeddedList&filters%5Bentity.category%5D=1';
$createdAtTh = 'th[data-property-name="createdAt"].sorted';

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

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

$forAttrValue = '/admin/?entity=Purchase&action=embeddedList';
$createdAtTh = 'th[data-property-name="createdAt"].sorted';

$this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"] '.$createdAtTh)->count());
}
}
9 changes: 8 additions & 1 deletion tests/Fixtures/App/config/config_embedded_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ easy_admin:
fields:
- { property: name }
- { property: products, label: '', type: embedded_list, type_options: { entity: Product, filters: { 'entity.category': 'form:parent.data.id' } } }
Product: AppTestBundle\Entity\FunctionalTests\Product
Product:
class: AppTestBundle\Entity\FunctionalTests\Product
list:
sort: [createdAt, DESC]
Purchase:
class: AppTestBundle\Entity\FunctionalTests\Purchase
embeddedList:
sort: [createdAt, DESC]

0 comments on commit a21e788

Please sign in to comment.