Skip to content

Commit 6a698b1

Browse files
committed
languagecode_criterion.md: Move code to PHP files
1 parent b880a23 commit 6a698b1

File tree

5 files changed

+109
-32
lines changed

5 files changed

+109
-32
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
list_articles_to_translate:
2+
path: /list/articles_to_translate/{language}
3+
defaults:
4+
_controller: 'App\Controller\ArticlesToTranslateController::listView'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use Ibexa\Contracts\Core\Repository\SearchService;
6+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
7+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class SearchTestCommand extends Command
14+
{
15+
protected static $defaultName = 'doc:test:search';
16+
private SearchService $searchService;
17+
18+
public function __construct(
19+
SearchService $searchService
20+
)
21+
{
22+
parent::__construct(self::$defaultName);
23+
$this->searchService = $searchService;
24+
}
25+
26+
protected function configure(): void
27+
{
28+
$this->setDescription('Search test.')
29+
->addArgument('text', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text.');
30+
}
31+
32+
protected function execute(InputInterface $input, OutputInterface $output)
33+
{
34+
$text = implode(' ', $input->getArgument('text'));
35+
36+
$query = new Query(['query' =>
37+
new Criterion\LogicalAnd([
38+
new Criterion\FullText($text),
39+
new Criterion\LanguageCode(['eng-GB'], false)
40+
])
41+
]);
42+
43+
$results = $this->searchService->findContent($query, ['eng-GB', 'fre-FR', 'ger-DE']);
44+
foreach ($results->searchHits as $searchHit) {
45+
/** @var $content \Ibexa\Core\Repository\Values\Content\Content */
46+
$content = $searchHit->valueObject;
47+
dump($content->getName('eng-GB'));
48+
}
49+
50+
return Command::SUCCESS;
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Ibexa\Contracts\Core\Repository\SearchService;
6+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
7+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
8+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class ArticlesToTranslateController extends AbstractController {
13+
private SearchService $searchService;
14+
15+
public function __construct(SearchService $searchService)
16+
{
17+
$this->searchService = $searchService;
18+
}
19+
20+
public function listView(Request $request): Response {
21+
$languageCode = $request->get('language');
22+
23+
$query = new Query();
24+
$query->query = new Criterion\LogicalAnd([
25+
new Criterion\ContentTypeIdentifier('article'),
26+
new Criterion\LogicalNot(
27+
new Criterion\LanguageCode($languageCode, false)
28+
)
29+
]);
30+
31+
$results = $this->searchService->findContent($query);
32+
$articles = [];
33+
foreach ($results->searchHits as $searchHit) {
34+
$articles[] = $searchHit->valueObject;
35+
}
36+
37+
return $this->render('@ibexadesign/list/articles_to_translate.html.twig', [
38+
'articles' => $articles,
39+
]);
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends '@ibexadesign/pagelayout.html.twig' %}
2+
3+
{% block content %}
4+
<ul>
5+
{% for article in articles %}
6+
<li><a href="{{ ibexa_path(article.contentInfo) }}">{{ ibexa_content_name(article) }}</a></li>
7+
{% endfor %}
8+
</ul>
9+
{% endblock %}

docs/search/criteria_reference/languagecode_criterion.md

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,12 @@ You can use the `LanguageCode` Criterion to search for articles that are lacking
4545
into a specific language:
4646

4747
``` php hl_lines="5"
48-
$query = new Query();
49-
$query->query = new Criterion\LogicalAnd([
50-
new Criterion\ContentTypeIdentifier('article'),
51-
new Criterion\LogicalNot(
52-
new Criterion\LanguageCode('ger-DE', false)
53-
)
54-
]);
55-
56-
$results = $this->searchService->findContent($query);
57-
$articles = [];
58-
foreach ($results->searchHits as $searchHit) {
59-
$articles[] = $searchHit;
60-
}
61-
62-
return $this->render('list/articles_to_translate.html.twig', [
63-
'articles' => $articles,
64-
]);
48+
[[= include_file('code_samples/search/language/src/Controller/ArticlesToTranslateController.php', 22, 39) =]]
6549
```
6650

6751
You can use the `LanguageCode` Criterion to search in
6852
several languages while ensuring result have a translation in one language:
6953

70-
```php
71-
$query = new Query(['query' =>
72-
new Criterion\LogicalAnd([
73-
new Criterion\FullText($text),
74-
new Criterion\LanguageCode(['eng-GB'], false)
75-
])
76-
]);
77-
78-
$results = $this->searchService->findContent($query, ['eng-GB', 'fre-FR', 'ger-DE']);
79-
80-
foreach ($results->searchHits as $searchHit) {
81-
/** @var $content \Ibexa\Core\Repository\Values\Content\Content */
82-
$content = $searchHit->valueObject;
83-
dump($content->getName('eng-GB'));
84-
}
54+
``` php
55+
[[= include_file('code_samples/search/language/src/Command/SearchTestCommand.php', 35, 48) =]]
8556
```

0 commit comments

Comments
 (0)