0

I'm doing some work for a design agency and I'm looking into how I can achieve their fanciful designs using Drupal. In particular I'm worried about the formate of their blog posts, it's quite clear that what they want is not going to be satisfied by any kind of WYSIWYG editor. So I'm left with two options:

  1. Allow them to enter HTML and code up each of the blog posts myself (obviously not desirable).
  2. Limit them to a few different templates that they can use for the future.

1 Answer 1

4

I think your best bet is to add an additional field to the blog post content type, which can be used in the theme layer to select the proper template. You will provide the templates in advance, which will result in an easy UI for the editor, but will not sacrifice your ability to customize and tightly control the template.

Step 1: make your new field

This new field should provide them with options for the layouts. For the purposes of this example I'll use a field called field_blog_layout that provides two options: Single Column and Staggered. Make sure you provide machine values in a format that is accepted by the theme hook system, just to make it a tad simpler for yourself in the next step. Here would be the values in your field settings:

single_column|Single Column
staggered|Staggered

Step 2: Add dynamic theme hook suggestions

Now that you have your field, add a new hook inside template.php (or a custom module, e.g. if you have a Blog Feature). My example would live inside a module file called my_feature.module:

/**
 * Implements hook_preprocess_node().
 *
 * Provides theme hook suggestions to complement the custom layout.
 */
function my_feature_preprocess_node(&$vars) {
  // Only run this code on Blog nodes.
  if ($vars['node']->type == 'blog') {
    // First, grab the value of the Layout field.
    $my_layout = field_get_items('node', $vars['node'], 'field_blog_layout');
    // Add the theme hook suggestion using the field's value
    $vars['theme_hook_suggestions'][] = 'node__blog__' . $my_layout[0]['value'];
  }
}

If you were careful about using the expected format for theme hooks within your custom field's machine values, there should be no other processing needed.

Step 3: Implement the theme hook by adding templates

With the theme hooks ready to go, you can now create templates. It's simple, just create a template using the exact format of the theme hook, except replace all underscores (_) with dashes (-):

node--blog--single-column.tpl.php
node--blog--staggered.tpl.php

For the content of these templates, just make a copy of node.tpl.php and customize it to your heart's content.

Note: you must have a file called node.tpl.php in your theme for the special templates to work. This is a known behavior of the Drupal 7 theming system.

Step 4: Clear the cache and marvel at your work

New hooks and templates both require a cache clear. Don't forget or you'll wonder why nothing is changing!

1
  • Thank you very much, this is exactly what I was looking for.
    – rich97
    Aug 6, 2014 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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