diff --git a/code_samples/search/language/config/append_to_routes.yaml b/code_samples/search/language/config/append_to_routes.yaml new file mode 100644 index 0000000000..a8980d0364 --- /dev/null +++ b/code_samples/search/language/config/append_to_routes.yaml @@ -0,0 +1,4 @@ +list_articles_to_translate: + path: /list/articles_to_translate/{language} + defaults: + _controller: 'App\Controller\ArticlesToTranslateController::listView' diff --git a/code_samples/search/language/src/Command/SearchTestCommand.php b/code_samples/search/language/src/Command/SearchTestCommand.php new file mode 100644 index 0000000000..716ef32ff1 --- /dev/null +++ b/code_samples/search/language/src/Command/SearchTestCommand.php @@ -0,0 +1,50 @@ +searchService = $searchService; + } + + protected function configure(): void + { + $this->setDescription('Search test.') + ->addArgument('text', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $text = implode(' ', $input->getArgument('text')); + + $query = new Query(['query' => new Criterion\LogicalAnd([ + new Criterion\FullText($text), + new Criterion\LanguageCode(['eng-GB'], false), + ])]); + + $results = $this->searchService->findContent($query, ['languages' => ['eng-GB', 'fre-FR', 'ger-DE']]); + foreach ($results->searchHits as $searchHit) { + /** @var \Ibexa\Core\Repository\Values\Content\Content $content */ + $content = $searchHit->valueObject; + dump($content->getName('eng-GB')); + } + + return Command::SUCCESS; + } +} diff --git a/code_samples/search/language/src/Controller/ArticlesToTranslateController.php b/code_samples/search/language/src/Controller/ArticlesToTranslateController.php new file mode 100644 index 0000000000..47b117518a --- /dev/null +++ b/code_samples/search/language/src/Controller/ArticlesToTranslateController.php @@ -0,0 +1,43 @@ +searchService = $searchService; + } + + public function listView(Request $request): Response + { + $languageCode = $request->get('language'); + + $query = new Query(); + $query->query = new Criterion\LogicalAnd([ + new Criterion\ContentTypeIdentifier('article'), + new Criterion\LogicalNot( + new Criterion\LanguageCode($languageCode, false) + ), + ]); + + $results = $this->searchService->findContent($query); + $articles = []; + foreach ($results->searchHits as $searchHit) { + $articles[] = $searchHit->valueObject; + } + + return $this->render('@ibexadesign/list/articles_to_translate.html.twig', [ + 'articles' => $articles, + ]); + } +} diff --git a/code_samples/search/language/templates/themes/standard/list/articles_to_translate.html.twig b/code_samples/search/language/templates/themes/standard/list/articles_to_translate.html.twig new file mode 100644 index 0000000000..6fa995e9e6 --- /dev/null +++ b/code_samples/search/language/templates/themes/standard/list/articles_to_translate.html.twig @@ -0,0 +1,9 @@ +{% extends '@ibexadesign/pagelayout.html.twig' %} + +{% block content %} + +{% endblock %} diff --git a/docs/search/criteria_reference/languagecode_criterion.md b/docs/search/criteria_reference/languagecode_criterion.md index 775502a21a..c45bc1eef1 100644 --- a/docs/search/criteria_reference/languagecode_criterion.md +++ b/docs/search/criteria_reference/languagecode_criterion.md @@ -39,28 +39,18 @@ $query->query = new Criterion\LanguageCode('ger-DE', false); } ``` -## Use case +## Use cases You can use the `LanguageCode` Criterion to search for articles that are lacking a translation into a specific language: ``` php hl_lines="5" -$query = new LocationQuery; -$query->query = new Criterion\LogicalAnd([ - new Criterion\ContentTypeIdentifier('article'), - new Criterion\LogicalNot( - new Criterion\LanguageCode('ger-DE', false) - ) - ] -); - -$results = $this->searchService->findContent($query); -$articles = []; -foreach ($results->searchHits as $searchHit) { - $articles[] = $searchHit; -} - -return $this->render('list/articles_to_translate.html.twig', [ - 'articles' => $articles, -]); +[[= include_file('code_samples/search/language/src/Controller/ArticlesToTranslateController.php', 24, 41) =]] +``` + +You can use the `LanguageCode` Criterion to search in +several languages while ensuring results have a translation in one specific language: + +``` php hl_lines="3 6" +[[= include_file('code_samples/search/language/src/Command/SearchTestCommand.php', 35, 47) =]] ```