How to getParentEntity() in node preview mode?

Created on 17 July 2018, over 6 years ago
Updated 14 December 2023, 11 months ago

I'm doing quite a lot of preprocessing in my theme, and I noticed that sometimes my preprocessing does not work in node preview mode. I've got quite a lot of code similar to

function MYTHEME_preprocess_paragraph__TYPE(array &$variables) {
  $paragraph = $variables['paragraph'];
  $parent = $paragraph->getParentEntity();

  if ($parent && $parent->whateverCondition()) {
    ...
  }
}

I noticed that in node preview mode getParentEntity() is often (but not always) null. It seems that getParentEntity() does work in case of nested paragraphs, but does not work if the paragraph is direct descendant of a node.

I guess it is related to the missing node id for preview nodes (they only seem to have an UUID), but I don't know how to work around this limitation.

Can anyone give me some hints how to reliably fetch the parent entity? (must work for nodes and nested paragraphs, and must also work in node preview mode)

💬 Support request
Status

Active

Version

1.3

Component

Code

Created by

🇦🇹Austria hudri Austria

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024