Skip to content

Commit 6709f01

Browse files
authored
Merge pull request #4 from delyriand/fix/fallback-locale
2 parents bd5854c + 04d95ef commit 6709f01

File tree

7 files changed

+73
-14
lines changed

7 files changed

+73
-14
lines changed

src/Form/Type/UiElement/BlockType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use MonsieurBiz\SyliusCmsBlockPlugin\Repository\BlockRepositoryInterface;
1818
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
1919
use Sylius\Component\Locale\Context\LocaleContextInterface;
20+
use Sylius\Resource\Translation\Provider\TranslationLocaleProviderInterface;
2021
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
2122
use Symfony\Component\Form\AbstractType;
2223
use Symfony\Component\Form\FormBuilderInterface;
@@ -28,6 +29,7 @@ final class BlockType extends AbstractType
2829
public function __construct(
2930
private BlockRepositoryInterface $blockRepository,
3031
private LocaleContextInterface $localeContext,
32+
private TranslationLocaleProviderInterface $translationLocaleProvider,
3133
) {
3234
}
3335

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

62-
$reversedTransformer = new ReversedTransformer(new ResourceToIdentifierTransformer($this->blockRepository));
64+
$reversedTransformer = new ReversedTransformer(new ResourceToIdentifierTransformer($this->blockRepository, 'code'));
6365
$builder->get('block')->addModelTransformer($reversedTransformer);
6466
}
6567
}

src/Repository/BlockRepository.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,57 @@
1818

