5

I have referenced Paragraph B in Paragraph A and in preprocess of Paragraph B I want to access host paragraph A. Is this possible with ease. As in case of paragraph references on node we can do paragraph->getParentEntity() ? I had tried same but it still get the parent node :(

4

If you look at the Paragraph class in the module, you will find a public method called getParentEntity that return the parent entity of a Paragraph instance.

So, if you do:

$parent = $paragraph->getParentEntity();

if (isset($parent)) {
  // Your stuff ...
}

You would have the parent entity in the $parent variable. You can chain the method if there is multiple nested paragraphs. I also suggest using the isset method on the $parent variable because I've encounter some weird issues where random paragraphs returns null.

  • getParentEntity() returns fully loaded entity. no need to load again – Mudassar Ali Mar 31 '17 at 16:52
  • Okay. I did a quick look of the code and it looked like it returned an id. Does this solve your question? – Kevin Mar 31 '17 at 16:52
  • 2
    Well not really I am doing something custom to reuse paragraph Items for specific needs. For example inside a paragraph, I am referencing existing paragraph item(i.e contents of existing paragraph). End up with two paragraph templates and render them. I need something like variable sharing between those two templates to output something conditional. – Mudassar Ali Mar 31 '17 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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