2

Craft 5, CKEditor accessing parent entry from partial

Is it possible to access the parent entry from a partial template being rendered from an embedded entry in a CKEditor field?

For example, consider a "Page" entry type that has a "Copy" CKeditor field. The "Copy" field allows embedding a "Video" entry type. When the "Copy" field is rendered, it will render the embedded "Video" entry type using the /templates/_partials/entry/video.twig template. From the video.twig partial template, is it possible to access the "Page" entry type?

This may be particularly useful if the "Video" entry type is embedded into different fields and entry types. For example, the "Video" might be rendered differently if it is part of a "Page" entry type vs a "Blog" entry type. As I see it right now, this would not be possible since each entry type can only have a single template and does not have access to it's parent.

Related documentation:

1 Answer 1

3

CKEditor entries (and Matrix entries; collectively known as "nested" entries) in Craft 5 doesn't have a parent (like an entry nested in a structure section would) – they have an owner.

To access the owner entry in a template partial, you can use the entry.getOwner() entry method (or just entry.owner, if you prefer that):

{% set owner = entry.getOwner() %}

{% if owner %}
    <p>My owner is {{ owner.title }}</p>
{% endif %}

Also, worth mentioning that in addition to having an owner, nested entries also always belong to a field (i.e. the "Copy" CKEditor field, in your case).

If you have a need to access that field from a nested entry's partial template (for example, to check the field's handle to confirm which field is being rendered) you can use the entry.getField() (or just entry.field) method in a similar manner:

{% set field = entry.getField() %}

{% if field %}
    <p>I belong to the {{ field.handle }} field!</p>
{% endif %}
1
  • 1
    I will see if I can create a pull request to add these details to the CKEditor documentation related to partials.
    – Jason
    Commented Apr 4 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.