From 43b8925d17e11c47908e397898e00484808db1e6 Mon Sep 17 00:00:00 2001 From: August Miller Date: Wed, 7 Feb 2024 14:00:10 -0800 Subject: [PATCH] In-line rendering of Work entries in CKEditor! --- templates/_partials/entry/embed.twig | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 templates/_partials/entry/embed.twig diff --git a/templates/_partials/entry/embed.twig b/templates/_partials/entry/embed.twig new file mode 100644 index 00000000..20ac4717 --- /dev/null +++ b/templates/_partials/entry/embed.twig @@ -0,0 +1,40 @@ +{# + This template is rendered whenever a CKEditor field’s content is output in + the front-end. The `entry` variable here is the element you created inside + CKEditor. You can access the element that CKEditor field is attached to via + the `owner` attribute. + + For performance, consider using `.eagerly()` when accessing relational + fields off of the embedded entry. This will allow Craft to lazily eager-load + other nested entries inside of the CKEditor field + + Each embedded/nested entry type gets its own template, named the same as its + entry type. This one will only be rendered for CKEditor fields that allow + creation of nested `embed` entries. +#} + +{% set attachment = entry.attachment.eagerly().one() %} + +
+ {# We’ll want to handle each type of attachment differently! #} + {% switch attachment.type.handle %} + {% case 'work' %} + {# Ok, they’ve embedded something from the “Collection” section! #} +
+ {% include '_/components/picture' with { + asset: attachment.representativeImage.one(), + transform: 'base', + sizes: sizes ?? '100vw', + lazytransition: 'lazyfadeDelay' + } %} + +
+ {# Auto-format a caption with the work’s metadata: #} + “{{ attachment.title }}” — {{ attachment.artist }} ({{ attachment.year }}) +
+
+ {% default %} + {# Just let it handle rendering itself by calling another of these partials: #} + {{ attachment.render() }} + {% endswitch %} +