Skip to content

Instantly share code, notes, and snippets.

@croxton
Last active September 11, 2025 10:45
Show Gist options
  • Select an option

  • Save croxton/16988277f8c0fedd8f006ef086083393 to your computer and use it in GitHub Desktop.

Select an option

Save croxton/16988277f8c0fedd8f006ef086083393 to your computer and use it in GitHub Desktop.
Craft CMS 5 - display related entries on a 'Used by' tab, using a custom template UI element
{% set rels = craft.entries.relatedTo({ targetElement: element }).siteId('*').collect() %}
{% set owners = [] %}
{% for rel in rels %}
{% set owner = rel.owner ?? rel %}
{% set owner = owner.primaryOwner ?? owner %}
{% if owner.canonicalId not in owners %}
{% set owners = owners + {
(""~owner.canonicalId~"") : {
cpEditUrl : owner.cpEditUrl,
title : owner.title,
site: owner.site.name,
section: owner.section.name,
canSave: owner.canSave(currentUser)
}
} %}
{% endif %}
{% endfor %}
<div class="pane">
{% if owners|length %}
<h2>This content is used by the following entr{{ owners|length == 1 ? 'y' : 'ies' }}:</h2>
<ul style="list-style: square; margin-left: 1rem;">
{% for item in owners|sort((a, b) => a.title <=> b.title) %}
<li>
{% if item.canSave %}
<a href="{{ item.cpEditUrl }}">
{{ item.title }}
</a>
{% else %}
{{ item.title }}
{% endif %}
({{ item.site }} ▸ {{ item.section }})
</li>
{% endfor %}
</ul>
{% else %}
<h2>This content appears to be unused by any entries at the moment</h2>
{% endif %}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment