Skip to content

Commit

Permalink
refactor(ArticlesSelection): use query builder when using an EntityTy…
Browse files Browse the repository at this point in the history
…pe field
  • Loading branch information
etienne-monsieurbiz committed Oct 23, 2024
1 parent 1e9e3bf commit 3945a3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
18 changes: 8 additions & 10 deletions src/Form/Type/ArticleSelectionElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,21 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$articles = $this->articleRepository->createShopListQueryBuilderByType(
$this->localeContext->getLocaleCode(),
ArticleInterface::BLOG_TYPE,
$this->channelContext->getChannel(),
null
);

$articles = $articles->orderBy('translation.title')->getQuery()->getResult();

$builder
->add('article', EntityType::class, [
'class' => Article::class,
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.article',
'choice_label' => fn (Article $article) => $article->getTitle(),
'choice_value' => fn (?Article $article) => $article?->getId(),
'required' => true,
'choices' => $articles,
'query_builder' => function (ArticleRepositoryInterface $articleRepository) {
return $articleRepository->createShopListQueryBuilderByType(
$this->localeContext->getLocaleCode(),
ArticleInterface::BLOG_TYPE,
$this->channelContext->getChannel(),
null
)->orderBy('translation.title');
},
])
->add('position', IntegerType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_selection_ui_element.fields.position',
Expand Down
20 changes: 9 additions & 11 deletions src/Form/Type/CaseStudyElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,21 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$caseStudies = $this->articleRepository->createShopListQueryBuilderByType(
$this->localeContext->getLocaleCode(),
ArticleInterface::CASE_STUDY_TYPE,
$this->channelContext->getChannel(),
null
);

$caseStudies = $caseStudies->orderBy('translation.title')->getQuery()->getResult();

$builder
->add('case_study', EntityType::class, [
'class' => Article::class,
'label' => 'monsieurbiz_blog.ui_element.case_studies_ui_element.fields.case_study',
'choice_label' => fn (Article $caseStudy) => $caseStudy->getTitle(),
'choice_value' => fn (?Article $caseStudy) => $caseStudy ? $caseStudy->getId() : null,
'choice_value' => fn (?Article $caseStudy) => $caseStudy?->getId(),
'required' => true,
'choices' => $caseStudies,
'query_builder' => function (ArticleRepositoryInterface $articleRepository) {
return $articleRepository->createShopListQueryBuilderByType(
$this->localeContext->getLocaleCode(),
ArticleInterface::CASE_STUDY_TYPE,
$this->channelContext->getChannel(),
null
)->orderBy('translation.title');
},
])
->add('position', IntegerType::class, [
'label' => 'monsieurbiz_blog.ui_element.case_studies_ui_element.fields.position',
Expand Down
12 changes: 5 additions & 7 deletions src/Form/Type/UiElement/ArticlesByTagsUiElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$tags = $this->tagRepository->createListQueryBuilder(
$this->localeContext->getLocaleCode(),
);

$tags = $tags->orderBy('translation.name')->getQuery()->getResult();

$builder
->add('title', TextType::class, [
'label' => 'monsieurbiz_blog.ui_element.articles_by_tags_ui_element.fields.title',
Expand All @@ -72,7 +66,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'class' => Tag::class,
'choice_label' => fn (Tag $tag) => $tag->getName(),
'choice_value' => fn (?Tag $tag) => $tag?->getId(),
'choices' => $tags,
'query_builder' => function (TagRepositoryInterface $tagRepository) {
return $tagRepository->createListQueryBuilder(
$this->localeContext->getLocaleCode(),
)->orderBy('translation.name');
},
'multiple' => true,
])
->add('limit', IntegerType::class, [
Expand Down

0 comments on commit 3945a3e

Please sign in to comment.