Skip to content

Commit f5dbe43

Browse files
dabrtbarw4
andcommitted
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 2af19e8 commit f5dbe43

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class FindContentTypeCommand extends Command
14+
{
15+
protected static $defaultName = 'doc:find_content_types';
16+
17+
protected static $defaultDescription = 'Lists content types that match specific criteria.';
18+
19+
private ContentTypeService $contentTypeService;
20+
21+
public function __construct(ContentTypeService $contentTypeService)
22+
{
23+
$this->contentTypeService = $contentTypeService;
24+
parent::__construct();
25+
}
26+
27+
protected function execute(InputInterface $input, OutputInterface $output): int
28+
{
29+
// Find content types from the "Content" group that contains a specific field definition (in this case, a "Body" field).
30+
$query = new ContentTypeQuery(
31+
new Criterion\LogicalAnd([
32+
new Criterion\ContentTypeGroupId([1]),
33+
new Criterion\ContainsFieldDefinitionId([121]),
34+
]),
35+
[
36+
new SortClause\Id(),
37+
new SortClause\Identifier(),
38+
new SortClause\Name(),
39+
]
40+
);
41+
42+
$searchResult = $this->contentTypeService->findContentTypes($query);
43+
44+
$output->writeln('Found ' . $searchResult->getTotalCount() . ' content type(s):');
45+
46+
foreach ($searchResult->getContentTypes() as $contentType) {
47+
$output->writeln(sprintf(
48+
'- [%d] %s (identifier: %s)',
49+
$contentType->id,
50+
$contentType->getName(),
51+
$contentType->identifier
52+
));
53+
}
54+
55+
return Command::SUCCESS;
56+
}
57+
}

docs/content_management/content_api/managing_content.md

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

166+
### Finding and filtering content types
167+
168+
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.
169+
This method accepts a `ContentTypeQuery` object that supports filtering and sorting by IDs, identifiers, group membership, and other criteria.
170+
171+
!!! note "Criterions and sort clauses"
172+
173+
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.
174+
175+
176+
The following example shows how you can use the criteria to find content types:
177+
178+
```php hl_lines="30-40"
179+
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentTypeCommand.php') =]]
180+
```
181+
182+
#### Query parameters
183+
184+
When constructing a `ContentTypeQuery`, you can pass the following parameters:
185+
186+
- `?CriterionInterface $criterion = null` — a filter to apply (use one or a combination of the criterions above)
187+
188+
- `array $sortClauses = []` — list of sort clauses to order the results
189+
190+
- `int $offset = 0` — starting offset (for pagination)
191+
192+
- `int $limit = 25` — maximum number of results to return
193+
194+
166195
## Calendar events
167196

168197
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="31-33"
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="36-38"
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
@@ -570,6 +570,7 @@ nav:
570570
- LogicalAnd Criterion: search/criteria_reference/logicaland_criterion.md
571571
- LogicalNot Criterion: search/criteria_reference/logicalnot_criterion.md
572572
- LogicalOr Criterion: search/criteria_reference/logicalor_criterion.md
573+
- Content Type Search Criteria: search/content_type_search_reference/content_type_criteria.md
573574
- Product Search Criteria:
574575
- Product Search Criteria: search/criteria_reference/product_search_criteria.md
575576
- AttributeName: search/criteria_reference/attributename_criterion.md
@@ -709,6 +710,7 @@ nav:
709710
- SectionName: search/sort_clause_reference/sectionname_sort_clause.md
710711
- UserLogin: search/sort_clause_reference/userlogin_sort_clause.md
711712
- Visibility: search/sort_clause_reference/visibility_sort_clause.md
713+
- Content Type Sort Clauses: search/content_type_search_reference/content_type_sort_clauses.md
712714
- Product Sort Clauses:
713715
- Product Sort Clauses: search/sort_clause_reference/product_sort_clauses.md
714716
- BasePrice: search/sort_clause_reference/baseprice_sort_clause.md

0 commit comments

Comments
 (0)