From 333ff172d53ffc3591ca85e149c07d765c852648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Thu, 12 Dec 2024 13:32:56 +0100 Subject: [PATCH 1/3] Add methods to access reverse relations on content --- lib/API/RelationService.php | 28 ++++++++ lib/API/Values/Content.php | 88 ++++++++++++++++++++++++ lib/Core/Site/RelationService.php | 58 ++++++++++++++++ lib/Core/Site/Values/Content.php | 108 ++++++++++++++++++++++++++++++ 4 files changed, 282 insertions(+) diff --git a/lib/API/RelationService.php b/lib/API/RelationService.php index 3a369511..166310cd 100644 --- a/lib/API/RelationService.php +++ b/lib/API/RelationService.php @@ -61,4 +61,32 @@ public function loadFieldRelationLocations( array $contentTypeIdentifiers = [], ?int $limit = null, ): array; + + /** + * Load all reverse related Content from $fieldDefinitionIdentifier field to the given + * $content, optionally limited by a list of $contentTypeIdentifiers and $limit. + * + * @return \Netgen\IbexaSiteApi\API\Values\Content[] + */ + public function loadReverseRelations( + Content $content, + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + ?int $limit = null, + ): array; + + /** + * Load all reverse related Locations from $fieldDefinitionIdentifier field to the given + * $content, optionally limited by a list of $contentTypeIdentifiers and $limit. + * + * Note: only visible main Locations of the reverse related Content will be used. + * + * @return \Netgen\IbexaSiteApi\API\Values\Location[] + */ + public function loadReverseRelationLocations( + Content $content, + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + ?int $limit = null, + ): array; } diff --git a/lib/API/Values/Content.php b/lib/API/Values/Content.php index 193d8f52..b588887c 100644 --- a/lib/API/Values/Content.php +++ b/lib/API/Values/Content.php @@ -196,6 +196,94 @@ abstract public function filterSudoFieldRelationLocations( int $currentPage = 1, ): Pagerfanta; + /** + * Return all reverse related Content from $fieldDefinitionIdentifier. + * + * @return \Netgen\IbexaSiteApi\API\Values\Content[] + */ + abstract public function getReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; + + /** + * Return all reverse related Content from $fieldDefinitionIdentifier using repository sudo. + * + * @return \Netgen\IbexaSiteApi\API\Values\Content[] + */ + abstract public function getSudoReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; + + /** + * Return reverse related Content from $fieldDefinitionIdentifier field, + * optionally limited by a list of $contentTypeIdentifiers. + * + * @param string[] $contentTypeIdentifiers + * + * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Content items + */ + abstract public function filterReverseRelations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta; + + /** + * Return reverse related Content from $fieldDefinitionIdentifier field using repository sudo, + * optionally limited by a list of $contentTypeIdentifiers. + * + * @param string[] $contentTypeIdentifiers + * + * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Content items + */ + abstract public function filterSudoReverseRelations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta; + + /** + * Return all reverse related Locations from $fieldDefinitionIdentifier. + * + * @return \Netgen\IbexaSiteApi\API\Values\Location[] + */ + abstract public function getReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; + + /** + * Return all reverse related Locations from $fieldDefinitionIdentifier using repository sudo. + * + * @return \Netgen\IbexaSiteApi\API\Values\Location[] + */ + abstract public function getSudoReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; + + /** + * Return reverse related Locations from $fieldDefinitionIdentifier field, + * optionally limited by a list of $contentTypeIdentifiers. + * + * @param string[] $contentTypeIdentifiers + * + * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Locations + */ + abstract public function filterReverseRelationLocations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta; + + /** + * Return reverse related Locations from $fieldDefinitionIdentifier field using repository sudo, + * optionally limited by a list of $contentTypeIdentifiers. + * + * @param string[] $contentTypeIdentifiers + * + * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Locations + */ + abstract public function filterSudoReverseRelationLocations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta; + /** * Return absolute path for the Content. * diff --git a/lib/Core/Site/RelationService.php b/lib/Core/Site/RelationService.php index 006fed5d..cdab0a7a 100644 --- a/lib/Core/Site/RelationService.php +++ b/lib/Core/Site/RelationService.php @@ -5,6 +5,9 @@ namespace Netgen\IbexaSiteApi\Core\Site; use Exception; +use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; +use Ibexa\Contracts\Core\Repository\Values\Content\Query; +use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Netgen\IbexaSiteApi\API\RelationService as RelationServiceInterface; use Netgen\IbexaSiteApi\API\Site as SiteInterface; use Netgen\IbexaSiteApi\API\Values\Content; @@ -139,6 +142,61 @@ public function loadFieldRelationLocations( return $locations; } + public function loadReverseRelations( + Content $content, + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + ?int $limit = null, + ): array { + $query = new Query(); + + $criteria = [ + new Criterion\FieldRelation($fieldDefinitionIdentifier, Criterion\Operator::CONTAINS, [$content->id]) + ]; + + if (count($contentTypeIdentifiers) > 0) { + $criteria[] = new Criterion\ContentTypeIdentifier($contentTypeIdentifiers); + } + + $query->filter = new Criterion\LogicalAnd($criteria); + + if ($limit !== null && $limit > 0) { + $query->limit = $limit; + } + + $result = $this->site->getFindService()->findContent($query); + + return $this->extractContentItems($result); + } + + public function loadReverseRelationLocations( + Content $content, + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + ?int $limit = null, + ): array { + $query = new LocationQuery(); + + $criteria = [ + new Criterion\FieldRelation($fieldDefinitionIdentifier, Criterion\Operator::CONTAINS, [$content->id]), + new Criterion\Location\IsMainLocation(Criterion\Location\IsMainLocation::MAIN), + ]; + + if (count($contentTypeIdentifiers) > 0) { + $criteria[] = new Criterion\ContentTypeIdentifier($contentTypeIdentifiers); + } + + $query->filter = new Criterion\LogicalAnd($criteria); + + if ($limit !== null && $limit > 0) { + $query->limit = $limit; + } + + $result = $this->site->getFindService()->findLocations($query); + + return $this->extractLocations($result); + } + /** * Return an array of related Content items. * diff --git a/lib/Core/Site/Values/Content.php b/lib/Core/Site/Values/Content.php index 1fd3ba85..ab39085c 100644 --- a/lib/Core/Site/Values/Content.php +++ b/lib/Core/Site/Values/Content.php @@ -351,6 +351,114 @@ public function filterSudoFieldRelationLocations( ); } + public function getReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array + { + return $this->site->getRelationService()->loadReverseRelations( + $this, + $fieldDefinitionIdentifier, + [], + $limit, + ); + } + + public function getSudoReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array + { + return $this->repository->sudo( + fn () => $this->getReverseRelations($fieldDefinitionIdentifier, $limit), + ); + } + + public function filterReverseRelations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta { + $relations = $this->site->getRelationService()->loadReverseRelations( + $this, + $fieldDefinitionIdentifier, + $contentTypeIdentifiers, + ); + + $pager = new Pagerfanta(new ArrayAdapter($relations)); + + $pager->setNormalizeOutOfRangePages(true); + $pager->setMaxPerPage($maxPerPage); + $pager->setCurrentPage($currentPage); + + return $pager; + } + + public function filterSudoReverseRelations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta { + return $this->repository->sudo( + fn () => $this->filterReverseRelations( + $fieldDefinitionIdentifier, + $contentTypeIdentifiers, + $maxPerPage, + $currentPage + ), + ); + } + + public function getReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array + { + return $this->site->getRelationService()->loadReverseRelationLocations( + $this, + $fieldDefinitionIdentifier, + [], + $limit, + ); + } + + public function getSudoReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array + { + return $this->repository->sudo( + fn () => $this->getReverseRelationLocations($fieldDefinitionIdentifier, $limit), + ); + } + + public function filterReverseRelationLocations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta { + $relations = $this->site->getRelationService()->loadReverseRelationLocations( + $this, + $fieldDefinitionIdentifier, + $contentTypeIdentifiers, + ); + + $pager = new Pagerfanta(new ArrayAdapter($relations)); + + $pager->setNormalizeOutOfRangePages(true); + $pager->setMaxPerPage($maxPerPage); + $pager->setCurrentPage($currentPage); + + return $pager; + } + + public function filterSudoReverseRelationLocations( + string $fieldDefinitionIdentifier, + array $contentTypeIdentifiers = [], + int $maxPerPage = 25, + int $currentPage = 1, + ): Pagerfanta { + return $this->repository->sudo( + fn () => $this->filterReverseRelationLocations( + $fieldDefinitionIdentifier, + $contentTypeIdentifiers, + $maxPerPage, + $currentPage + ), + ); + } + public function getPath(array $parameters = []): string { return $this->internalGetPath()->getAbsolute($parameters); From 1fa482ebfe4e8300e41cc8b9ab07b57105a30e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Fri, 13 Dec 2024 15:49:26 +0100 Subject: [PATCH 2/3] Rename methods --- lib/API/RelationService.php | 4 ++-- lib/API/Values/Content.php | 16 ++++++++-------- lib/Core/Site/RelationService.php | 4 ++-- lib/Core/Site/Values/Content.php | 32 +++++++++++++++---------------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/API/RelationService.php b/lib/API/RelationService.php index 166310cd..0be37914 100644 --- a/lib/API/RelationService.php +++ b/lib/API/RelationService.php @@ -68,7 +68,7 @@ public function loadFieldRelationLocations( * * @return \Netgen\IbexaSiteApi\API\Values\Content[] */ - public function loadReverseRelations( + public function loadReverseFieldRelations( Content $content, string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], @@ -83,7 +83,7 @@ public function loadReverseRelations( * * @return \Netgen\IbexaSiteApi\API\Values\Location[] */ - public function loadReverseRelationLocations( + public function loadReverseFieldRelationLocations( Content $content, string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], diff --git a/lib/API/Values/Content.php b/lib/API/Values/Content.php index b588887c..b0209c48 100644 --- a/lib/API/Values/Content.php +++ b/lib/API/Values/Content.php @@ -201,14 +201,14 @@ abstract public function filterSudoFieldRelationLocations( * * @return \Netgen\IbexaSiteApi\API\Values\Content[] */ - abstract public function getReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; + abstract public function getReverseFieldRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; /** * Return all reverse related Content from $fieldDefinitionIdentifier using repository sudo. * * @return \Netgen\IbexaSiteApi\API\Values\Content[] */ - abstract public function getSudoReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; + abstract public function getSudoReverseFieldRelations(string $fieldDefinitionIdentifier, int $limit = 25): array; /** * Return reverse related Content from $fieldDefinitionIdentifier field, @@ -218,7 +218,7 @@ abstract public function getSudoReverseRelations(string $fieldDefinitionIdentifi * * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Content items */ - abstract public function filterReverseRelations( + abstract public function filterReverseFieldRelations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, @@ -233,7 +233,7 @@ abstract public function filterReverseRelations( * * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Content items */ - abstract public function filterSudoReverseRelations( + abstract public function filterSudoReverseFieldRelations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, @@ -245,14 +245,14 @@ abstract public function filterSudoReverseRelations( * * @return \Netgen\IbexaSiteApi\API\Values\Location[] */ - abstract public function getReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; + abstract public function getReverseFieldRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; /** * Return all reverse related Locations from $fieldDefinitionIdentifier using repository sudo. * * @return \Netgen\IbexaSiteApi\API\Values\Location[] */ - abstract public function getSudoReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; + abstract public function getSudoReverseFieldRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array; /** * Return reverse related Locations from $fieldDefinitionIdentifier field, @@ -262,7 +262,7 @@ abstract public function getSudoReverseRelationLocations(string $fieldDefinition * * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Locations */ - abstract public function filterReverseRelationLocations( + abstract public function filterReverseFieldRelationLocations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, @@ -277,7 +277,7 @@ abstract public function filterReverseRelationLocations( * * @return \Pagerfanta\Pagerfanta Pagerfanta instance iterating over Site API Locations */ - abstract public function filterSudoReverseRelationLocations( + abstract public function filterSudoReverseFieldRelationLocations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, diff --git a/lib/Core/Site/RelationService.php b/lib/Core/Site/RelationService.php index cdab0a7a..df56c9a5 100644 --- a/lib/Core/Site/RelationService.php +++ b/lib/Core/Site/RelationService.php @@ -142,7 +142,7 @@ public function loadFieldRelationLocations( return $locations; } - public function loadReverseRelations( + public function loadReverseFieldRelations( Content $content, string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], @@ -169,7 +169,7 @@ public function loadReverseRelations( return $this->extractContentItems($result); } - public function loadReverseRelationLocations( + public function loadReverseFieldRelationLocations( Content $content, string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], diff --git a/lib/Core/Site/Values/Content.php b/lib/Core/Site/Values/Content.php index ab39085c..d8a69350 100644 --- a/lib/Core/Site/Values/Content.php +++ b/lib/Core/Site/Values/Content.php @@ -351,9 +351,9 @@ public function filterSudoFieldRelationLocations( ); } - public function getReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array + public function getReverseFieldRelations(string $fieldDefinitionIdentifier, int $limit = 25): array { - return $this->site->getRelationService()->loadReverseRelations( + return $this->site->getRelationService()->loadReverseFieldRelations( $this, $fieldDefinitionIdentifier, [], @@ -361,20 +361,20 @@ public function getReverseRelations(string $fieldDefinitionIdentifier, int $limi ); } - public function getSudoReverseRelations(string $fieldDefinitionIdentifier, int $limit = 25): array + public function getSudoReverseFieldRelations(string $fieldDefinitionIdentifier, int $limit = 25): array { return $this->repository->sudo( - fn () => $this->getReverseRelations($fieldDefinitionIdentifier, $limit), + fn () => $this->getReverseFieldRelations($fieldDefinitionIdentifier, $limit), ); } - public function filterReverseRelations( + public function filterReverseFieldRelations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, int $currentPage = 1, ): Pagerfanta { - $relations = $this->site->getRelationService()->loadReverseRelations( + $relations = $this->site->getRelationService()->loadReverseFieldRelations( $this, $fieldDefinitionIdentifier, $contentTypeIdentifiers, @@ -389,14 +389,14 @@ public function filterReverseRelations( return $pager; } - public function filterSudoReverseRelations( + public function filterSudoReverseFieldRelations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, int $currentPage = 1, ): Pagerfanta { return $this->repository->sudo( - fn () => $this->filterReverseRelations( + fn () => $this->filterReverseFieldRelations( $fieldDefinitionIdentifier, $contentTypeIdentifiers, $maxPerPage, @@ -405,9 +405,9 @@ public function filterSudoReverseRelations( ); } - public function getReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array + public function getReverseFieldRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array { - return $this->site->getRelationService()->loadReverseRelationLocations( + return $this->site->getRelationService()->loadReverseFieldRelationLocations( $this, $fieldDefinitionIdentifier, [], @@ -415,20 +415,20 @@ public function getReverseRelationLocations(string $fieldDefinitionIdentifier, i ); } - public function getSudoReverseRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array + public function getSudoReverseFieldRelationLocations(string $fieldDefinitionIdentifier, int $limit = 25): array { return $this->repository->sudo( - fn () => $this->getReverseRelationLocations($fieldDefinitionIdentifier, $limit), + fn () => $this->getReverseFieldRelationLocations($fieldDefinitionIdentifier, $limit), ); } - public function filterReverseRelationLocations( + public function filterReverseFieldRelationLocations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, int $currentPage = 1, ): Pagerfanta { - $relations = $this->site->getRelationService()->loadReverseRelationLocations( + $relations = $this->site->getRelationService()->loadReverseFieldRelationLocations( $this, $fieldDefinitionIdentifier, $contentTypeIdentifiers, @@ -443,14 +443,14 @@ public function filterReverseRelationLocations( return $pager; } - public function filterSudoReverseRelationLocations( + public function filterSudoReverseFieldRelationLocations( string $fieldDefinitionIdentifier, array $contentTypeIdentifiers = [], int $maxPerPage = 25, int $currentPage = 1, ): Pagerfanta { return $this->repository->sudo( - fn () => $this->filterReverseRelationLocations( + fn () => $this->filterReverseFieldRelationLocations( $fieldDefinitionIdentifier, $contentTypeIdentifiers, $maxPerPage, From 6882800b162be7eef1cd59ee134710eea24d2cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Fri, 13 Dec 2024 16:03:43 +0100 Subject: [PATCH 3/3] Update docs --- docs/reference/objects.rst | 118 ++++++++++++++++++++++++++++++++++ docs/reference/services.rst | 46 +++++++++++++ docs/reference/templating.rst | 48 ++++++++++++++ 3 files changed, 212 insertions(+) diff --git a/docs/reference/objects.rst b/docs/reference/objects.rst index c36e0095..48d66387 100644 --- a/docs/reference/objects.rst +++ b/docs/reference/objects.rst @@ -434,6 +434,124 @@ Used to filter field relation Locations from the `Field`_ with the given ``$iden | | | +----------------------------------------+------------------------------------------------------------------------------------+ +``getReverseFieldRelations`` +............................ + +Used to get ``$limit`` reverse field relation Content items from the `Field`_ with the given ``$identifier``. Relations +will be sorted as is defined by the relation field. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``string $identifier`` | +| | 2. ``int $limit = 25`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | An array of reverse related `Content`_ items | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Sorting method** | Sorted as is defined by the relation `Field`_ | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in PHP** | .. code-block:: php | +| | | +| | $relations = $content->getReverseFieldRelations('images', 10); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in Twig** | .. code-block:: twig | +| | | +| | {% set relations = content.reverseFieldRelations('images') %} | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + +``filterReverseFieldRelations`` +............................... + +Used to filter reverse field relation Content items from the `Field`_ with the given ``$identifier``. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``string $identifier`` | +| | 2. ``array $contentTypeIdentifiers = []`` | +| | 3. ``int $maxPerPage = 25`` | +| | 4. ``int $currentPage = 1`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | Pagerfanta instance with reverse related `Content`_ items | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in PHP** | .. code-block:: php | +| | | +| | $relations = $content->filterReverseFieldRelations( | +| | 'related_items', | +| | ['images', 'videos'], | +| | 10, | +| | 2 | +| | ); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in Twig** | .. code-block:: twig | +| | | +| | {% set relations = content.filterReverseFieldRelations( | +| | 'related_items' | +| | ['images', 'videos'] | +| | 10, | +| | 2 | +| | ) %} | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + +``getReverseFieldRelationLocations`` +.................................... + +Used to get ``$limit`` reverse field relation Locations from the `Field`_ with the given ``$identifier``. Relations +will be sorted as is defined by the relation field. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``string $identifier`` | +| | 2. ``int $limit = 25`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | An array of related `Location`_ items | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Sorting method** | Sorted as is defined by the relation `Field`_ | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in PHP** | .. code-block:: php | +| | | +| | $relations = $content->getReverseFieldRelationLocations('images', 10); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in Twig** | .. code-block:: twig | +| | | +| | {% set relations = content.reverseFieldRelationLocations('images') %} | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + +``filterReverseFieldRelationLocations`` +....................................... + +Used to filter reverse field relation Locations from the `Field`_ with the given ``$identifier``. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``string $identifier`` | +| | 2. ``array $contentTypeIdentifiers = []`` | +| | 3. ``int $maxPerPage = 25`` | +| | 4. ``int $currentPage = 1`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | Pagerfanta instance with reverse related `Location`_ items | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in PHP** | .. code-block:: php | +| | | +| | $relations = $content->filterReverseFieldRelationLocations( | +| | 'related_items', | +| | ['images', 'videos'], | +| | 10, | +| | 2 | +| | ); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example in Twig** | .. code-block:: twig | +| | | +| | {% set relations = content.filterReverseFieldRelationLocations( | +| | 'related_items' | +| | ['images', 'videos'] | +| | 10, | +| | 2 | +| | ) %} | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + ``getPath`` ........... diff --git a/docs/reference/services.rst b/docs/reference/services.rst index fa247b0b..3f126ed8 100644 --- a/docs/reference/services.rst +++ b/docs/reference/services.rst @@ -344,6 +344,52 @@ filtering by ContentType. | | | +----------------------------------------+------------------------------------------------------------------------------------+ +``loadReverseFieldRelations()`` +............................... + +Get all reverse relation :ref:`Content items` from a specific field to a given Content. The method supports optional +filtering by ContentType. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``Netgen\IbexaSiteApi\API\Values\Content $content`` | +| | 2. ``string $fieldDefinitionIdentifier`` | +| | 3. ``array $contentTypeIdentifiers = []`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | An array of :ref:`Content items` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example** | .. code-block:: php | +| | | +| | $contentItems = $relationService->loadReverseFieldRelations( | +| | $content, | +| | 'relations', | +| | ['articles'] | +| | ); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + +``loadReverseFieldRelationLocations()`` +....................................... + +Get all reverse relation :ref:`Locations` from a specific field to a given Content. The method supports optional +filtering by ContentType. + ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Parameters** | 1. ``Netgen\IbexaSiteApi\API\Values\Content $content`` | +| | 2. ``string $fieldDefinitionIdentifier`` | +| | 3. ``array $contentTypeIdentifiers = []`` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Returns** | An array of :ref:`Locations` | ++----------------------------------------+------------------------------------------------------------------------------------+ +| **Example** | .. code-block:: php | +| | | +| | $contentItems = $relationService->loadReverseFieldRelationLocations( | +| | $content, | +| | 'relations', | +| | ['articles'] | +| | ); | +| | | ++----------------------------------------+------------------------------------------------------------------------------------+ + Settings -------- diff --git a/docs/reference/templating.rst b/docs/reference/templating.rst index eb005e6a..8ee2752f 100644 --- a/docs/reference/templating.rst +++ b/docs/reference/templating.rst @@ -491,6 +491,54 @@ Content Field relations {{ pagerfanta( events, 'twitter_bootstrap' ) }} +Content Field reverse relations +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- **Accessing all field relations** + + This is done by calling the method ``getReverseFieldRelations()``, also available as + ``reverseFieldRelations()`` in Twig. It returns an array of Content items and has two parameters: + + 1. **required** identifier of the reverse relation field + 2. **optional** maximum number of items returned, by default ``25`` + + .. code-block:: twig + + {% set reverse_related_articles = content.reverseFieldRelations('related_articles', 10) %} + +
    + {% for article in reverse_related_articles %} +
  • + {{ article.name }} +
  • + {% endfor %} +
+ +- **Filtering through reverse field relations** + + This is done by calling the method ``filterReverseFieldRelations()``, which returns a Pagerfanta + instance and has four parameters: + + 1. **required** identifier of the reverse relation field + 2. **optional** array of ContentType identifiers that will be used to filter the result, by + default an empty array ``[]`` + 3. **optional** maximum number of items per page, by default ``25`` + 4. **optional** current page, by default ``1`` + + .. code-block:: twig + + {% set articles = content.filterReverseFieldRelations('related_items', ['article'], 10, 1) %} + + + + {{ pagerfanta( events, 'twitter_bootstrap' ) }} + Location children ~~~~~~~~~~~~~~~~~