Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FieldDescriptionInterface::getTargetModel if exists #6208

Merged
merged 1 commit into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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