Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Criterion\LanguageCode example #2550

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions code_samples/search/language/config/append_to_routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
list_articles_to_translate:
path: /list/articles_to_translate/{language}
defaults:
_controller: 'App\Controller\ArticlesToTranslateController::listView'
50 changes: 50 additions & 0 deletions code_samples/search/language/src/Command/SearchTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace App\Command;

use Ibexa\Contracts\Core\Repository\SearchService;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SearchTestCommand extends Command
{
protected static $defaultName = 'doc:test:search';

private SearchService $searchService;

public function __construct(
SearchService $searchService
) {
parent::__construct(self::$defaultName);
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace App\Controller;

use Ibexa\Contracts\Core\Repository\SearchService;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ArticlesToTranslateController extends AbstractController
{
private SearchService $searchService;

public function __construct(SearchService $searchService)
{
$this->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,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends '@ibexadesign/pagelayout.html.twig' %}

{% block content %}
<ul>
{% for article in articles %}
<li><a href="{{ ibexa_path(article.contentInfo) }}">{{ ibexa_content_name(article) }}</a></li>
{% endfor %}
</ul>
{% endblock %}
28 changes: 9 additions & 19 deletions docs/search/criteria_reference/languagecode_criterion.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,18 @@
}
```

## Use case
## Use cases

Check warning on line 42 in docs/search/criteria_reference/languagecode_criterion.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/search/criteria_reference/languagecode_criterion.md#L42

[Ibexa.HeadingContent] Rename the heading '## Use cases', or re-purpose the content elsewhere.
Raw output
{"message": "[Ibexa.HeadingContent] Rename the heading '## Use cases', or re-purpose the content elsewhere.", "location": {"path": "docs/search/criteria_reference/languagecode_criterion.md", "range": {"start": {"line": 42, "column": 1}}}, "severity": "WARNING"}

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) =]]
```
Loading