diff --git a/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php b/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php index 35d37c56b8..6bde1b2316 100644 --- a/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php +++ b/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php @@ -3,6 +3,8 @@ namespace App\Command; use Ibexa\Contracts\Core\Repository\ContentService; +use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator; +use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter\RelationListIteratorAdapter; use Ibexa\Contracts\Core\Repository\LocationService; use Ibexa\Contracts\Core\Repository\ObjectStateService; use Ibexa\Contracts\Core\Repository\PermissionResolver; @@ -104,9 +106,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int // Relations $versionInfo = $this->contentService->loadVersionInfo($contentInfo); - $relationCount = $this->contentService->countRelations($versionInfo); - $relationList = $this->contentService->loadRelationList($versionInfo, 0, $relationCount); - foreach ($relationList as $relationListItem) { + $relationListIterator = new BatchIterator( + new RelationListIteratorAdapter( + $this->contentService, + $versionInfo + ) + ); + foreach ($relationListIterator as $relationListItem) { $name = $relationListItem->hasRelation() ? $relationListItem->getRelation()->destinationContentInfo->name : '(Unauthorized)'; $output->writeln("Relation to content '$name'"); } diff --git a/code_samples/front/embed_content/src/Controller/RelationController.php b/code_samples/front/embed_content/src/Controller/RelationController.php index b0cd9aa5be..ce99dbbd25 100644 --- a/code_samples/front/embed_content/src/Controller/RelationController.php +++ b/code_samples/front/embed_content/src/Controller/RelationController.php @@ -3,6 +3,8 @@ namespace App\Controller; use Ibexa\Contracts\Core\Repository\ContentService; +use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator; +use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter\RelationListIteratorAdapter; use Ibexa\Contracts\Core\Repository\LocationService; use Ibexa\Core\MVC\Symfony\View\View; @@ -25,11 +27,16 @@ public function showContentAction(View $view, $locationId): View $location = $this->locationService->loadLocation($locationId); $contentInfo = $location->getContentInfo(); $versionInfo = $this->contentService->loadVersionInfo($contentInfo); - $relationList = $this->contentService->loadRelationList($versionInfo); + $relationListIterator = new BatchIterator( + new RelationListIteratorAdapter( + $this->contentService, + $versionInfo + ) + ); $items = []; - foreach ($relationList as $relationListItem) { + foreach ($relationListIterator as $relationListItem) { if ($relationListItem->hasRelation() && in_array($relationListItem->getRelation()->getDestinationContentInfo()->getContentType()->identifier, $acceptedContentTypes)) { $items[] = $this->contentService->loadContentByContentInfo($relationListItem->getRelation()->getDestinationContentInfo()); } diff --git a/docs/content_management/content_api/browsing_content.md b/docs/content_management/content_api/browsing_content.md index 03fcbcd953..6e6f3597e3 100644 --- a/docs/content_management/content_api/browsing_content.md +++ b/docs/content_management/content_api/browsing_content.md @@ -28,9 +28,9 @@ You can also use it to request other Content-related value objects from various ``` php hl_lines="10" // ... [[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 4, 5) =]] -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 20, 22) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 22, 24) =]] // ... -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 55, 57) =]][[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 63, 72) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 57, 59) =]][[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 65, 73) =]] ``` @@ -46,7 +46,7 @@ It provides you with basic content metadata such as modification and publication To get the locations of a content item you need to make use of the [`LocationService`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-LocationService.html): ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 73, 77) =]] } +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 75, 79) =]] } ``` [`LocationService::loadLocations`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-LocationService.html#method_loadLocations) uses `ContentInfo` to get all the locations of a content item. @@ -60,7 +60,7 @@ The [`URLAliasService`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-C [`URLAliasService::reverseLookup`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-URLAliasService.html#method_reverseLookup) gets the location's main [URL alias](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-URLAlias.html): ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 73, 76) =]][[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 77, 80) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 75, 78) =]][[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 79, 82) =]] ``` ### Content type @@ -68,7 +68,7 @@ The [`URLAliasService`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-C You can retrieve the content type of a content item through the [`getContentType`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-ContentInfo.html#method_getContentType) method of the ContentInfo object: ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 82, 84) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 84, 86) =]] ``` ### Versions @@ -76,13 +76,13 @@ You can retrieve the content type of a content item through the [`getContentType To iterate over the versions of a content item, use the [`ContentService::loadVersions`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentService.html#method_loadVersions) method, which returns an array of `VersionInfo` value objects. ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 86, 92) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 88, 94) =]] ``` You can additionally provide the `loadVersions` method with the version status to get only versions of a specific status, for example: ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 93, 94) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 95, 96) =]] ``` !!! note @@ -93,12 +93,16 @@ You can additionally provide the `loadVersions` method with the version status t ### Relations Content Relations are versioned. -To list Relations to and from your content, you need to pass a `VersionInfo` object to the [`ContentService::loadRelationList`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentService.html#method_loadRelationList) method. -This method loads only the specified subset of relations to improve performance and was created with pagination in mind. +To list Relations to and from your content, you can: + +- pass a `VersionInfo` object to the [`ContentService::loadRelationList` method](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentService.html#method_loadRelationList) which returns a slice of the relation list thanks to pagination arguments +- use the [`RelationListIteratorAdapter`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Iterator-BatchIteratorAdapter-RelationListIteratorAdapter.html) + within a [`BatchIterator`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Iterator-BatchIterator.html) which allow traversing the relation list using one same object + You can get the current version's `VersionInfo` using [`ContentService::loadVersionInfo`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentService.html#method_loadVersionInfo). ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 105, 112) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 107, 118) =]] ``` You can also specify the version number as the second argument to get Relations for a specific version: @@ -117,7 +121,7 @@ It also holds the [relation type](content_relations.md), and the optional field You can use the `getOwner` method of the `ContentInfo` object to load the content item's owner as a `User` value object. ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 114, 115) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 120, 121) =]] ``` To get the creator of the current version and not the content item's owner, you need to use the `creatorId` property from the current version's `VersionInfo` object. @@ -127,7 +131,7 @@ To get the creator of the current version and not the content item's owner, you You can find the section to which a content item belongs through the [`getSection`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-ContentInfo.html#method_getSection) method of the ContentInfo object: ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 117, 118) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 123, 124) =]] ``` !!! note @@ -142,7 +146,7 @@ You need to provide it with the object state group. All object state groups can be retrieved through [`loadObjectStateGroups`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ObjectStateService.html#method_loadObjectStateGroups). ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 120, 125) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 126, 131) =]] ``` ## Viewing content with fields diff --git a/docs/templating/embed_and_list_content/embed_content.md b/docs/templating/embed_and_list_content/embed_content.md index f2fd959e5b..bb013a1d7d 100644 --- a/docs/templating/embed_and_list_content/embed_content.md +++ b/docs/templating/embed_and_list_content/embed_content.md @@ -36,13 +36,13 @@ You can use a custom controller for any situation where Query types aren't suffi This configuration points to a custom `RelationController` that should render all Articles with the `showContentAction()` method. -``` php hl_lines="23 27 28" +``` php hl_lines="25 29-35" [[= include_file('code_samples/front/embed_content/src/Controller/RelationController.php') =]] ``` -This controller uses the Public PHP API to get [the Relations of a content item](browsing_content.md#relations) (lines 27-28). +This controller uses the Public PHP API to get [the Relations of a content item](browsing_content.md#relations) (lines 29-35). -The controller takes the custom parameter called `accepted_content_types` (line 23), which is an array of content type identifiers that are rendered. +The controller takes the custom parameter called `accepted_content_types` (line 25), which is an array of content type identifiers that are rendered. This way you can control which content types you want to show or exclude.