From 5235415d9d0a21f82eb516bb140fbddd35a2a8eb Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Fri, 17 Jul 2020 23:18:05 +0200 Subject: [PATCH] Use FieldDescriptionInterface::getTargetModel if exists --- src/Action/RetrieveAutocompleteItemsAction.php | 18 ++++++++++++++++-- src/Action/SetObjectFieldValueAction.php | 10 +++++++++- src/Admin/AbstractAdmin.php | 11 +++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Action/RetrieveAutocompleteItemsAction.php b/src/Action/RetrieveAutocompleteItemsAction.php index e6c64332d4..19b5a21a08 100644 --- a/src/Action/RetrieveAutocompleteItemsAction.php +++ b/src/Action/RetrieveAutocompleteItemsAction.php @@ -190,7 +190,14 @@ private function retrieveFilterFieldDescription( throw new \RuntimeException(sprintf('The field "%s" does not exist.', $field)); } - if (null === $fieldDescription->getTargetModel()) { + // NEXT_MAJOR: Remove the check and use `getTargetModel`. + if (method_exists($fieldDescription, 'getTargetModel')) { + $targetModel = $fieldDescription->getTargetModel(); + } else { + $targetModel = $fieldDescription->getTargetEntity(); + } + + if (null === $targetModel) { throw new \RuntimeException(sprintf('No associated entity with field "%s".', $field)); } @@ -212,7 +219,14 @@ private function retrieveFormFieldDescription( throw new \RuntimeException(sprintf('The field "%s" does not exist.', $field)); } - if (null === $fieldDescription->getTargetModel()) { + // NEXT_MAJOR: Remove the check and use `getTargetModel`. + if (method_exists($fieldDescription, 'getTargetModel')) { + $targetModel = $fieldDescription->getTargetModel(); + } else { + $targetModel = $fieldDescription->getTargetEntity(); + } + + if (null === $targetModel) { throw new \RuntimeException(sprintf('No associated entity with field "%s".', $field)); } diff --git a/src/Action/SetObjectFieldValueAction.php b/src/Action/SetObjectFieldValueAction.php index 71c5e9bf8e..dcc805a26f 100644 --- a/src/Action/SetObjectFieldValueAction.php +++ b/src/Action/SetObjectFieldValueAction.php @@ -13,6 +13,7 @@ namespace Sonata\AdminBundle\Action; +use Sonata\AdminBundle\Admin\FieldDescriptionInterface; use Sonata\AdminBundle\Admin\Pool; use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension; use Symfony\Component\HttpFoundation\JsonResponse; @@ -132,7 +133,8 @@ public function __invoke(Request $request): JsonResponse if ('' !== $value && 'choice' === $fieldDescription->getType() && null !== $fieldDescription->getOption('class') - && $fieldDescription->getOption('class') === $fieldDescription->getTargetModel() + // NEXT_MAJOR: Replace this call with "$fieldDescription->getOption('class') === $fieldDescription->getTargetModel()". + && $this->hasFieldDescriptionAssociationWithClass($fieldDescription, $fieldDescription->getOption('class')) ) { $value = $admin->getModelManager()->find($fieldDescription->getOption('class'), $value); @@ -170,4 +172,10 @@ public function __invoke(Request $request): JsonResponse return new JsonResponse($content, Response::HTTP_OK); } + + private function hasFieldDescriptionAssociationWithClass(FieldDescriptionInterface $fieldDescription, string $class): bool + { + return (method_exists($fieldDescription, 'getTargetModel') && $class === $fieldDescription->getTargetModel()) + || $class === $fieldDescription->getTargetEntity(); + } } diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 492f027565..1319dde9de 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -1434,11 +1434,18 @@ public function attachAdminClass(FieldDescriptionInterface $fieldDescription) $admin = $pool->getAdminByAdminCode($adminCode); } else { - if (!$pool->hasAdminByClass($fieldDescription->getTargetModel())) { + // NEXT_MAJOR: Remove the check and use `getTargetModel`. + if (method_exists($fieldDescription, 'getTargetModel')) { + $targetModel = $fieldDescription->getTargetModel(); + } else { + $targetModel = $fieldDescription->getTargetEntity(); + } + + if (!$pool->hasAdminByClass($targetModel)) { return; } - $admin = $pool->getAdminByClass($fieldDescription->getTargetModel()); + $admin = $pool->getAdminByClass($targetModel); } if ($this->hasRequest()) {