1

This is similar to Considerations displaying page titles in page or node template which in turn claims to be similar to another question, but the questioner doesn't mention how they achieve what they are doing, and what they are doing may be different from what I'm doing.

In my case, on single node view pages, for certain (most) content-types, there is an optional image field, that needs to be floated left of the title.

the layout I need

I'm not sure if it would be best practice to move the title output down into the node.tpl.php template or to keep it in the page.tpl.php template and load the relevant field for the node in the page template.

This is what I've done:

template.php:

function enviroscope_preprocess_page(&$variables, $hook) {
  if (isset($variables['node']->field_doc_cover)) {
    $node = node_load($variables['node']->nid);
    $output = field_view_field('node', $node, 'field_doc_cover', array('label' => 'hidden'));
    $variables['field_doc_cover'] = $output;
  }
}

page.tpl.php:

<?php print render($title_prefix); ?>
<?php if ($title): print render($field_doc_cover); ?><h3 class="title" id="page-title"><?php print $title; ?></h3><?php endif; ?>
<?php print render($title_suffix); ?>

This gets the image field printed before the title, but I'm not sure if there is an equivalent here to hide($content['field_doc_cover']);? Currently this has field_doc_cover printing twice.

So the main question is,

Is this the right approach?

but this breaks down into some more specific questions:

  • Should the title definition stay in page.tpl.php for single node pages?
    • If so, is there a good way to exclude the field_doc_cover field from being rendered in the node.tpl.php file without messing with that template too?
  • Is there a good reason not to handle the title in the node.tpl.php, and remove it from the page.tpl.php file for these content_types?
    • If there is a good reason for it to not be in nodes, would it be sensible to render the element in page.tpl.php and send it as a variable to be used in node.tpl.php?

I'm pretty new to drupal theming as is probably obvious, so examples with your answers would be very helpful!

Update: I was asked to create a visual representation of what I'm trying to do, which was a good call because it reminded me of one more issue: The last modified date should be printed above and to the right of the title.

I guess one of the main things I don't understand is whether there is some good reason why the title needs to be part of the page.tpl.php template. It sure would be easier if it were down in the node template.

  • I still don't really get what you're trying to achieve. A visual drawing/picture of the desired output would help. – No Sssweat Feb 3 '16 at 4:00
3

It is valid to do it the way you are doing it and it would also be valid to not print the title in the page tpl for certain nodes and instead print it in the node template.

It's subjective as to which is preferable (if one is even better than the other) and the preferred solution may change based on your specific requirements for a given project.

One thing to consider is anything that usually prints out between the title and the content in your page template. Will moving the title into the content (in this case the content is essentially the node template) mean these nodes will be inconsistent to the rest of the site (for example, will messages/tabs/help/etc. be in a different place on these nodes)?
If they were to be different would it matter to you?

Yes, you can still use hide($content['field_doc_cover']); in your node template in this case.

There are also alternatives depending on your site and requirements. For example the Panels module allows you to assign specific fields to different regions and there are a number of ways to get a block to show parts of a node so that you can assign that to a different region using Drupal core or the Context module or similar.

  • OK, I thought maybe there was something to do with the RDF module or other pre/post title hooks that would make this the wrong approach. If I could ask a quick follow-up, What would be the best way to check content type and toggle the visibility of the title in template_preprocess_page()? – UltraBob Feb 3 '16 at 5:54
  • @UltraBob have a look at this How to hide page title based on node type using template_preprocess_page() – No Sssweat Feb 3 '16 at 6:17
  • @nosssweat awesome, thanks! Based on that I got something working. Now if I could just figure out how to pass a variable from my preprocess_node function to my preprocess_node function, I could stop repeating myself. (That IS the order they execute in isn't it?) – UltraBob Feb 3 '16 at 9:43
  • 1
    You might also be interested in node type specific page templates. For that see drupal.org/node/1089656#contenttype – rooby Feb 3 '16 at 13:09
  • 1
    @UltraBob as for the flag, generally you just have to put the code in both preprocessors. Alternatively, if the logic is not really simple you could create a helper function and call that from each preprocessor. – rooby Feb 4 '16 at 6:17

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.