1919
class BlockRepository extends EntityRepository implements BlockRepositoryInterface
2020
{
21-
public function createListQueryBuilder(string $localeCode): QueryBuilder
21+
public function createListQueryBuilder(string $localeCode, ?string $fallbackLocaleCode = null): QueryBuilder
2222
{
23-
return $this->createQueryBuilder('o')
23+
$queryBuilder = $this->createQueryBuilder('o')
2424
->addSelect('translation')
2525
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
2626
->setParameter('localeCode', $localeCode)
2727
;
28+
if (null !== $fallbackLocaleCode && $fallbackLocaleCode !== $localeCode) {
29+
$queryBuilder
30+
->addSelect('fallbackTranslation')
31+
->leftJoin('o.translations', 'fallbackTranslation', 'WITH', 'fallbackTranslation.locale = :fallbackLocaleCode')
32+
->setParameter('fallbackLocaleCode', $fallbackLocaleCode)
33+
;
34+
}
35+
36+
return $queryBuilder;
2837
}
2938

3039
/**
3140
* @throws NonUniqueResultException
3241
*/
33-
public function findOneEnabledByCode(string $code): ?BlockInterface
42+
public function findOneEnabledByCode(string $code, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface
3443
{
35-
/** @phpstan-ignore-next-line */
36-
return $this->createQueryBuilder('b')
37-
->andWhere('b.code = :code')
38-
->andWhere('b.enabled = true')
44+
$queryBuilder = $locale ? $this->createListQueryBuilder($locale, $fallbackLocaleCode) : $this->createQueryBuilder('o');
45+
46+
/** @var ?BlockInterface */
47+
return $queryBuilder
48+
->andWhere('o.code = :code')
49+
->andWhere('o.enabled = true')
3950
->setParameter('code', $code)
4051
->getQuery()
4152
->getOneOrNullResult()
4253
;
4354
}
55+
56+
public function findOneEnabledByIdentifier(string $identifier, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface
57+
{
58+
$block = $this->findOneEnabledByCode($identifier, $locale, $fallbackLocaleCode);
59+
if (null !== $block) {
60+
return $block;
61+
}
62+
63+
$queryBuilder = $locale ? $this->createListQueryBuilder($locale, $fallbackLocaleCode) : $this->createQueryBuilder('o');
64+
65+
/** @var ?BlockInterface */
66+
return $queryBuilder
67+
->andWhere('o.id = :id')
68+
->andWhere('o.enabled = true')
69+
->setParameter('id', $identifier)
70+
->getQuery()
71+
->getOneOrNullResult()
72+
;
73+
}
4474
}

src/Repository/BlockRepositoryInterface.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111

1212
namespace MonsieurBiz\SyliusCmsBlockPlugin\Repository;
1313

14+
use Doctrine\ORM\NonUniqueResultException;
1415
use Doctrine\ORM\QueryBuilder;
1516
use MonsieurBiz\SyliusCmsBlockPlugin\Entity\BlockInterface;
1617
use Sylius\Component\Resource\Repository\RepositoryInterface;
1718

1819
interface BlockRepositoryInterface extends RepositoryInterface
1920
{
20-
public function createListQueryBuilder(string $localeCode): QueryBuilder;
21+
public function createListQueryBuilder(string $localeCode, ?string $fallbackLocaleCode = null): QueryBuilder;
2122

22-
public function findOneEnabledByCode(string $code): ?BlockInterface;
23+
/**
24+
* @throws NonUniqueResultException
25+
*/
26+
public function findOneEnabledByCode(string $code, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface;
27+
28+
/**
29+
* @throws NonUniqueResultException
30+
*/
31+
public function findOneEnabledByIdentifier(string $identifier, ?string $locale = null, ?string $fallbackLocaleCode = null): ?BlockInterface;
2332
}

src/Resources/translations/messages.en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ monsieurbiz_cms_block:
77
cms_content: "CMS content"
88
blocks_subheader: "CMS block management"
99
back_to_admin: "Back to admin"
10+
block_not_found: 'Block "%name%" not found.'
1011
form:
1112
block_info: 'Block information'
1213
block_content: 'Block content'

src/Resources/translations/messages.fr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ monsieurbiz_cms_block:
77
cms_content: "Contenu CMS"
88
blocks_subheader: "Gestion des blocs CMS"
99
back_to_admin: "Retour à l'administration"
10+
block_not_found: 'Le bloc "%name%" n''a pas été trouvé.'
1011
form:
1112
block_info: 'Informations sur le bloc'
1213
block_content: 'Contenu du bloc'

src/Resources/views/Admin/UiElement/block.html.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ element methods:
1010
{% set blockEntity = ui_element.getBlock(element.block) %}
1111
{% if blockEntity and blockEntity.enabled %}
1212
{{ blockEntity.content|monsieurbiz_richeditor_render_field }}
13+
{% else %}
14+
<div class="ui orange message">
15+
{{ 'monsieurbiz_cms_block.ui.block_not_found'|trans({'%name%': element.block}) }}
16+
</div>
1317
{% endif %}

src/UiElement/BlockUiElement.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,35 @@
1111

1212
namespace MonsieurBiz\SyliusCmsBlockPlugin\UiElement;
1313

14+
use Doctrine\ORM\NonUniqueResultException;
1415
use MonsieurBiz\SyliusCmsBlockPlugin\Entity\BlockInterface;
1516
use MonsieurBiz\SyliusCmsBlockPlugin\Repository\BlockRepositoryInterface;
1617
use MonsieurBiz\SyliusRichEditorPlugin\UiElement\UiElementInterface;
1718
use MonsieurBiz\SyliusRichEditorPlugin\UiElement\UiElementTrait;
19+
use Sylius\Component\Locale\Context\LocaleContextInterface;
20+
use Sylius\Resource\Translation\Provider\TranslationLocaleProviderInterface;
1821

1922
final class BlockUiElement implements UiElementInterface
2023
{
2124
use UiElementTrait;
2225

2326
public function __construct(
2427
private BlockRepositoryInterface $blockRepository,
28+
private LocaleContextInterface $localeContext,
29+
private TranslationLocaleProviderInterface $translationLocaleProvider,
2530
) {
2631
}
2732

28-
public function getBlock(string $id): ?BlockInterface
33+
public function getBlock(string $identifier): ?BlockInterface
2934
{
30-
/** @phpstan-ignore-next-line */
31-
return $this->blockRepository->find($id);
35+
try {
36+
return $this->blockRepository->findOneEnabledByIdentifier(
37+
$identifier,
38+
$this->localeContext->getLocaleCode(),
39+
$this->translationLocaleProvider->getDefaultLocaleCode()
40+
);
41+
} catch (NonUniqueResultException) {
42+
return null;
43+
}
3244
}
3345
}

0 commit comments

Comments
 (0)