-
Notifications
You must be signed in to change notification settings - Fork 638
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
[5.x]: Can't render element cards in "Template" field layout element #16267
Labels
authoring ✍️
features related to author experience
Comments
brandonkelly
added
authoring ✍️
features related to author experience
and removed
bug
labels
Dec 5, 2024
Not sure if this helps you but we render a list of all related entries in a template UI element with the following code: {# Fetch related entries #}
{% set relatedEntries = craft.entries()
.relatedTo(element)
.orderBy('title')
.all()
%}
{# Group entries by their entry type #}
{% set entriesByType = relatedEntries | group('typeId') %}
{% for typeId, entries in entriesByType %}
{# Entry Type Name as Subheading #}
{% set typeName = entries[0].getType().name %}
<h3>{{ typeName }}</h3>
<ul style="list-style: square; margin-left: 1rem;">
{% for entry in entries %}
{% set relatedFields = [] %}
{% set fieldLayout = entry.getFieldLayout() %}
{% if fieldLayout %}
{% for field in fieldLayout.getCustomFields() %}
{% set fieldValue = entry.getFieldValue(field.handle) %}
{% if fieldValue and attribute(fieldValue, 'ids') is defined %}
{% set relatedIds = fieldValue.ids() %}
{% if element.id in relatedIds %}
{% set relatedFields = relatedFields | merge([field.name]) %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
<li>
<a href="{{ entry.getCpEditUrl() }}">[{{entry.parent}}] ({{entry.section}}) {{ entry.title }}</a>
{% if relatedFields | length %}
<p><em>Related by:</em> {{ relatedFields | join(', ') }}</p>
{% endif %}
</li>
{% endfor %}
</ul>
{% endfor %} |
brandonkelly
added a commit
that referenced
this issue
Dec 9, 2024
Thanks for pointing that out! I just made it possible to start using CP template-exclusive Twig functions in site templates rendered via “Template” UI elements, for Craft 5.6. (6767471) |
Craft 5.6.0 is out now 🚀 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happened?
Description
We use a template field element to render where particular elements are referenced (e.g. show a list of blog posts referencing a particular product). To keep things stylistically homogenous, I used
{% hook "cp.elements.element" %}
to render a link to the elements. In Craft 5, this hook is deprecated and we're told to use theCp::elementCardHtml()
function; however, this function is not available in thesite
template mode, which is what's used for rendering these templates.Here's the Twig code for the embedded template: https://gist.github.com/Mosnar/f8aebd38fc4548dc9249631401666fc9
Steps to reproduce
{% hook "cp.elements.element" %}
(or insert code from attached Gist)Expected behavior
Ideally, I'd like to be able to use
elementCardHtml()
in the Template field layout element without needing to write a custom plugin or module.Craft CMS version
5.5.3
PHP version
8.2.26
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
No response
The text was updated successfully, but these errors were encountered: