1

I am a newbie to Drupal. I have a custom content type blog_post, with some custom fields, like subtitle.

I have the following two divs, which I want to render for this blog_post like that:

+--------------+--------------------------------+
| 9 Sept 2013  | Good weather on the West coast |
|    2013      |           (title)              |
|   (date)     | Winds are blowing hard in NY   |
|              |          (subtitle)            |
|              | According to the Department... |
|              |          (content)             |
+ -------------+--------------------------------+

My solution: amend node.tpl.php like that:

<aside>
    <?php print $date; ?>
</aside>
<article>
  <a href="<?php print $node_url;?>">
    <?php print $title; ?><br />
    <?php print $subtitle; ?>
  </a>
  <?php print render($content); ?>
</article>

How do I pull $subtitle from the database? Best way I found so far, looks somewhat ugly:

<?php
  print $field_subtitle[LANGUAGE_NONE][0]['value'];
?>
4
  • Hello and welcome. Please try not to put 3 questions as one. And Q1 & Q2 falls easily into definition of "primarily opinion-based". Only Q3 can possibly have good, definite answer.
    – Mołot
    Sep 10, 2013 at 6:59
  • 1
    Thanks. I corrected the question, even though I believe an overview of "best practices" should be allowed to a certain extent and these questions are valid and relevant to the original question. Sep 10, 2013 at 20:06
  • 1
    Alexei - I believe Mołot's main point is not that the questions are not allowed or otherwise inappropriate, but rather that they're bundled within a single StackExchange question. The idea of the site is to have one question per submission. That said, answers coming right up... Sep 10, 2013 at 20:26
  • @YuriyBabenko exactly, that was my main point. Lesser point was that each one separately might need a bit of care and attention, too. Now they are too crowded to be good, not enough space and attention span ;)
    – Mołot
    Sep 10, 2013 at 20:54

2 Answers 2

2

Custom layout options can be managed in numerous ways. Just a few of the options:

  1. Use Display Suite (KyleTaylored's answer).
  2. Use separate node templates and render the fields you're after manually via field_view_field(). See Rendering Drupal 7 fields (the right way).
  3. Use a single node template with conditional statements based on $node->type
  4. Use CSS to extensively modify layout
  5. Modify the node content through a hook, such as hook_node_view_alter().

These days I prefer to go with option 1, as it does what I want 99% of the time. You can alter displays on a per-entity (node) and per-view (full, teaser, etc. - you can create custom views, too), so it's quite flexible.

Option 2 is another alternative that can be fairly clean. Drupal's theming/rendering engine looks for multiple template "suggestions" (variations of the file name), and picks the first option found. The template suggestions can be modified and expanded in a pre-processing function/hook. You could add suggestions based on any data you have available. I have an old blog post (Extending Drupal Templating) which provides examples of how to do this for Drupal 5 and 6, but the same principle applies to Drupal 7.

Option 3 is simple enough to not need an explanation, but unless you're adding very small changes, (different layout is not a small change) I'd advise against it.

Option 4 will obviously depend on your markup. A lot can be done with CSS, but drastic layout changes are rarely a good idea.

Option 5 is another extreme. Useful for tying in to the rendering process at a fairly early stage, but also not something I'd use in your case.

3
  • I appreciate the overview of options. I am going to stick with Option 2 for right now, as I assume it gives maximum possible flexibility. Could you include an example of inserting field variable to your template to your answer next to Option 2? Sep 11, 2013 at 5:56
  • Sure. I've added a link to a blog post which walks you through the proper way of rending fields. Sep 11, 2013 at 17:03
  • Thanks. The linked article is what I was looking for and is amazing! Sep 11, 2013 at 20:37
4

Best suggestion, don't start doing custom template overrides. With site building tools like Display Suite, you no longer have to continue writing custom node templates. By using Display Suite, you can set a view mode you defined to a two-column template, and then set the fields you'd like into each region.

You can see in the example below how easy it is to achieve something like this without having to build custom templates. Of course if you need to do anything extra, you can always customize on top of that.

Display Suite Example

Not the answer you're looking for? Browse other questions tagged or ask your own question.