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

[5.x]: Can't render element cards in "Template" field layout element #16267

Closed
Mosnar opened this issue Dec 3, 2024 · 3 comments
Closed

[5.x]: Can't render element cards in "Template" field layout element #16267

Mosnar opened this issue Dec 3, 2024 · 3 comments
Assignees
Labels
authoring ✍️ features related to author experience

Comments

@Mosnar
Copy link
Contributor

Mosnar commented Dec 3, 2024

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 the Cp::elementCardHtml() function; however, this function is not available in the site 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

  1. In a field layout "Template" element, use {% hook "cp.elements.element" %} (or insert code from attached Gist)
  2. See deprecation warning logged

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

@Mosnar Mosnar added the bug label Dec 3, 2024
@brandonkelly brandonkelly added authoring ✍️ features related to author experience and removed bug labels Dec 5, 2024
@humandevs
Copy link

humandevs commented Dec 8, 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}}) &nbsp; {{ entry.title }}</a>
                {% if relatedFields | length %}
                    <p><em>Related by:</em> {{ relatedFields | join(', ') }}</p>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endfor %}

@brandonkelly
Copy link
Member

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)

@brandonkelly
Copy link
Member

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
Labels
authoring ✍️ features related to author experience
Projects
None yet
Development

No branches or pull requests

3 participants