This plugin shows all relations of an element. For example, where an asset, entry or any other element is linked.
Feature Requests are welcome!
This plugin requires Craft CMS 4.0.0 or 5.0.0 later.
For Craft CMS 4: When using SEOmatic: MySQL 8.0.17+, MariaDB 10.4.6+, or PostgreSQL 13+ is required.
To install this plugin, follow these steps:
- Install with Composer via
composer require internetztube/craft-element-relations - Install plugin in the Craft Control Panel under Settings > Plugins
You can also install this plugin via the Plugin Store in the Craft Control Panel.
As a basis the relations table is used. This means that any field that stores relations in the relations table will work out of the box.
- Entries, Assets, Categories, Globals, Users, Products, ...
- Matrix
- Neo
- SuperTable
- User Photo
- SEOmatic
- Redactor
- CkEditor
- Hyper
- LinkIt
- TypedLinkField
- Formie
... and many more.
The Element Relations field exposes its data in GraphQL. Query the field by its handle to
access counts, usage flags, and related elements. Below is a minimal example fetching usage
information for a field named relationsField on an entry:
{
entry(slug: "my-entry") {
... on entry_default_Entry {
title
relationsField {
count
isInUse
elements(limit: 2) {
id
title
}
globalCount: count(siteIds: []),
globalIsInUse: isInUse(siteIds: []),
globalElements: elements(
siteIds: [],
limit: 2,
sections: ['sectionHandle'],
entryTypes: ['entryTypeHandle']
) {
id
title
}
}
}
}
}Obtain a RelationsModel instance from any element’s relations field, then use its methods to inspect usage.
{# Fetch the RelationsModel from a field named `relationsField` #}
{# @var relationsModel \internetztube\elementRelations\models\RelationsModel #}
{% set relationsModel = element.relationsField %}{# Check if the element is used in the current site #}
{% if relationsModel.isInUse %}
{# ... #}
{% endif %}
{# Check usage across all sites #}
{% if relationsModel.isInUse([]) %}
{# ... #}
{% endif %}
{# Check usage in specific sections/entry types #}
{% if relationsModel.isInUse(sections: ['sectionHandle'], entryTypes: ['entryTypeHandle']) %}
{# ... #}
{% endif %}{# All related elements in elements site #}
{% set elements = relationsModel.elements %}
{# All related elements across all sites #}
{% set allElements = relationsModel.elements(siteIds: []) %}
{# Filter by specific site id(s) #}
{% set site1Elements = relationsModel.elements(siteIds: [1]) %}
{# Limit number of results (e.g., first 2 items) #}
{% set firstTwo = relationsModel.elements(siteIds: [], limit: 2) %}
{# Filter by sections and entry types #}
{% set filteredElements = relationsModel.elements(sections: ['sectionHandle'], entryTypes: ['entryTypeHandle']) %}
{# Combine filters with limit #}
{% set limitedFiltered = relationsModel.elements(limit: 5, sections: ['news', 'blog']) %}
{# All options #}
{% set elements = relationsModel.elements(
siteIds: [],
limit: null,
offset: 0,
sections: [],
entryTypes: []
) %}{% for element in relationsModel.elementsIterator(siteIds: [], limit: null, offset: 0, batchSize: 100) %}
{{ dump(element) }}
{% endfor %}
{# Filter by sections/entry types while iterating #}
{% for element in relationsModel.elementsIterator(sections: ['news'], entryTypes: ['article'], limit: 50) %}
{{ element.title }}
{% endfor %}{# List of sites where the element is in use #}
{% set sites = relationsModel.sites %}{# Detect usage in SEOmatic global settings #}
{% if relationsModel.isUsedInSeomaticGlobalSettings %}
{# ... #}
{% endif %}Please report any issues you find to the Issues page.
Brought to you by Frederic Koeberl




