Skip to content

Commit

Permalink
embeddedlist test + widget identifier refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Oct 23, 2018
1 parent f45139a commit e478d33
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 170 deletions.
4 changes: 3 additions & 1 deletion TODO_mongo_odm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- ExcludeFieldsConfigPass
- ListFormFiltersConfigPass OK
- ShortFormTypeConfigPass: OK
- ShowViewConfigPass
- ShowViewConfigPass: OK

# embeddedlist id => md5

# Tests !
52 changes: 30 additions & 22 deletions src/Configuration/EmbeddedListViewConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
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.
* Initializes the configuration for all the views of each object of type "%s", which is
* needed when some object of type "%s" relies on the default configuration for some view.
*/
class EmbeddedListViewConfigPass implements ConfigPassInterface
{
Expand All @@ -32,9 +32,13 @@ public function process(array $backendConfig)
*/
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;
foreach (array('entities', 'documents') as $objectTypeRootKey) {
if (isset($backendConfig[$objectTypeRootKey]) && is_array($backendConfig[$objectTypeRootKey])) {
foreach ($backendConfig[$objectTypeRootKey] as $objectName => $objectConfig) {
if (!isset($objectConfig['embeddedList']['open_new_tab'])) {
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['open_new_tab'] = $this->defaultOpenNewTab;
}
}
}
}

Expand All @@ -48,25 +52,29 @@ private function processOpenNewTabConfig(array $backendConfig)
*/
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));
}
foreach (array('entities', 'documents') as $objectTypeRootKey) {
if (isset($backendConfig[$objectTypeRootKey]) && is_array($backendConfig[$objectTypeRootKey])) {
foreach ($backendConfig[$objectTypeRootKey] as $objectName => $objectConfig) {
if (
!isset($objectConfig['embeddedList']['sort'])
&& isset($objectConfig['list']['sort'])
) {
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['sort'] = $objectConfig['list']['sort'];
} elseif (isset($objectConfig['embeddedList']['sort'])) {
$sortConfig = $objectConfig['embeddedList']['sort'];
if (!is_string($sortConfig) && !is_array($sortConfig)) {
throw new \InvalidArgumentException(sprintf('The "sort" option of the "embeddedList" view of the "%s" object contains an invalid value (it can only be a string or an array).', $objectName));
}

if (is_string($sortConfig)) {
$sortConfig = array('field' => $sortConfig, 'direction' => 'DESC');
} else {
$sortConfig = array('field' => $sortConfig[0], 'direction' => strtoupper($sortConfig[1]));
}
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;
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['sort'] = $sortConfig;
}
}
}
}

Expand Down
48 changes: 38 additions & 10 deletions src/Configuration/ShowViewConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,47 @@ private function processTemplateOptions(string $type, array $fieldMetadata)

