Skip to content

Commit 70276ed

Browse files
dabrtbarw4
andauthored
IBX-10885: Document Content Type search API (#2946)
* IBX-10885: Document Content Type search API --------- Co-authored-by: dabrt <dabrt@users.noreply.github.com> Co-authored-by: Bartek Wajda <bartlomiej.wajda@ibexa.co>
1 parent 951ba55 commit 70276ed

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 FindContentTypeCommand 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 from the "Content" group that contains a specific field definition (in this case, a "Body" field).
28+
$query = new ContentTypeQuery(
29+
new Criterion\LogicalAnd([
30+
new Criterion\ContentTypeGroupId([1]),
31+
new Criterion\ContainsFieldDefinitionId([121]),
32+
]),
33+
[
34+
new SortClause\Id(),
35+
new SortClause\Identifier(),
36+
new SortClause\Name(),
37+
]
38+
);
39+
40+
$searchResult = $this->contentTypeService->findContentTypes($query);
41+
42+
$output->writeln('Found ' . $searchResult->getTotalCount() . ' content type(s):');
43+
44+
foreach ($searchResult->getContentTypes() as $contentType) {
45+
$output->writeln(sprintf(
46+
'- [%d] %s (identifier: %s)',
47+
$contentType->id,
48+
$contentType->getName(),
49+
$contentType->identifier
50+
));
51+
}
52+
53+
return Command::SUCCESS;
54+
}
55+
}

docs/content_management/content_api/managing_content.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,35 @@ To change the identifier of the copy, use a [`ContentTypeUpdateStruct`](/api/php
165165
[[= include_file('code_samples/api/public_php_api/src/Command/CreateContentTypeCommand.php', 87, 93) =]]
166166
```
167167

168+
### Finding and filtering content types
169+
170+
You can find content types that match specific criteria by using the [`ContentTypeService::findContentTypes()`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentTypeService.html#method_findContentTypes) method.
171+
This method accepts a `ContentTypeQuery` object that supports filtering and sorting by IDs, identifiers, group membership, and other criteria.
172+
173+
!!! note "Criterions and sort clauses"
174+
175+
For a full list of available criterions and sort clauses that you can use when finding and filtering content types, see [Content Type Search Criteria](content_type_criteria.md) and [Content Type Search Sort Clauses](content_type_sort_clauses.md) references.
176+
177+
178+
The following example shows how you can use the criteria to find content types:
179+
180+
```php hl_lines="28-38"
181+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php') =]]
182+
```
183+
184+
#### Query parameters
185+
186+
When constructing a `ContentTypeQuery`, you can pass the following parameters:
187+
188+
- `?CriterionInterface $criterion = null` — a filter to apply (use one or a combination of the criterions above)
189+
190+
- `array $sortClauses = []` — list of sort clauses to order the results
191+
192+
- `int $offset = 0` — starting offset (for pagination)
193+
194+
- `int $limit = 25` — maximum number of results to return
195+
196+
168197
## Calendar events
169198

170199
You can handle the calendar using `CalendarServiceInterface` (`Ibexa\Contracts\Calendar\CalendarServiceInterface`).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
description: Content Type Search Criteria help define and fine-tune search queries for content types.
3+
page_type: reference
4+
month_change: true
5+
---
6+
7+
# Content Type Search Criteria reference
8+
9+
Content Type Search Criteria are only supported by [Content Type Search (`ContentTypeService::findContentTypes`)](managing_content.md#finding-and-filtering-content-types).
10+
11+
| Criterion | Description |
12+
|-------|-------------|
13+
| [ContainsFieldDefinitionId](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-ContainsFieldDefinitionId.html) | Matches content types that contain a field definition with the specified ID. |
14+
| [ContentTypeGroupId](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-ContentTypeGroupId.html) | Matches content types by their assigned group ID. |
15+
| [ContentTypeId](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-ContentTypeId.html) | Matches content types by their ID. |
16+
| [ContentTypeIdentifier](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-ContentTypeIdentifier.html) | Matches content types by their identifier. |
17+
| [IsSystem](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-IsSystem.html) | Matches content types based on whether the group they belong to is system or not. |
18+
| [LogicalAnd](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-LogicalAnd.html) | Implements a logical AND Criterion. It matches if ALL of the provided Criteria match. |
19+
| [LogicalOr](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-LogicalOr.html) | Implements a logical OR Criterion. It matches if at least one of the provided Criteria matches. |
20+
| [LogicalNot](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-Criterion-LogicalNot.html) | Implements a logical NOT Criterion. It matches if the provided Criterion doesn't match. |
21+
22+
The following example shows how to use them to search for content types:
23+
24+
```php hl_lines="29-31"
25+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php') =]]
26+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
description: Content Type Search Sort Clauses
3+
month_change: true
4+
---
5+
6+
# Content Type Search Sort Clauses
7+
8+
Content Type Search Sort Clauses are the sorting options for content types.
9+
They're only supported by [Content Type Search (`ContentTypeService::findContentTypes`)](managing_content.md#finding-and-filtering-content-types).
10+
11+
Sort Clauses are found in the [`Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause`](/api/php_api/php_api_reference/namespaces/ibexa-contracts-core-repository-values-contenttype-query-sortclause.html) namespace:
12+
13+
| Name | Description |
14+
| --- | --- |
15+
| [Id](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-SortClause-Id.html)| Sort by content type's id |
16+
| [Identifier](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-SortClause-Identifier.html)| Sort by content type's identifier |
17+
| [Name](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-ContentType-Query-SortClause-Name.html)| Sort by content type's name |
18+
19+
20+
The following example shows how to use them to sort the searched content types:
21+
22+
```php hl_lines="34-36"
23+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php') =]]
24+
```
25+
26+
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).

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ nav:
567567
- LogicalAnd Criterion: search/criteria_reference/logicaland_criterion.md
568568
- LogicalNot Criterion: search/criteria_reference/logicalnot_criterion.md
569569
- LogicalOr Criterion: search/criteria_reference/logicalor_criterion.md
570+
- Content Type Search Criteria: search/content_type_search_reference/content_type_criteria.md
570571
- Product Search Criteria:
571572
- Product Search Criteria: search/criteria_reference/product_search_criteria.md
572573
- AttributeName: search/criteria_reference/attributename_criterion.md
@@ -706,6 +707,7 @@ nav:
706707
- SectionName: search/sort_clause_reference/sectionname_sort_clause.md
707708
- UserLogin: search/sort_clause_reference/userlogin_sort_clause.md
708709
- Visibility: search/sort_clause_reference/visibility_sort_clause.md
710+
- Content Type Sort Clauses: search/content_type_search_reference/content_type_sort_clauses.md
709711
- Product Sort Clauses:
710712
- Product Sort Clauses: search/sort_clause_reference/product_sort_clauses.md
711713
- BasePrice: search/sort_clause_reference/baseprice_sort_clause.md

0 commit comments

Comments
 (0)