Score:0

Content Type Hierarchy

in flag

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

cn flag
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.
No Sssweat avatar
ua flag
The only thing that comes close to that for D9 is [Corresponding Entity References](https://www.drupal.org/project/cer) 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.
Jaypan avatar
de flag
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.
in flag
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.
Score:0
in flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.