0

Problem: I have a content type called "Homepage" of which there is only one node instance (which is pointed to as the front page), that contains a bunch of editable fields so the client can update certain front page content in several places.

They want to be able to, now and then, change the front page to a slightly different layout to focus on a call to action. This will probably be an

  • extra content-editable text area
  • a driver image

That will take the place of an image on the front page. This image is output in node--homepage.tpl.php as you might expect.

My proposed solution:

  • Create two new fields in the Homepage content: driver text, driver image.
  • Create an admin page, or add to an existing admin form a button: "Driver mode". When this button is enabled,
  • Make the Homepage content type reference a new template that hides the old image and outputs the new fields.

Is it possible to do this - use a new template depending on some set variable, or should I have some logic in my node--homepage.tpl.php that conditionally outputs one bunch of HTML or another depending on the variable set in the admin? E.g. (rough code, ignore syntax/logic errors)

<?php

if (variable_get('driver_mode'))
{
   print render($content['field_driver_image']);
   print render($content['field_driver_text']);
}
else
{
   print render($content['field_homepage_image']);
}

?>

So basically, I'm asking what you think is a simple, clean and/or Drupal way to solve my problem above? Note that the code above is just a general example to support what I'm trying to achieve, no need to analyse it's correctness, I'm more interested in what your approach would be - should I perhaps be preprocessing the output in template.php depending on whether driver mode is on or off, so that I only output one variable, which produces the desired output depending on state?

1

My version is:

  1. Create needed fields (driver image and driver text) for node type
  2. Add checkbox field to the frontpage node type ('Driver mode')
  3. Use template_preprocess_node(&$variables) or hook_node_view() to make some changes if checkbox field is checked
  • Yes, adding the checkbox to the content type seems much better, and preprocess does make sense to seperate the display and logic. Thankyou! – njp May 30 '13 at 10:57

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.