Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to access reverse relations on content #36

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/API/RelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
pspanja marked this conversation as resolved.
Show resolved Hide resolved
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;
}
88 changes: 88 additions & 0 deletions lib/API/Values/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
58 changes: 58 additions & 0 deletions lib/Core/Site/RelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
108 changes: 108 additions & 0 deletions lib/Core/Site/Values/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading