Skip to content

Commit

Permalink
Use FieldDescriptionInterface::getTargetModel if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and jordisala1991 committed Jul 18, 2020
1 parent 31e7c24 commit dbd9087
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/Action/RetrieveAutocompleteItemsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
10 changes: 9 additions & 1 deletion src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
}
11 changes: 9 additions & 2 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit dbd9087

Please sign in to comment.