1

Been struggling with this for a while... I know there is probably a fairly easy way to accomplish it but I haven't been able to figure it out.

The goal: When a user creates a new page for the site, I would like them to be able to choose from several different headers using a drop-down list. I know I can set up blocks for the different headers and use the built-in functionality to have them appear on different content types or pages, but for various reasons I would like to have the header as a custom field within the page content type.

I have created a "Header Content" content type and used the Entity Reference module to populate a select list field in the page content type. I can display the selected option or the rendered entity it refers to in the body of my page without any trouble.

What I can't figure out is how to display the rendered entity in the header section instead of the main content.

Put another way: When creating a new page or editing an existing one, the user should be able to select existing nodes from a drop-down list (this part is done). The selected node should show up in the header region (this part is giving me trouble).

I think it could be accomplished in one of two ways:

1) Using Views: I tried creating a views block to read either the reference or rendered content from the parent page and display it in the header (or wherever I place the block) but I can't seem to get the relationship right.

2) Using a custom module This seems pretty straightforward but I'm not sure how to reference the value of "field_header" (the name of my custom field using hooks. I'm also not sure if the data will be available if I configure field_header not to display in the configuration.

This seems like a problem that's likely to be solved already. Maybe I'm not phrasing it correctly when I search because I haven't been able to find any examples to help me solve it.

1

What I'm 99% sure you want is the Header Image module. It works by being able to select a piece of (node) content to place in some region thats been pre-defined.

Eg, put an image in some header region of your site. Or text or whatever ....

  • Thanks Tenken, but I'm using 7.x... perhaps I should've mentioned that... Header Image doesn't have a 7.x version. I think I'll take a look and see if I can't get it to work anyway. – BriSull Apr 17 '12 at 5:05
  • 1
    there is a 7x version in the issue queue. I know because I'm using it :) – tenken Apr 18 '12 at 2:30
1

Hmm... looks pretty good but the important thing I was looking for was the ability to choose the header from a drop-down when creating the page. As far as I can tell, the module only works from the other direction -- assigning nodes to the header.

I was able to hack it together by editing page.tpl with the following:

// If the current node has a field called "field_header" and that field has content
if( $field = field_get_items('node', $node, 'field_header', $node->language) ) {
    $nid = $field['0']['target_id'];
    $delta = 0;
    $language = 'und';

    $entity = entity_load('node', array($nid));
    $header_content = $entity[$nid]->body[$language][$delta]['value'];
}
if ( !empty( $header_content ) ) {
    // Hacked together replacement for header content
    echo '<div class="region region-header"><div class="block-inner clearfix">';
    print render($header_content);
    echo '</div></div>';
}
else {
    // Render original header content
    print render($page['header']);
}

I'm sure there are a zillion better ways to accomplish this -- and I plan to figure out a few when I have time. For now, this works pretty well considering I already had blocks set up with header content that now act as fallbacks.

Thanks again for your help -- I'd love to see any other pointers. This is my first Drupal site and I'm very impressed and a little intimidated so far.

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.