0

I'm fairly new to Drupal have content types that loop in children content types using fields. The result is something like this.

Office Content Type
Field: Office Content Type
- Office Content Type
- Field: Person Content Type
- Field: Person Content Type
- Field: Person Content Type
- Office Content Type
-- Field: Office Content Type
-- Field: Person Content Type
-- Field: Person Content Type

I need a content type to be able to recognize it has a parent or grandparent. However this directory was created using content types for the offices instead of taxonomies. Is it possible to pull hierarchical information from nested content types? Should the Office Content Type have been created as a taxonomy instead?

Links to details and documentation is welcome.

I'm using Drupal 9.3.x

4
  • 1
    In Drupal, taxonomies have hierarchy, but content types do not. So if you want a hierarchy, you will likely find it far easier to implement using taxonomy than hacking content types to do it yourself. Mar 18, 2022 at 23:52
  • The only thing that comes close to that for D9 is Corresponding Entity References but based on the description it doesn't sound like it will let you recognize grandparent. So you'll have to use taxonomy unless you want to create your own custom module to handle such content type tree.
    – No Sssweat
    Mar 19, 2022 at 2:32
  • 1
    You can create entity references to create a hierarchy. But it's not clear what you mean by "recognize". It's also not clear what your end goal is.
    – Jaypan
    Mar 19, 2022 at 4:31
  • Thank you everyone for your assistance. Taxonomies, Entity References and even adding a field can be used. I tested a few of them. My colleague clarified and requested a preprocess solution so I'm going to dig a little deeper into advanced PHP. Mar 22, 2022 at 16:02

1 Answer 1

0

I came to a solution using a preprocess function. A key/value pair is added to the parent node. Named "headline_level" it is later referenced by the child and +1 is added. That number is assigned to a twig variable as the headline's number, "headline_level" is increased by 1 for the child if itself has children.

In this image we see a parent "office" pulling in child nodes as fields. enter image description here

Level Diagram

Parent (h1)
|_ Child (h2) (see image)
   |_ Grandchild (h3)
      |_ Great GrandChild (h4)

Preprocess Function for theme

function THEME_preprocess_HOOK(&$variables) {
  $node = $variables['node'];

    /**
     * Parent Headline Level 
     * Displays logical headline structure
     * Applies higher value to 'headline_level' for subordinate content types; used in '/templates/node--HOOK.html.twig'
     */
    if ($node->_referringItem) {
      // Variables
      $counter = 1; // start level at 1
      
      // Append 'headline_level' to HOOK node
      $node->headline_level = $counter;
      $parentHeadline = $node->_referringItem->getEntity()->headline_level;
      
      // Check node for $parentHeadline else $counter, set twig variable
      if (isset($parentHeadline)) {
        // Increase 'headline_level' by 1
        ++$parentHeadline; // add 1
        $node->headline_level = $parentHeadline;
        $variables['office_headline_level'] = $parentHeadline; 
      } else {
        $variables['headline_level'] = $counter;
      }
    }

Twig Template for Content Type

  <div>
    <h{{ headline_level ?: 2 }}>{{ label }}</h{{ headline_level ?: 2 }}>
    {{ content.field_people }}
  </div>

As mentioned in the comments, these Offices should have been a Taxonomy. However, they were created as content types and referenced by fields.

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.