3

Let's say I override the way a content type page looks. My content type is Parent. So I create a page named: node--parent.tpl.php

But how about when you want to use nested content types?

Let's say:

  • Content Type Parent
  • Content Type Child

I use node--parent.tpl.php, but how do I display the children that belong to the parent? (linked using Contextual Filters).

For example in node--parent.tpl.php have a way to display:

Parent Name: Joe

Children: Sue, Phil, Mary and Jim

3

Contextual filters are part of Views, they can't be used to link content types together. If you're simulating a relationship between the two types using a contextual filter, it will only exist in the very narrow context of that View.

The standard way to link content types together is with the Entity Reference module. Something similar to the following should get you what you're looking for:

  • Add a multiple cardinality entity reference field to the Parent content type, referencing the Child type.
  • Optionally install Inline Entity Form, if creating the Child content while inside the Parent edit form makes sense for your use case. It provides a widget for the entity reference field type. I won't go into details, if you choose that widget and visit the edit form for a Parent node, you'll see what it does.
  • Use either the autocomplete or inline entity widget to relate the Child nodes to the relevant Parent nodes.
  • Visit the display options for the Parent content type, choose "Rendered Entity" for the display format of your entity reference field, and in the settings choose an appropriate view mode.

That will render the Child nodes inside the Parent node without the need to alter the template file at all.

|improve this answer|||||

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.