[Admin][Taxon] Fallback from parent code to id when generating a slug

This commit is contained in:
Jan Goralski 2023-08-25 10:48:04 +02:00
parent 2d5aa6864f
commit a24c696fdf
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
2 changed files with 16 additions and 6 deletions

View file

@ -106,8 +106,6 @@ Feature: Editing taxon's slug
And I save my changes
Then the slug of the "Renaissance weapons" taxon should be "renaissance"
@ui @javascript
Scenario: Automatically changing a child taxon's slug when changing the parent
Given the store has "Renaissance weapons" taxonomy

View file

@ -39,7 +39,6 @@ final class TaxonSlugController
}
$locale = (string) $request->query->get('locale');
$parentCode = $request->query->get('parentCode');
/** @var TaxonInterface $taxon */
$taxon = $this->taxonFactory->createNew();
@ -47,12 +46,25 @@ final class TaxonSlugController
$taxon->setFallbackLocale($locale);
$taxon->setName($name);
if (null !== $parentCode) {
$taxon->setParent($this->taxonRepository->findOneBy(['code' => $parentCode]));
}
$taxon->setParent($this->getParentTaxon($request));
return new JsonResponse([
'slug' => $this->taxonSlugGenerator->generate($taxon, $locale),
]);
}
private function getParentTaxon(Request $request): ?TaxonInterface
{
$parentCode = $request->query->get('parentCode');
if (null !== $parentCode) {
return $this->taxonRepository->findOneBy(['code' => $parentCode]);
}
$parentId = $request->query->get('parentId');
if (null !== $parentId) {
return $this->taxonRepository->find($parentId);
}
return null;
}
}