41

I'm looking to render a certain field before the node's title when displaying a teaser in D7.

Effectively I want to go from this to this.

I have looked into updating the node.tpl.php but I notice that it separates $title from $content so I'm unsure how to approach this. After some searching I have found that people have suggested using CCK and ConTemplate modules to get around this in Drupal-6 but I'm wondering if there is a more Drupal-7 solution.

Any suggestions?

  • Apart from all php-based template solutions, you can also come a long way with CSS only. Negative margins can do a lot. In your jsfiddle example it would look like this. – marcvangend Apr 1 '11 at 11:42
  • I had considered this but what happens for long titles? – irishbuzz Apr 1 '11 at 12:47
  • You're absolutely right, this only works if the title has a fixed height. Like every solution, it has its weak points. Personally, I think that (regardless of the design and technology you're using) long titles should be prevented by educating the editors, but I realize that's not always an option. – marcvangend Apr 1 '11 at 22:45

11 Answers 11

53
+50

This is really quite simple with Drupal 7.

With render and hide you know have the posibility to control the placement of a single element in $content without having to print out all the items yourself like was needed in Drupal 6.

A simplified node template looks like this:

<div>
  <h2><?php print $title; ?></h2>
  <div class="content">
    <?php print render($content); ?>
  </div>
</div>

A little modification would handle your needs:

<div>
  <?php print render($content['field_name_of_image']); ?>
  <h2><?php print $title; ?></h2>
  <div class="content">
    <?php print render($content); ?>
  </div>
</div>

Using render, you can output an element. This is more clever than simply printing stuff, as Drupal will keep track of what has been rendered, to avoid the same thing being printed twice on the place. When rendering the field above the title you are also removing for what's rendered when $content is rendered.

  • 2
    Very nice. In an ideal world it might be better to just tell Drupal to treat the title field like any other field but at least in Drupal 7 we have simple solutions like this. Kudos @googletorp! – irishbuzz Apr 1 '11 at 16:24
  • For the record: This is hardly different from Drupal6, in contrary to what the comment above might insiuate. – berkes Apr 19 '11 at 10:35
  • 1
    @berkes: If you print out fields individually in D6, you have to maintain the node template, and edit code to be able to add new fields. This is a big difference compared to D7 where you can keep adding fields and using the UI to sort them as well. – googletorp Apr 19 '11 at 10:48
  • render is indeed new. But its concept is not. There are several tricks to achieve the same. I did not mean to say that in Drupal6 this is just as simple, clean and nice. Merely that it is possible too. – berkes Apr 19 '11 at 13:42
  • @googletorp is there a way to print a field in page.tpl.php? – CocoSkin Aug 20 '14 at 17:49
9

Display Suite is also pretty cool.

Display Suite allows you to take full control over how your content is displayed using a drag and drop interface. Arrange your nodes, views, comments, user data etc. the way you want without having to work your way through dozens of template files. A predefined list of layouts (D7 only) is available for even more drag and drop fun!

  • It's a cool module alright but unfortunately it only seems to offer control for non-title fields so no help here. Thanks all the same. – irishbuzz Mar 29 '11 at 12:17
  • Actually that's not true, you just need to install all the required modules: drupal.org/project/ds, drupal.org/project/nd & drupal.org/project/nd_contrib – Decipher Apr 4 '11 at 2:04
  • @Decipher: Installing 3 modules to move an image-tag above the h2 tag? You must be kidding, right :) – berkes Apr 19 '11 at 10:36
  • @berkes - Installing Drupal to display one webpage, you must be kidding right? My point is that with these modules installed you can do so much, just as with Drupal you can do so much, but you can also use it to solve a menial task. – Decipher Apr 19 '11 at 10:39
  • @Decipher: For sure, installing generic modules brings new opportunities to your site. But it is ill advice to install views, view-gallery, cck, filefiend and image-field to simply display a row of images. It would be good advise if views, cck and the rest will be used for more then only this list-of-images. But the OP did not mention this at all. – berkes Apr 19 '11 at 13:47
8

Have a look at the Title module - http://drupal.org/project/title. It might prove useful for your scenario. I installed/enabled it for some reason, probably similar to yours, and it seemed to give flexibility of how to place the title field.

I have since disabled it (and am using Display Suite), so my recall might be a bit sketchy.

5

This is a hackish solution and I'm sure someone will come up with something more elegant, but in case you're stuck, and no one does...

You could use page.tpl.php to remove the printing of $title altogether, and add a new custom field called Title with machine name $new_title or something like that. Then on node edit, hide the original Title box, and on node Save use Computed Field or some custom php to copy the $new_title to $title.

After all that, write some css to make $new_title look like the page title did before and you should be able to place whatever fields in whatever order you like, just by dragging and dropping in the manage fields screen. Because you copied over $new_title to $title, the page title in the browser bar should still work and nothing should complain about missing node titles.

Like I said, it's a hack - but it would work. Let me know if you need more info

  • 1
    Thanks for the suggestion Frederik. As you indicate it's far from ideal but looks like one of the few possible workarounds. I will add to your solution by suggesting that the auto_nodetitle module should be used if taking this approach. I will leave the question open for now as I really hope there is some better way and I may start a bounty. +1 for your effort all the same. – irishbuzz Mar 31 '11 at 16:32
