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 all commits
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
118 changes: 118 additions & 0 deletions docs/reference/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
...........

Expand Down
46 changes: 46 additions & 0 deletions docs/reference/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,52 @@ filtering by ContentType.
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

``loadReverseFieldRelations()``
...............................

Get all reverse relation :ref:`Content items<content_object>` 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<content_object>` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Example** | .. code-block:: php |
| | |
| | $contentItems = $relationService->loadReverseFieldRelations( |
| | $content, |
| | 'relations', |
| | ['articles'] |
| | ); |
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

``loadReverseFieldRelationLocations()``
.......................................

Get all reverse relation :ref:`Locations<location_object>` 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<location_object>` |
+----------------------------------------+------------------------------------------------------------------------------------+
| **Example** | .. code-block:: php |
| | |
| | $contentItems = $relationService->loadReverseFieldRelationLocations( |
| | $content, |
| | 'relations', |
| | ['articles'] |
| | ); |
| | |
+----------------------------------------+------------------------------------------------------------------------------------+

Settings
--------

Expand Down
48 changes: 48 additions & 0 deletions docs/reference/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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) %}

<ul>
{% for article in reverse_related_articles %}
<li>
<a href="{{ ibexa_path(article) }}">{{ article.name }}</a>
</li>
{% endfor %}
</ul>

- **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) %}

<ul>
{% for article in articles %}
<li>
<a href="{{ ibexa_path(article) }}">{{ article.name }}</a>
</li>
{% endfor %}
</ul>

{{ pagerfanta( events, 'twitter_bootstrap' ) }}

Location children
~~~~~~~~~~~~~~~~~

Expand Down
Loading
Loading