switch ($type) {
case 'embedded_list':
$parentEntityFqcn = $templateOptions['parent_entity_fqcn'] ?? $fieldMetadata['sourceEntity'];
$parentEntityProperty = $templateOptions['parent_entity_property'] ?? $fieldMetadata['property'];
$entityFqcn = $this->embeddedListHelper->getEntityFqcnFromParent(
$parentEntityFqcn, $parentEntityProperty
// Deprecations
if (isset($templateOptions['entity_fqcn']) && !isset($templateOptions['object_fqcn'])) {
$templateOptions['object_fqcn'] = $templateOptions['entity_fqcn'];
unset($templateOptions['entity_fqcn']);

trigger_error(sprintf('The "entity_fqcn" option for embedded_list is deprecated since version 1.4.0 and it will be removed in 2.0. Use the "object_fqcn" option instead.'), E_USER_DEPRECATED);
}
if (isset($templateOptions['parent_entity_fqcn']) && !isset($templateOptions['parent_object_fqcn'])) {
$templateOptions['parent_object_fqcn'] = $templateOptions['parent_entity_fqcn'];
unset($templateOptions['parent_entity_fqcn']);

trigger_error(sprintf('The "parent_entity_fqcn" option for embedded_list is deprecated since version 1.4.0 and it will be removed in 2.0. Use the "parent_object_fqcn" option instead.'), E_USER_DEPRECATED);
}
if (isset($templateOptions['parent_entity_property']) && !isset($templateOptions['parent_object_property'])) {
$templateOptions['parent_object_property'] = $templateOptions['parent_entity_property'];
unset($templateOptions['parent_entity_property']);

trigger_error(sprintf('The "parent_entity_property" option for embedded_list is deprecated since version 1.4.0 and it will be removed in 2.0. Use the "parent_object_property" option instead.'), E_USER_DEPRECATED);
}

$parentObjectFqcn = $templateOptions['parent_object_fqcn'] ?? $fieldMetadata['sourceEntity'];
$parentObjectProperty = $templateOptions['parent_object_property'] ?? $fieldMetadata['property'];
$objectFqcn = $this->embeddedListHelper->getEntityFqcnFromParent(
$parentObjectFqcn, $parentObjectProperty
);
if (!isset($templateOptions['entity_fqcn'])) {
$templateOptions['entity_fqcn'] = $entityFqcn;

if (isset($templateOptions['document'])) {
$templateOptions['object_type'] = 'document';
} else {
$templateOptions['object_type'] = 'entity';
}
if (!isset($templateOptions['parent_entity_property'])) {
$templateOptions['parent_entity_property'] = $parentEntityProperty;

if (!isset($templateOptions['entity']) && !isset($templateOptions['document'])) {
$templateOptions['entity'] = $this->embeddedListHelper->guessEntityEntry($objectFqcn);
}

if (!isset($templateOptions['object_fqcn'])) {
$templateOptions['object_fqcn'] = $objectFqcn;
}
if (!isset($templateOptions['entity'])) {
$templateOptions['entity'] = $this->embeddedListHelper->guessEntityEntry($entityFqcn);
if (!isset($templateOptions['parent_object_property'])) {
$templateOptions['parent_object_property'] = $parentObjectProperty;
}
if (!isset($templateOptions['filters'])) {
$templateOptions['filters'] = [];
Expand Down
1 change: 1 addition & 0 deletions src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function embeddedListAction()
$this->dispatch(EasyAdminEvents::POST_LIST, array('paginator' => $paginator));

return $this->render('@EasyAdminExtension/default/embedded_list.html.twig', array(
'objectType' => 'entity',
'paginator' => $paginator,
'fields' => $fields,
'masterRequest' => $this->get('request_stack')->getMasterRequest(),
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/MongoOdmAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ protected function embeddedListAction()
$this->dispatch(EasyAdminMongoOdmEvents::PRE_LIST);

$fields = $this->document['list']['fields'];
$paginator = $this->findAll($this->document['class'], $this->request->query->get('page', 1), $this->config['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'));
$paginator = $this->mongoOdmFindAll(
$this->document['class'],
$this->request->query->get('page', 1),
$this->config['list']['max_results'] ?: 25,
$this->request->query->get('sortField'),
$this->request->query->get('sortDirection')
);

$this->dispatch(EasyAdminMongoOdmEvents::POST_LIST, array('paginator' => $paginator));

return $this->render('@EasyAdminExtension/default/embedded_list.html.twig', array(
'objectType' => 'document',
'paginator' => $paginator,
'fields' => $fields,
'masterRequest' => $this->get('request_stack')->getMasterRequest(),
Expand Down
51 changes: 41 additions & 10 deletions src/Form/Type/EasyAdminEmbeddedListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,70 @@ public function getBlockPrefix()
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$parentData = $form->getParent()->getData();
if (null !== $options['document']) {
$view->vars['object_type'] = 'document';
$this->buildViewForDocumentList($view, $form, $options);
} else {
$view->vars['object_type'] = 'entity';
$this->buildViewForEntityList($view, $form, $options);
}

if ($options['sort']) {
$sort['field'] = $options['sort'][0];
$sort['direction'] = $options['sort'][1] ?? 'DESC';
$view->vars['sort'] = $sort;
}
}

private function buildViewForEntityList(FormView $view, FormInterface $form, array $options)
{
$parentData = $form->getParent()->getData();
$embeddedListEntity = $options['entity'];
$embeddedListFilters = $options['filters'];

// Guess entity FQCN from parent metadata
$entityFqcn = $this->embeddedListHelper->getEntityFqcnFromParent(get_class($parentData), $form->getName());
if (null !== $entityFqcn) {
$view->vars['entity_fqcn'] = $entityFqcn;
$view->vars['object_fqcn'] = $entityFqcn;
// Guess embeddedList entity if not set
if (!isset($embeddedListEntity)) {
$embeddedListEntity = $this->embeddedListHelper->guessEntityEntry($entityFqcn);
}
}

$view->vars['entity'] = $embeddedListEntity;
$view->vars['parent_entity_property'] = $form->getConfig()->getName();
$view->vars['parent_object_property'] = $form->getConfig()->getName();

// Only for backward compatibility (when there were no guesser)
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$filters = array_map(function ($filter) use ($propertyAccessor, $form) {
$filters = array_map(function ($filter) use ($propertyAccessor, $parentData) {
if (0 === strpos($filter, 'form:')) {
$filter = $propertyAccessor->getValue($form, substr($filter, 5));
$filter = $propertyAccessor->getValue($parentData, substr($filter, 5));
}

return $filter;
}, $embeddedListFilters);

$view->vars['filters'] = $filters;
}

if ($options['sort']) {
$sort['field'] = $options['sort'][0];
$sort['direction'] = $options['sort'][1] ?? 'DESC';
$view->vars['sort'] = $sort;
}
private function buildViewForDocumentList(FormView $view, FormInterface $form, array $options)
{
$parentData = $form->getParent()->getData();
$view->vars['document'] = $options['document'];
$embeddedListFilters = $options['filters'];

// Only for backward compatibility (when there were no guesser)
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$filters = array_map(function ($filter) use ($propertyAccessor, $parentData) {
if (0 === strpos($filter, 'form:')) {
$filter = $propertyAccessor->getValue($parentData, substr($filter, 5));
}

return $filter;
}, $embeddedListFilters);

$view->vars['filters'] = $filters;
}

/**
Expand All @@ -73,9 +102,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('document', null)
->setDefault('entity', null)
->setDefault('filters', array())
->setDefault('sort', null)
->setAllowedTypes('document', ['null', 'string'])
->setAllowedTypes('entity', ['null', 'string'])
->setAllowedTypes('filters', ['array'])
->setAllowedTypes('sort', ['null', 'string', 'array'])
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<argument>%easy_admin_extension.embedded_list.open_new_tab%</argument>
<!-- Makes it process just after ViewConfigPass -->
<tag name="easyadmin.config_pass" priority="29"/>
<tag name="easyadmin_mongo_odm.config_pass" priority="29"/>
</service>
<service id="alterphp.easyadmin_extension.helper.embedded_list" class="AlterPHP\EasyAdminExtensionBundle\Helper\EmbeddedListHelper">
<argument type="service" id="doctrine"/>
Expand Down
Loading

0 comments on commit e478d33

Please sign in to comment.