From e68a80cf3ac37b8834e745083c0ee08ca11ac76e Mon Sep 17 00:00:00 2001 From: Adrien Dupuis <61695653+adriendupuis@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:17:49 +0100 Subject: [PATCH] =?UTF-8?q?ContentService::loadRelations=20=E2=86=92=20Con?= =?UTF-8?q?tentService::loadRelationList=20(#2544)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit loadRelations is deprecated in 4.5, and removed in 5.0 --------- Co-authored-by: Marek NocoĊ„ --- .../src/Command/ViewContentMetaDataCommand.php | 11 ++++++----- .../src/Controller/RelationController.php | 8 ++++---- .../content_api/browsing_content.md | 13 +++++++------ tools/code_samples/code_samples_usage_diff2html.php | 2 +- 4 files changed, 18 insertions(+), 16 deletions(-) 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 2d3cdf92d5..6037ae2515 100644 --- a/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php +++ b/code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $user = $this->userService->loadUserByLogin('admin'); $this->permissionResolver->setCurrentUserReference($user); - $contentId = $input->getArgument('contentId'); + $contentId = (int) $input->getArgument('contentId'); // Metadata $contentInfo = $this->contentService->loadContentInfo($contentId); @@ -99,10 +99,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int // Relations $versionInfo = $this->contentService->loadVersionInfo($contentInfo); - $relations = $this->contentService->loadRelations($versionInfo); - foreach ($relations as $relation) { - $name = $relation->destinationContentInfo->name; - $output->writeln('Relation to content ' . $name); + $relationCount = $this->contentService->countRelations($versionInfo); + $relationList = $this->contentService->loadRelationList($versionInfo, 0, $relationCount); + foreach ($relationList as $relationListItem) { + $name = $relationListItem->hasRelation() ? $relationListItem->getRelation()->destinationContentInfo->name : '(Unauthorized)'; + $output->writeln("Relation to content '$name'"); } // Owner diff --git a/code_samples/front/embed_content/src/Controller/RelationController.php b/code_samples/front/embed_content/src/Controller/RelationController.php index 129a0369a3..b0cd9aa5be 100644 --- a/code_samples/front/embed_content/src/Controller/RelationController.php +++ b/code_samples/front/embed_content/src/Controller/RelationController.php @@ -25,13 +25,13 @@ public function showContentAction(View $view, $locationId): View $location = $this->locationService->loadLocation($locationId); $contentInfo = $location->getContentInfo(); $versionInfo = $this->contentService->loadVersionInfo($contentInfo); - $relations = $this->contentService->loadRelations($versionInfo); + $relationList = $this->contentService->loadRelationList($versionInfo); $items = []; - foreach ($relations as $relation) { - if (in_array($relation->getDestinationContentInfo()->getContentType()->identifier, $acceptedContentTypes)) { - $items[] = $this->contentService->loadContentByContentInfo($relation->getDestinationContentInfo()); + foreach ($relationList 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 5fa8eecf96..8ffeb40861 100644 --- a/docs/content_management/content_api/browsing_content.md +++ b/docs/content_management/content_api/browsing_content.md @@ -103,11 +103,11 @@ to get only versions of a specific status, e.g.: Content Relations are versioned. To list Relations to and from your content, -you need to pass a `VersionInfo` object to the [`ContentService::loadRelations`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-ContentService.html#method_loadRelations) method. +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. 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', 100, 106) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 100, 107) =]] ``` You can also specify the version number as the second argument to get Relations for a specific version: @@ -116,7 +116,8 @@ You can also specify the version number as the second argument to get Relations $versionInfo = $this->contentService->loadVersionInfo($contentInfo, 2); ``` -`loadRelations` provides an array of [`Relation`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-Relation.html) objects. +`loadRelationList` provides an iterable [`RelationList`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-RelationList.html) object +listing [`Relation`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-Values-Content-Relation.html) objects. `Relation` has two main properties: `destinationContentInfo`, and `sourceContentInfo`. It also holds the [relation type](content_relations.md), and the optional Field this relation is made with. @@ -126,7 +127,7 @@ and the optional Field this relation is made with. 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', 108, 109) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 109, 110) =]] ``` To get the creator of the current version and not the content item's owner, @@ -139,7 +140,7 @@ the [`getSection`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-C of the ContentInfo object: ``` php -[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 111, 112) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 112, 113) =]] ``` !!! note @@ -155,7 +156,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', 114, 119) =]] +[[= include_file('code_samples/api/public_php_api/src/Command/ViewContentMetaDataCommand.php', 115, 120) =]] ``` ## Viewing content with Fields diff --git a/tools/code_samples/code_samples_usage_diff2html.php b/tools/code_samples/code_samples_usage_diff2html.php index 965cd3f6ae..f15f6fe28c 100644 --- a/tools/code_samples/code_samples_usage_diff2html.php +++ b/tools/code_samples/code_samples_usage_diff2html.php @@ -33,7 +33,7 @@ continue; } $statusChar = strlen($diffLine) ? $diffLine[0] : ''; - $realLine = $str = substr($diffLine, 1); + $realLine = str_replace(['<', '>'], ['<', '>'], substr($diffLine, 1)); if ($previousStatusChar !== $statusChar) { switch ("$previousStatusChar$statusChar") { case ' +':