0

I'm afraid my question is a bit to general because I don't quite know where to start. I still really don't have my head wrapped around the drupal 7 theming system.

Scenario

I have two content types:

  1. Type main
  2. Type secondary (this type has a required entity reference field, referencing Type main

The display on Type main has a set of 2 horizontal tabs htabs group named group_expandables.

Desired Outcome

What I would like to happen is, if an entity of Type secondary has been added referencing this node; add a third horizontal tab containing the Type secondary node and make it the default.

Initial Approach

These are the steps I thought through that should probably happen on page load for a node of type Main:

  1. Look for a Type secondary node referencing this node, return if not found
  2. If found, build up a new render array for the Type secondary node htab. I was thinking it would be easiest to build this display in views, and load in the view, but doing it in a template file would probably work too.
  3. Once the variable has been for the new htab, insert it in the htabs render array and modify the render array to remove the open by default indicator on the old default tab.

So far I have used hook_preprocess_page to grab the $variables['page']['content']['system_main']['nodes'][nid]['#fieldgroups']['group_expandables'] and add a new group name to its children, clone one of the htab field groups and append it to the ['#fieldgroups'] array, and modify the elements that referenced the old htab and replace them with the name for the new tab. Once I figured out that I needed an entry in #group_children too, I actually got the tab to appear in the group, but I haven't had any luck getting content to show up in it yet.

Question

Here are the places I'm really hung up:

  1. Is hook_preprocess_page the best place to start this?
  2. Is my general strategy for how to go about this sound?
  3. How would I build my desired content into the new htab?

I'm sorry for the broad nature of the question, any pointers would be greatly appreciated.

1 Answer 1

1

I'm not sure your general strategy is the absolute best way to go, but I don't see any major problems, I think hook_preprocess_page is too broad as it runs everywhere and would suggest template_preprocess_node instead (even though this is a template preprocess it can be run like a normal hook preprocess in a module)

In regards to getting the content you will need to do a EntityFieldQuery using the nid of the page being viewed as a fieldCondition, the only problem I can see is when there is more than one secondary type assigned to the same main type.

An alternative approach would be to see whether it's possible to write a View which takes the nid as a Contextual Filter and then builds all the content, I think you'd need to add a Relatonship to the secondary type, etc. and if it's possible this would cut down on the amount of custom coding needed.

1
  • Thanks for the answer! When I run template_preprocess_node I don't see the #field_groups array that I need to use to put things into field_groups, so either it is somewhere I haven't looked, or I need to use preprocess_page. Thanks for the pointer for EntityFieldQuery. Now that I've managed to show node inside a field group, the next step is to start building the logic for conditionally building and showing that tab, so I'll dig into that soon.
    – UltraBob
    Nov 9, 2018 at 7:46

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.