Skip to content

Commit 11968ce

Browse files
committed
Move code to external file
1 parent 02adaec commit 11968ce

File tree

3 files changed

+63
-50
lines changed

3 files changed

+63
-50
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Command;
4+
5+
use Ibexa\Contracts\Core\Repository\ContentTypeService;
6+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
7+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion;
8+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
#[AsCommand(
15+
name: 'doc:find_content_types',
16+
description: 'Lists content types that match specific criteria.'
17+
)]
18+
class FindContentTypesCommand extends Command
19+
{
20+
public function __construct(private readonly ContentTypeService $contentTypeService)
21+
{
22+
parent::__construct();
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output): int
26+
{
27+
// Find content types whose identifier is "folder" or "article",
28+
// or content type "user"
29+
$query = new ContentTypeQuery(
30+
new Criterion\LogicalOr([
31+
new Criterion\LogicalAnd([
32+
new Criterion\ContentTypeIdentifier(['folder', 'article']),
33+
]),
34+
new Criterion\ContentTypeIdentifier(['user']),
35+
]),
36+
[
37+
new SortClause\Id(),
38+
new SortClause\Identifiers(),
39+
new SortClause\Name(),
40+
]
41+
);
42+
43+
$searchResult = $this->contentTypeService->findContentTypes($query);
44+
45+
$output->writeln('Found ' . $searchResult->totalCount . ' content types:');
46+
47+
foreach ($searchResult->searchHits as $searchHit) {
48+
$contentType = $searchHit->valueObject;
49+
$output->writeln(sprintf(
50+
'- [%d] %s (identifier: %s)',
51+
$contentType->id,
52+
$contentType->getName(),
53+
$contentType->identifier
54+
));
55+
}
56+
57+
return Command::SUCCESS;
58+
}
59+
}

docs/content_management/content_api/managing_content.md

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -177,31 +177,8 @@ This method accepts a `ContentTypeQuery` object that supports filtering and sort
177177

178178
The following example shows how you can use the criteria to find content types:
179179

180-
```php hl_lines="9-15"
181-
<?php
182-
183-
declare(strict_types=1);
184-
185-
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion;
186-
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
187-
188-
// Example: find content types whose identifier is "folder" or "article" *and* are in group 1, or identifier is "user":
189-
$query = new ContentTypeQuery(
190-
new Criterion\LogicalOr([
191-
new Criterion\LogicalAnd([
192-
new Criterion\ContentTypeIdentifier(['folder','article']),
193-
new Criterion\ContentTypeGroupIds([1]),
194-
]),
195-
new Criterion\ContentTypeIdentifier(['user']),
196-
]),
197-
[
198-
new SortClause\Id(),
199-
new SortClause\Identifier(),
200-
new SortClause\Name(),
201-
]
202-
);
203-
204-
$results = $contentTypeService->findContentTypes($query);
180+
```php hl_lines="12-17"
181+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php', 17, 43) =]]
205182
```
206183

207184
#### Query parameters

docs/search/content_type_search_reference/content_type_sort_clauses.md

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,8 @@ Sort Clauses are found in the [`Ibexa\Contracts\Core\Repository\Values\ContentTy
1919

2020
The following example shows how to use them to sort the searched content items:
2121

22-
```php hl_lines="18-20"
23-
<?php
24-
25-
declare(strict_types=1);
26-
27-
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion;
28-
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery;
29-
30-
// Example: find content types whose identifier is "folder" or "article" *and* are in group 1, or identifier is "user":
31-
$query = new ContentTypeQuery(
32-
new Criterion\LogicalOr([
33-
new Criterion\LogicalAnd([
34-
new Criterion\ContentTypeIdentifier(['folder','article']),
35-
new Criterion\ContentTypeGroupIds([1]),
36-
]),
37-
new Criterion\ContentTypeIdentifier(['user']),
38-
]),
39-
[
40-
new SortClause\Id(),
41-
new SortClause\Identifier(),
42-
new SortClause\Name(),
43-
]
44-
);
45-
46-
$results = $contentTypeService->findContentTypes($query);
22+
```php hl_lines="37-39"
23+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php') =]]
4724
```
4825

4926
You can change the default sorting order by using the `SORT_ASC` and `SORT_DESC` constants from [`AbstractSortClause`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-CoreSearch-Values-Query-AbstractSortClause.html#constants).

0 commit comments

Comments
 (0)