Skip to content

Commit

Permalink
Merge pull request #4 from delyriand/fix/fallback-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran authored Jan 30, 2025
2 parents bd5854c + 04d95ef commit 6709f01
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/Form/Type/UiElement/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MonsieurBiz\SyliusCmsBlockPlugin\Repository\BlockRepositoryInterface;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -28,6 +29,7 @@ final class BlockType extends AbstractType
public function __construct(
private BlockRepositoryInterface $blockRepository,
private LocaleContextInterface $localeContext,
private TranslationLocaleProviderInterface $translationLocaleProvider,
) {
}

Expand All @@ -49,7 +51,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'multiple' => false,
'choice_label' => fn (BlockInterface $block): string => \sprintf('[%s] %s', $block->getCode(), $block->getName()),
'query_builder' => function (BlockRepository $blockRepository) {
return $blockRepository->createListQueryBuilder($this->localeContext->getLocaleCode())
return $blockRepository->createListQueryBuilder($this->localeContext->getLocaleCode(), $this->translationLocaleProvider->getDefaultLocaleCode())
->addOrderBy('o.code', 'ASC')
;
},
Expand All @@ -59,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
;

$reversedTransformer = new ReversedTransformer(new ResourceToIdentifierTransformer($this->blockRepository));
$reversedTransformer = new ReversedTransformer(new ResourceToIdentifierTransformer($this->blockRepository, 'code'));
$builder->get('block')->addModelTransformer($reversedTransformer);
}
}
44 changes: 37 additions & 7 deletions src/Repository/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,57 @@

class BlockRepository extends EntityRepository implements BlockRepositoryInterface
{
public function createListQueryBuilder(string $localeCode): QueryBuilder
public function createListQueryBuilder(string $localeCode, ?string $fallbackLocaleCode = null): QueryBuilder
{
return $this->createQueryBuilder('o')
$queryBuilder = $this->createQueryBuilder('o')
->addSelect('translation')
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
->setParameter('localeCode', $localeCode)
;
if (null !== $fallbackLocaleCode && $fallbackLocaleCode !== $localeCode) {
$queryBuilder
->addSelect('fallbackTranslation')
->leftJoin('o.translations', 'fallbackTranslation', 'WITH', 'fallbackTranslation.locale = :fallbackLocaleCode')
->setParameter('fallbackLocaleCode', $fallbackLocaleCode)
;
}

return $queryBuilder;
}

/**
* @throws NonUniqueResultException
*/
public function findOneEnabledByCode(string $code): ?BlockInterface
public function findOneEnabledByCode(string $code, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface
{
/** @phpstan-ignore-next-line */
return $this->createQueryBuilder('b')
->andWhere('b.code = :code')
->andWhere('b.enabled = true')
$queryBuilder = $locale ? $this->createListQueryBuilder($locale, $fallbackLocaleCode) : $this->createQueryBuilder('o');

/** @var ?BlockInterface */
return $queryBuilder
->andWhere('o.code = :code')
->andWhere('o.enabled = true')
->setParameter('code', $code)
->getQuery()
->getOneOrNullResult()
;
}

public function findOneEnabledByIdentifier(string $identifier, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface
{
$block = $this->findOneEnabledByCode($identifier, $locale, $fallbackLocaleCode);
if (null !== $block) {
return $block;
}

$queryBuilder = $locale ? $this->createListQueryBuilder($locale, $fallbackLocaleCode) : $this->createQueryBuilder('o');

/** @var ?BlockInterface */
return $queryBuilder
->andWhere('o.id = :id')
->andWhere('o.enabled = true')
->setParameter('id', $identifier)
->getQuery()
->getOneOrNullResult()
;
}
}
13 changes: 11 additions & 2 deletions src/Repository/BlockRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@

namespace MonsieurBiz\SyliusCmsBlockPlugin\Repository;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use MonsieurBiz\SyliusCmsBlockPlugin\Entity\BlockInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

interface BlockRepositoryInterface extends RepositoryInterface
{
public function createListQueryBuilder(string $localeCode): QueryBuilder;
public function createListQueryBuilder(string $localeCode, ?string $fallbackLocaleCode = null): QueryBuilder;

public function findOneEnabledByCode(string $code): ?BlockInterface;
/**
* @throws NonUniqueResultException
*/
public function findOneEnabledByCode(string $code, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface;

/**
* @throws NonUniqueResultException
*/
public function findOneEnabledByIdentifier(string $identifier, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface;
}
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ monsieurbiz_cms_block:
cms_content: "CMS content"
blocks_subheader: "CMS block management"
back_to_admin: "Back to admin"
block_not_found: 'Block "%name%" not found.'
form:
block_info: 'Block information'
block_content: 'Block content'
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ monsieurbiz_cms_block:
cms_content: "Contenu CMS"
blocks_subheader: "Gestion des blocs CMS"
back_to_admin: "Retour à l'administration"
block_not_found: 'Le bloc "%name%" n''a pas été trouvé.'
form:
block_info: 'Informations sur le bloc'
block_content: 'Contenu du bloc'
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/views/Admin/UiElement/block.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ element methods:
{% set blockEntity = ui_element.getBlock(element.block) %}
{% if blockEntity and blockEntity.enabled %}
{{ blockEntity.content|monsieurbiz_richeditor_render_field }}
{% else %}
<div class="ui orange message">
{{ 'monsieurbiz_cms_block.ui.block_not_found'|trans({'%name%': element.block}) }}
</div>
{% endif %}
18 changes: 15 additions & 3 deletions src/UiElement/BlockUiElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@

namespace MonsieurBiz\SyliusCmsBlockPlugin\UiElement;

use Doctrine\ORM\NonUniqueResultException;
use MonsieurBiz\SyliusCmsBlockPlugin\Entity\BlockInterface;
use MonsieurBiz\SyliusCmsBlockPlugin\Repository\BlockRepositoryInterface;
use MonsieurBiz\SyliusRichEditorPlugin\UiElement\UiElementInterface;
use MonsieurBiz\SyliusRichEditorPlugin\UiElement\UiElementTrait;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Resource\Translation\Provider\TranslationLocaleProviderInterface;

final class BlockUiElement implements UiElementInterface
{
use UiElementTrait;

public function __construct(
private BlockRepositoryInterface $blockRepository,
private LocaleContextInterface $localeContext,
private TranslationLocaleProviderInterface $translationLocaleProvider,
) {
}

public function getBlock(string $id): ?BlockInterface
public function getBlock(string $identifier): ?BlockInterface
{
/** @phpstan-ignore-next-line */
return $this->blockRepository->find($id);
try {
return $this->blockRepository->findOneEnabledByIdentifier(
$identifier,
$this->localeContext->getLocaleCode(),
$this->translationLocaleProvider->getDefaultLocaleCode()
);
} catch (NonUniqueResultException) {
return null;
}
}
}

0 comments on commit 6709f01

Please sign in to comment.