From 2bacd6dc384d175040796fce8b138bbab85523cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9C=C3=A9?= Date: Tue, 23 Oct 2018 15:03:58 +0200 Subject: [PATCH] embeddedlist test + widget identifier refactored --- TODO_mongo_odm.md | 4 +- .../EmbeddedListViewConfigPass.php | 52 +++-- src/Configuration/ShowViewConfigPass.php | 48 ++++- src/Controller/AdminController.php | 1 + src/Controller/MongoOdmAdminController.php | 9 +- src/Form/Type/EasyAdminEmbeddedListType.php | 53 ++++- src/Resources/config/services.xml | 1 + .../views/default/embedded_list.html.twig | 202 +++++++++--------- .../default/field_embedded_list.html.twig | 18 +- .../views/form/bootstrap_3_layout.html.twig | 15 +- .../includes/_include_embedded_list.html.twig | 8 +- src/Twig/EasyAdminExtensionTwigExtension.php | 13 ++ src/Twig/EmbeddedListExtension.php | 17 +- .../Configuration/ShowViewConfigPassTest.php | 18 +- tests/Controller/EmbeddedListTest.php | 10 +- 15 files changed, 298 insertions(+), 171 deletions(-) diff --git a/TODO_mongo_odm.md b/TODO_mongo_odm.md index cba69b5..8fe73cc 100644 --- a/TODO_mongo_odm.md +++ b/TODO_mongo_odm.md @@ -7,6 +7,8 @@ - ExcludeFieldsConfigPass - ListFormFiltersConfigPass OK - ShortFormTypeConfigPass: OK - - ShowViewConfigPass + - ShowViewConfigPass: OK + +# embeddedlist id => md5 # Tests ! diff --git a/src/Configuration/EmbeddedListViewConfigPass.php b/src/Configuration/EmbeddedListViewConfigPass.php index 3d1d995..45ba79c 100644 --- a/src/Configuration/EmbeddedListViewConfigPass.php +++ b/src/Configuration/EmbeddedListViewConfigPass.php @@ -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 { @@ -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; + } + } } } @@ -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; + } + } } } diff --git a/src/Configuration/ShowViewConfigPass.php b/src/Configuration/ShowViewConfigPass.php index 2841fbf..5cb6fa2 100644 --- a/src/Configuration/ShowViewConfigPass.php +++ b/src/Configuration/ShowViewConfigPass.php @@ -81,19 +81,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'] = []; diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index 5d080f1..14a27e2 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -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(), diff --git a/src/Controller/MongoOdmAdminController.php b/src/Controller/MongoOdmAdminController.php index 585fd05..89b78ba 100644 --- a/src/Controller/MongoOdmAdminController.php +++ b/src/Controller/MongoOdmAdminController.php @@ -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(), diff --git a/src/Form/Type/EasyAdminEmbeddedListType.php b/src/Form/Type/EasyAdminEmbeddedListType.php index 40432c2..dce740c 100644 --- a/src/Form/Type/EasyAdminEmbeddedListType.php +++ b/src/Form/Type/EasyAdminEmbeddedListType.php @@ -38,15 +38,31 @@ 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()); + $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 (null === $embeddedListEntity) { $embeddedListEntity = $this->embeddedListHelper->guessEntityEntry($entityFqcn); @@ -54,25 +70,38 @@ public function buildView(FormView $view, FormInterface $form, array $options) } $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; } /** @@ -81,9 +110,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']) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index c1661bf..789dc22 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -32,6 +32,7 @@ %easy_admin_extension.embedded_list.open_new_tab% + diff --git a/src/Resources/views/default/embedded_list.html.twig b/src/Resources/views/default/embedded_list.html.twig index 259225e..110b41d 100644 --- a/src/Resources/views/default/embedded_list.html.twig +++ b/src/Resources/views/default/embedded_list.html.twig @@ -1,12 +1,12 @@ -{% set _entity_config = easyadmin_entity(app.request.query.get('entity')) %} -{% trans_default_domain _entity_config.translation_domain %} -{% set _trans_parameters = { '%entity_name%': _entity_config.name|trans, '%entity_label%': _entity_config.label|trans } %} +{% set _object_config = easyadmin_object(app.request) %} +{% trans_default_domain _object_config.translation_domain %} +{% set _trans_parameters = { '%entity_name%': _object_config.name|trans, '%entity_label%': _object_config.label|trans } %} {# OVERRIDE referer #} {% set _request_parameters = app.request.query.all %} {% set _request_parameters = _request_parameters|merge({ referer: masterRequest.requestUri|url_encode }) %} -{% set widget_identifier = app.request.requestUri %} +{% set widget_identifier = app.request.requestUri|embedded_list_identifier %} {% set _content_title %} {% spaceless %} @@ -14,114 +14,118 @@ {{ 'search.page_title'|transchoice(paginator.nbResults, {}, 'EasyAdminBundle')|raw }} {% else %} {% set _default_title = 'list.page_title'|trans(_trans_parameters, 'EasyAdminBundle') %} - {{ _entity_config.list.title is defined ? _entity_config.list.title|trans(_trans_parameters) : _default_title }} + {{ _object_config.list.title is defined ? _object_config.list.title|trans(_trans_parameters) : _default_title }} {% endif %} {% endspaceless %} {% endset %} {% block main %} -
- {% set _list_item_actions = easyadmin_get_actions_for_list_item(_entity_config.name) %} - {# Prune forbidden actions AND delete action anyway (not handled with #delete-modal) #} - {% set _list_item_actions = _list_item_actions|prune_item_actions(_entity_config, ['delete']) %} - {% set _columns_count = fields|length + (_list_item_actions|length > 0 ? 1 : 0) %} +
+ {% set _list_item_actions = easyadmin_get_actions_for_list_item(_object_config.name) %} + {# Prune forbidden actions AND delete action anyway (not handled with #delete-modal) #} + {% set _list_item_actions = _list_item_actions|prune_item_actions(_object_config, ['delete']) %} + {% set _columns_count = fields|length + (_list_item_actions|length > 0 ? 1 : 0) %} -
- - - {% block table_head %} - - {% for field, metadata in fields %} - {% set isSortingField = metadata.property == app.request.get('sortField')|split('.')|first %} - {% set nextSortDirection = isSortingField ? (app.request.get('sortDirection') == 'DESC' ? 'ASC' : 'DESC') : 'DESC' %} - {% set _column_label = (metadata.label ?: field|humanize)|trans(_trans_parameters) %} - {% set _column_icon = isSortingField ? (nextSortDirection == 'DESC' ? 'fa-caret-up' : 'fa-caret-down') : 'fa-sort' %} +
+
+ + {% block table_head %} + + {% for field, metadata in fields %} + {% set isSortingField = metadata.property == app.request.get('sortField')|split('.')|first %} + {% set nextSortDirection = isSortingField ? (app.request.get('sortDirection') == 'DESC' ? 'ASC' : 'DESC') : 'DESC' %} + {% set _column_label = (metadata.label ?: field|humanize)|trans(_trans_parameters) %} + {% set _column_icon = isSortingField ? (nextSortDirection == 'DESC' ? 'fa-caret-up' : 'fa-caret-down') : 'fa-sort' %} - - {% endfor %} + + {% endfor %} - {% if _list_item_actions|length > 0 %} - - {% endif %} - - {% endblock table_head %} - + {% if _list_item_actions|length > 0 %} + + {% endif %} + + {% endblock table_head %} + - - {% block table_body %} - {% for item in paginator.currentPageResults %} - {# the empty string concatenation is needed when the primary key is an object (e.g. an Uuid object) #} - {% set _item_id = '' ~ attribute(item, _entity_config.primary_key_field_name) %} - - {% for field, metadata in fields %} - {% set isSortingField = metadata.property == app.request.get('sortField') %} - {% set _column_label = (metadata.label ?: field|humanize)|trans(_trans_parameters) %} + + {% block table_body %} + {% for item in paginator.currentPageResults %} + {# the empty string concatenation is needed when the primary key is an object (e.g. an Uuid object) #} + {% set _item_id = '' ~ attribute(item, _object_config.primary_key_field_name) %} + + {% for field, metadata in fields %} + {% set isSortingField = metadata.property == app.request.get('sortField') %} + {% set _column_label = (metadata.label ?: field|humanize)|trans(_trans_parameters) %} - - {% endfor %} + + {% endfor %} - {% if _list_item_actions|length > 0 %} - {% set _column_label = 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') %} - - {% endif %} - - {% else %} - - - - {% endfor %} - {% endblock table_body %} - - - {% if _entity_config.embeddedList.open_new_tab %} - - - - - + {% if _list_item_actions|length > 0 %} + {% set _column_label = 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') %} + {% endif %} -
- {% if metadata.sortable %} - - - {{ _column_label|raw }} - - {% else %} - {{ _column_label|raw }} - {% endif %} - + {% if metadata.sortable %} + + + {{ _column_label|raw }} + + {% else %} + {{ _column_label|raw }} + {% endif %} + - {{ 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') }} -
+ {{ 'list.row_actions'|trans(_trans_parameters, 'EasyAdminBundle') }} +
- {{ easyadmin_render_field_for_list_view(_entity_config.name, item, metadata) }} - + {{ easyadmin_object_render_field_for_list_view(objectType, _object_config.name, item, metadata) }} + - {% block item_actions %} - {{ include('@EasyAdmin/default/includes/_actions.html.twig', { - actions: _list_item_actions|prune_item_actions(_entity_config, ['delete'], item), - request_parameters: _request_parameters, - translation_domain: _entity_config.translation_domain, - trans_parameters: _trans_parameters, - item_id: _item_id - }, with_context = false) }} - {% endblock item_actions %} -
- {{ 'search.no_results'|trans(_trans_parameters, 'EasyAdminBundle') }} -
- {% block open_new_tab %} - - - {{ 'open.new_tab'|trans({}, 'EasyAdminBundle') }} - - {% endblock open_new_tab %} -
+ {% block item_actions %} + {{ include('@EasyAdmin/default/includes/_actions.html.twig', { + actions: _list_item_actions|prune_item_actions(_object_config, ['delete'], item), + request_parameters: _request_parameters, + translation_domain: _object_config.translation_domain, + trans_parameters: _trans_parameters, + item_id: _item_id + }, with_context = false) }} + {% endblock item_actions %} +
-
+ + {% else %} + + + {{ 'search.no_results'|trans(_trans_parameters, 'EasyAdminBundle') }} + + + {% endfor %} + {% endblock table_body %} + + + {% if _object_config.embeddedList.open_new_tab %} + + + + {% block open_new_tab %} + + + {{ 'open.new_tab'|trans({}, 'EasyAdminBundle') }} + + {% endblock open_new_tab %} + + + + {% endif %} + +
- {% block paginator %}{{ include(_entity_config.templates.paginator) }}{% endblock paginator %} + {% block paginator %}{{ include(_object_config.templates.paginator) }}{% endblock paginator %} - +
{% endblock main %} diff --git a/src/Resources/views/default/field_embedded_list.html.twig b/src/Resources/views/default/field_embedded_list.html.twig index b42401d..bbc1f21 100644 --- a/src/Resources/views/default/field_embedded_list.html.twig +++ b/src/Resources/views/default/field_embedded_list.html.twig @@ -1,10 +1,16 @@ {% set default_filters = guess_default_filters( - field_options.template_options.entity_fqcn, field_options.template_options.parent_entity_property, item + field_options.template_options.object_fqcn, field_options.template_options.parent_object_property, item ) %} {% set filters = default_filters|merge(field_options.template_options.filters) %} -{% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with { - entity: field_options.template_options.entity, - filters: filters, - sort: field_options.template_options.sort - } only %} +{% set tpl_params = { + filters: filters, + sort: field_options.template_options.sort +} %} +{% if 'document' == field_options.template_options.object_type %} + {% set tpl_params = tpl_params|merge({document: field_options.template_options.document}) %} +{% else %} + {% set tpl_params = tpl_params|merge({entity: field_options.template_options.entity}) %} +{% endif %} + +{% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with tpl_params only %} diff --git a/src/Resources/views/form/bootstrap_3_layout.html.twig b/src/Resources/views/form/bootstrap_3_layout.html.twig index 051f14c..aacca05 100644 --- a/src/Resources/views/form/bootstrap_3_layout.html.twig +++ b/src/Resources/views/form/bootstrap_3_layout.html.twig @@ -1,16 +1,21 @@ {% use '@BaseEasyAdmin/form/bootstrap_3_layout.html.twig' %} {% block easyadmin_embedded_list_row %} - {% set default_filters = entity_fqcn is defined - ? guess_default_filters(entity_fqcn, parent_entity_property, form.parent.vars.data) + {% set default_filters = object_fqcn is defined + ? guess_default_filters(object_fqcn, parent_object_property, form.parent.vars.data) : [] %} {% set filters = default_filters|merge(filters) %} - {% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with { - entity: entity, + {% set tpl_params = { filters: filters, sort: sort|default(null) - } only %} + } %} + {% if 'document' == object_type %} + {% set tpl_params = tpl_params|merge({document: document}) %} + {% else %} + {% set tpl_params = tpl_params|merge({entity: entity}) %} + {% endif %} + {% include '@EasyAdmin/includes/_include_embedded_list.html.twig' with tpl_params only %} {% endblock %} {% block easyadmin_admin_roles_widget %} diff --git a/src/Resources/views/includes/_include_embedded_list.html.twig b/src/Resources/views/includes/_include_embedded_list.html.twig index 126b351..c59a372 100644 --- a/src/Resources/views/includes/_include_embedded_list.html.twig +++ b/src/Resources/views/includes/_include_embedded_list.html.twig @@ -1,4 +1,10 @@ -{% set action_params = { entity: entity, action: 'embeddedList', filters: filters } %} +{% set action_params = { action: 'embeddedList', filters: filters } %} +{% if document is defined %} + {% set action_params = action_params|merge({document: document}) %} +{% else %} + {% set action_params = action_params|merge({entity: entity}) %} +{% endif %} + {% if sort is not null %} {% set action_params = action_params|merge({ sortField: sort.field, sortDirection: sort.direction}) %} {% endif %} diff --git a/src/Twig/EasyAdminExtensionTwigExtension.php b/src/Twig/EasyAdminExtensionTwigExtension.php index 8cd84c1..15ed85a 100644 --- a/src/Twig/EasyAdminExtensionTwigExtension.php +++ b/src/Twig/EasyAdminExtensionTwigExtension.php @@ -30,9 +30,22 @@ public function getFunctions() new TwigFunction('easyadmin_object', array($this, 'getObjectConfiguration')), new TwigFunction('easyadmin_object_type', array($this, 'getObjectType')), new TwigFunction('easyadmin_path', array($this, 'getEasyAdminPath')), + new TwigFunction('easyadmin_object_render_field_for_*_view', array($this, 'renderObjectField'), array('is_safe' => array('html'), 'needs_environment' => true)), ); } + public function renderObjectField( + \Twig_Environment $twig, string $view, string $objectType, string $objectName, $item, array $fieldMetadata + ) { + if ('document' === $objectType && $twig->getFunction('easyadmin_mongo_odm_render_field_for_*_view')) { + $function = $twig->getFunction('easyadmin_mongo_odm_render_field_for_*_view'); + } else { + $function = $twig->getFunction('easyadmin_render_field_for_*_view'); + } + + return call_user_func($function->getCallable(), $twig, $view, $objectName, $item, $fieldMetadata); + } + /** * Returns the namespaced base Twig path. * diff --git a/src/Twig/EmbeddedListExtension.php b/src/Twig/EmbeddedListExtension.php index cc2330f..74d3056 100644 --- a/src/Twig/EmbeddedListExtension.php +++ b/src/Twig/EmbeddedListExtension.php @@ -3,6 +3,7 @@ namespace AlterPHP\EasyAdminExtensionBundle\Twig; use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; use Twig\TwigFunction; class EmbeddedListExtension extends AbstractExtension @@ -22,6 +23,13 @@ public function __construct($embeddedListHelper) $this->embeddedListHelper = $embeddedListHelper; } + public function getFilters() + { + return array( + new TwigFilter('embedded_list_identifier', array($this, 'getEmbeddedListIdentifier')), + ); + } + public function getFunctions() { return array( @@ -29,8 +37,13 @@ public function getFunctions() ); } - public function guessDefaultFilters(string $entityFqcn, string $parentEntityProperty, $parentEntity) + public function getEmbeddedListIdentifier(string $requestUri) + { + return md5($requestUri); + } + + public function guessDefaultFilters(string $objectFqcn, string $parentDocumentProperty, $parentDocument) { - return $this->embeddedListHelper->guessDefaultFilter($entityFqcn, $parentEntityProperty, $parentEntity); + return $this->embeddedListHelper->guessDefaultFilter($objectFqcn, $parentDocumentProperty, $parentDocument); } } diff --git a/tests/Configuration/ShowViewConfigPassTest.php b/tests/Configuration/ShowViewConfigPassTest.php index e1cade4..2f63d18 100644 --- a/tests/Configuration/ShowViewConfigPassTest.php +++ b/tests/Configuration/ShowViewConfigPassTest.php @@ -57,9 +57,10 @@ public function testDefaultEmbeddedListShow() 'sourceEntity' => 'App\Entity\MyEntity', 'template' => '@EasyAdminExtension/default/field_embedded_list.html.twig', 'template_options' => array( - 'entity_fqcn' => 'App\Entity\MyRelation', - 'parent_entity_property' => 'relations', + 'object_type' => 'entity', 'entity' => 'MyRelation', + 'object_fqcn' => 'App\Entity\MyRelation', + 'parent_object_property' => 'relations', 'filters' => array(), 'sort' => null, ), @@ -101,10 +102,10 @@ public function testDefinedEmbeddedListShow() 'sourceEntity' => 'App\Entity\MyEntity', 'template' => 'path/to/template.html.twig', 'template_options' => array( - 'parent_entity_fqcn' => 'Foo\Entity\MyEntity', - 'parent_entity_property' => 'children', - 'entity_fqcn' => 'Foo\Entity\Child', 'entity' => 'Child', + 'object_fqcn' => 'Foo\Entity\Child', + 'parent_object_fqcn' => 'Foo\Entity\MyEntity', + 'parent_object_property' => 'children', 'filters' => array('bar' => 'baz'), 'sort' => array('qux', 'ASC'), ), @@ -128,12 +129,13 @@ public function testDefinedEmbeddedListShow() 'sourceEntity' => 'App\Entity\MyEntity', 'template' => 'path/to/template.html.twig', 'template_options' => array( - 'parent_entity_fqcn' => 'Foo\Entity\MyEntity', - 'parent_entity_property' => 'children', - 'entity_fqcn' => 'Foo\Entity\Child', 'entity' => 'Child', + 'object_fqcn' => 'Foo\Entity\Child', + 'parent_object_fqcn' => 'Foo\Entity\MyEntity', + 'parent_object_property' => 'children', 'filters' => array('bar' => 'baz'), 'sort' => array('field' => 'qux', 'direction' => 'ASC'), + 'object_type' => 'entity', ), ), ), diff --git a/tests/Controller/EmbeddedListTest.php b/tests/Controller/EmbeddedListTest.php index 85da9aa..b33d50d 100644 --- a/tests/Controller/EmbeddedListTest.php +++ b/tests/Controller/EmbeddedListTest.php @@ -17,7 +17,7 @@ public function testManyToManyEmbedddLists() { $crawler = $this->requestEditView('AdminGroup', 1); - $forAttrValue = '/admin/?entity=AdminUser&action=embeddedList&filters%5Bentity.id%5D%5B0%5D=1&filters%5Bentity.id%5D%5B1%5D=2&filters%5Bentity.id%5D%5B2%5D=3&filters%5Bentity.id%5D%5B3%5D=4&filters%5Bentity.id%5D%5B4%5D=5&filters%5Bentity.id%5D%5B5%5D=6&filters%5Bentity.id%5D%5B6%5D=7&filters%5Bentity.id%5D%5B7%5D=8&filters%5Bentity.id%5D%5B8%5D=9&filters%5Bentity.id%5D%5B9%5D=10&filters%5Bentity.id%5D%5B10%5D=11&filters%5Bentity.id%5D%5B11%5D=12&filters%5Bentity.id%5D%5B12%5D=13&filters%5Bentity.id%5D%5B13%5D=14&filters%5Bentity.id%5D%5B14%5D=15&filters%5Bentity.id%5D%5B15%5D=16&filters%5Bentity.id%5D%5B16%5D=17&filters%5Bentity.id%5D%5B17%5D=18&filters%5Bentity.id%5D%5B18%5D=19&filters%5Bentity.id%5D%5B19%5D=20'; + $forAttrValue = md5('/admin/?action=embeddedList&filters%5Bentity.id%5D%5B0%5D=1&filters%5Bentity.id%5D%5B1%5D=2&filters%5Bentity.id%5D%5B2%5D=3&filters%5Bentity.id%5D%5B3%5D=4&filters%5Bentity.id%5D%5B4%5D=5&filters%5Bentity.id%5D%5B5%5D=6&filters%5Bentity.id%5D%5B6%5D=7&filters%5Bentity.id%5D%5B7%5D=8&filters%5Bentity.id%5D%5B8%5D=9&filters%5Bentity.id%5D%5B9%5D=10&filters%5Bentity.id%5D%5B10%5D=11&filters%5Bentity.id%5D%5B11%5D=12&filters%5Bentity.id%5D%5B12%5D=13&filters%5Bentity.id%5D%5B13%5D=14&filters%5Bentity.id%5D%5B14%5D=15&filters%5Bentity.id%5D%5B15%5D=16&filters%5Bentity.id%5D%5B16%5D=17&filters%5Bentity.id%5D%5B17%5D=18&filters%5Bentity.id%5D%5B18%5D=19&filters%5Bentity.id%5D%5B19%5D=20&entity=AdminUser'); $this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"]')->count()); } @@ -26,7 +26,7 @@ public function testEmbeddedListIsDisplaidInEdit() { $crawler = $this->requestEditView('Category', 1); - $forAttrValue = '/admin/?entity=Product&action=embeddedList&filters%5Bentity.category%5D=1'; + $forAttrValue = md5('/admin/?action=embeddedList&filters%5Bentity.category%5D=1&entity=Product'); $this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"]')->count()); } @@ -34,7 +34,7 @@ public function testEmbeddedListIsDisplaidInShow() { $crawler = $this->requestShowView('Category', 1); - $forAttrValue = '/admin/?entity=Product&action=embeddedList&filters%5Bentity.category%5D=1'; + $forAttrValue = md5('/admin/?action=embeddedList&filters%5Bentity.category%5D=1&entity=Product'); $this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"]')->count()); } @@ -140,7 +140,7 @@ public function testListSortIsUsedForEmbedddLists() { $crawler = $this->requestEditView('Category', 1); - $forAttrValue = '/admin/?entity=Product&action=embeddedList&filters%5Bentity.category%5D=1'; + $forAttrValue = md5('/admin/?action=embeddedList&filters%5Bentity.category%5D=1&entity=Product'); $createdAtTh = 'th[data-property-name="createdAt"].sorted'; $this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"] '.$createdAtTh)->count()); @@ -150,7 +150,7 @@ public function testDefinedSortIsUsedForEmbedddLists() { $crawler = $this->getBackendPage(array('entity' => 'Purchase', 'action' => 'embeddedList')); - $forAttrValue = '/admin/?entity=Purchase&action=embeddedList'; + $forAttrValue = md5('/admin/?entity=Purchase&action=embeddedList'); $createdAtTh = 'th[data-property-name="createdAt"].sorted'; $this->assertSame(1, $crawler->filter('.embedded-list[for="'.$forAttrValue.'"] '.$createdAtTh)->count());