4

page.tpl.php (garland)

...
      <div id="center"><div id="squeeze"><div class="right-corner"><div class="left-corner">
          <?php print $breadcrumb; ?>
          <?php if ($mission): print '<div id="mission">'. $mission .'</div>'; endif; ?>

          /*
           * Print custom block or var
           */
          <?php if ($custom_block): print '<div id="custom-block" class="clear-block">'. $custom_block .'</div>'; endif; ?>

          <?php if ($tabs): print '<div id="tabs-wrapper" class="clear-block">'; endif; ?>
          <?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
          <?php if ($tabs): print '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
          <?php if ($tabs2): print '<ul class="tabs secondary">'. $tabs2 .'</ul>'; endif; ?>
          <?php if ($show_messages && $messages): print $messages; endif; ?>
          <?php print $help; ?>
          <div class="clear-block">
            <?php print $content ?>
          </div>
          <?php print $feed_icons ?>
          <div id="footer"><?php print $footer_message . $footer ?></div>
      </div></div></div></div> <!-- /.left-corner, /.right-corner, /#squeeze, /#center -->
...

template.php

/**
 * Override or insert variables into the page template.
 */
function phptemplate_preprocess_page(&$variables) {
  $node = $variables['node'];
  if($node->type == MY_NODE_TYPE) {
    $variables['custom_block'] = 'bla bla bla';
  }
}
  • I'm not sure I follow what's going on here as the first code snippet gives little indication of what is new and what is existing. I'm also using the Zen theme rather than Garland although the required changes should be similar. In the second code snippet are you invoking a hook_preprocess_page()? Can you expand with a more concrete example? – irishbuzz Mar 31 '11 at 16:39
  • In second code we had define a new variable 'custom_block'. Look at part of first code where we are 'custom_block' printing. <?php if ($custom_block): print '<div id="custom-block" class="clear-block">'. $custom_block .'</div>'; endif; ?> just define rules for generate this variable. So? :) – dobeerman Mar 31 '11 at 21:28
4

This doesn't apply to the Teaser question in the original post, but for people who stumble across this and just want to make it happen in the full view, here's a code-free option: display the field in a block (using views, for instance) and place that block above your content block but in the same region.

  • I'm still a drupal n00b, could you explain what you mean? – Kenny Wyland May 15 '12 at 3:55
3

I think you should to use preprocess functions in your template.php

/**
 * Override or insert variables into the page template.
 */
function THEMENAME_process_page(&$variables) {
  // look for $variables['page_title'] if i remember
}

And as additional

/**
 * Override or insert variables into the node template.
 */
function THEMENAME_preprocess_node(&$variables) {
  //
}

dump variables and do anything what you want. add/remove/update.

  • 1
    Those functions don't alter the order in which the title and other node fields are output. For that, you should change the template file. – kiamlaluno Mar 29 '11 at 22:20
  • I see.I thought you would like to add a field value to title. – dobeerman Mar 29 '11 at 22:49
3

I resolved this by codeless solution using Display Suite and its "Region to Block" extras feature. I like this solution most for its flexibility.

Basically, enable "Region to Block" extras part, enable "Fulltext Content" display in Display Suite. (assuming you want to display content above title in the full-node display, similarly you can use it for any other view mode except Default). Now, in "Manage display" part of "Fulltext Content", at the bottom of the screen, the "Block Regions" tab should appear. Enter new block name there, save and drag&drop the desired fields to this block. Lastly, move the newly created block in Blocks screen somewhere above the node title.

You can see the video of same approach here: http://www.youtube.com/watch?v=y4wyjxyhbNA , of course you need to place the block above title (not to the left sidebar) at the end.

2

For zen templates below code works for me.

<?php
// this goes in template.php
// check if node type page and then add to $vars array
// declaring $vars['foo'] gives you a $foo variable to use in page.tpl.php

function YOURTHEMENAME_preprocess_page(&$vars, $hook) {
    if (isset($vars['node'])) :
        if($vars['node']->type == 'page'):       
            $node = node_load($vars['node']->nid);
            $output = field_view_field('node', $node, 'field_image', array('label' => 'hidden'));       
            $vars['my_image_field'] = $output;               
        endif;
    endif;
}
?>

So finally, you can easily print your field in page.tpl.php;

<?php
if ($my_image_field):
   print render($my_image_field);
endif;
?>
  • regarding the page.tpl.php, I was getting an undefined variable warning with that with Business Responsive Theme but when I removed the conditional, it works fine. In other words, I changed <?php if ($my_image_field): print render($my_image_field); endif; ?> to <?php print render($my_image_field); ?> Which seems to not throw any warnings across all my pages. – Amy Niebel Jan 27 '15 at 18:37
2

I know you would have already used some theme to build and now trying to add the fields at the top of title.

My Suggestions to all developers building theme on Drupal 7 would be to use: Omega theme + Context and Delta modules. This gives a very strong foundation for your layout.

And flexibility on arranging Regions, Blocks and Fields.

1

You can do ist with panels module

  • Old answer here, but this caught a flag. It would be better to explain a bit how Panels can be used in this situation. – mpdonadio May 2 '17 at 0:05
  • Panels is huge overhead for just moving a field in a different position. – Akko May 16 '17 at 12:05

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.