1

I have built a custom content type that has a boolean field called 'Hide images'. There is also an images field that displays all images at the bottom of the node. I'd like to setup a way so that depending on the 'Hide images' field I can decide whether to display the images field. Is there a way to edit this via a template?

I saw in page--node--contenttype.tpl.php that I could render($page['content']); but I couldn't actually see how I could choose what content to display.

1 Answer 1

1

In your template, instead of rendering the entire content array, you can break it down to print each individual item separately. Then, you could wrap the printing of the images in a statement that checks if the boolean value is true/false.

<?php if ($content['field_boolean_hide_images'] === FALSE): ?>
  <?php print render($content['field_image']); ?>
<?php endif; ?>

This is just an example and shouldn't be used verbatim.

Also, to create a template for your content type, it would be node--yourContentType.tpl.php rather than a page template override.

6
  • I can't seem to get it to work. I'm using print <?php render($content['field_image']); ?> Without using an if statement (to test whether I can render it at all) But nothing renders. I have double checked to make sure that is the machine name of my field.
    – Amy
    Commented Jun 1, 2015 at 18:27
  • Are you certain your template file is being used? Did you forget print before render()? Commented Jun 1, 2015 at 18:32
  • Yes I'm using print. Sorry I omitted that from the comment. I'm certain that the template file is being used because I added junk text as a test and the junk text appears on the page.
    – Amy
    Commented Jun 1, 2015 at 18:36
  • You should install devel and add dpm($content) in your template to be sure that you have the render array named correctly. If field_image was correct it would certainly print. You can also try printing other fields to see if there is an issue with the image field. Commented Jun 1, 2015 at 18:46
  • Also, this would be a node--yourContentType.tpl.php template, not a page template. Commented Jun 1, 2015 at 18:48

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.