Skip to content

Commit

Permalink
fix(Attributes): Fixed AttributeValueRepository findByAttributable me…
Browse files Browse the repository at this point in the history
…thod preventing fetch attribute values without translations. Fixed AttributeChoiceType still requesting entityManager.
  • Loading branch information
ambroisemaupate committed Jun 17, 2024
1 parent 7b7b3b2 commit 1ce795e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
16 changes: 9 additions & 7 deletions lib/RoadizCoreBundle/src/Form/AttributeChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RZ\Roadiz\CoreBundle\Form;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Entity\Attribute;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use Symfony\Component\Form\AbstractType;
Expand All @@ -14,8 +14,12 @@
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AttributeChoiceType extends AbstractType
final class AttributeChoiceType extends AbstractType
{
public function __construct(private ManagerRegistry $managerRegistry)
{
}

/**
* @inheritDoc
*/
Expand All @@ -28,8 +32,8 @@ function ($dataToForm) {
}
return null;
},
function ($formToData) use ($options) {
return $options['entityManager']->find(Attribute::class, $formToData);
function ($formToData) {
return $this->managerRegistry->getRepository(Attribute::class)->find($formToData);
}
));
}
Expand All @@ -41,14 +45,12 @@ public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver->setDefault('empty_data', null);
$resolver->setRequired('entityManager');
$resolver->setAllowedTypes('entityManager', [EntityManagerInterface::class]);
$resolver->setRequired('translation');
$resolver->setAllowedTypes('translation', [Translation::class]);
$resolver->setNormalizer('choices', function (Options $options) {
$choices = [];
/** @var Attribute[] $attributes */
$attributes = $options['entityManager']->getRepository(Attribute::class)->findBy(
$attributes = $this->managerRegistry->getRepository(Attribute::class)->findBy(
[],
['code' => 'ASC']
);
Expand Down
3 changes: 0 additions & 3 deletions lib/RoadizCoreBundle/src/Form/AttributeValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('attribute', AttributeChoiceType::class, [
'label' => 'attribute_values.form.attribute',
'entityManager' => $options['entityManager'],
'translation' => $options['translation'],
]);
}
Expand All @@ -31,8 +30,6 @@ public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

$resolver->setRequired('entityManager');
$resolver->setAllowedTypes('entityManager', [EntityManagerInterface::class]);
$resolver->setRequired('translation');
$resolver->setAllowedTypes('translation', [Translation::class]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function findByAttributable(
->addSelect('ad')
->addSelect('ag')
->addSelect('agt')
->innerJoin('av.attributeValueTranslations', 'avt')
// We need to fetch values without translations too
->leftJoin('av.attributeValueTranslations', 'avt')
->innerJoin('av.attribute', 'a')
->leftJoin('a.attributeDocuments', 'ad')
->leftJoin('a.attributeTranslations', 'at')
Expand Down
13 changes: 12 additions & 1 deletion lib/Rozier/src/Controllers/Nodes/NodesAttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ protected function handleAddAttributeForm(Request $request, Node $node, Translat
$attributeValue = new AttributeValue();
$attributeValue->setAttributable($node);
$addAttributeForm = $this->createForm(AttributeValueType::class, $attributeValue, [
'entityManager' => $this->em(),
'translation' => $this->em()->getRepository(Translation::class)->findDefault(),
]);
$addAttributeForm->handleRequest($request);
Expand All @@ -210,6 +209,18 @@ protected function handleAddAttributeForm(Request $request, Node $node, Translat
$this->em()->persist($attributeValue);
$this->em()->flush();

$nodeSource = $node->getNodeSourcesByTranslation($translation)->first() ?: null;
if ($nodeSource instanceof NodesSources) {
$msg = $this->getTranslator()->trans(
'attribute_value_translation.%name%.updated_from_node.%nodeName%',
[
'%name%' => $attributeValue->getAttribute()->getLabelOrCode($translation),
'%nodeName%' => $nodeSource->getTitle(),
]
);
$this->publishConfirmMessage($request, $msg, $nodeSource);
}

return $this->redirectToRoute('nodesEditAttributesPage', [
'nodeId' => $node->getId(),
'translationId' => $translation->getId(),
Expand Down

0 comments on commit 1ce795e

Please sign in to comment.