Is it possible to add conditionals inside .tpl.php files? For example, if I put a conditional in field.tpl.php, it will check for the field name which is trying to access it (e.g. 'field_foo'). That way instead of making multiple field--['field_name'].tpl.php files, all I need is just one (field.tpl.php).

Another way you can look at it is "How do I get the name of the page/region/node/field which is trying to access this .tpl.php file?"

Comments

koneru.46@gmail.com’s picture

I think you can use them.
refer to http://api.drupal.org/api/drupal/modules--field--theme--field.tpl.php/7 for avliable variables so that they can be used in conditions.Not sure though.feel free to correct me if i am wrong.

enchance’s picture

hehehe... *cue thunder*

Now I can consolidate all my template files into one primary file giving me more control since everything is in code instead of in files.

For reference, here's a sample code to see how you can use it in your themes:

<!-- CONTENTS OF FILE: field.tpl.php -->

<?php if( $element['#field_name'] == 'field_foo' ) : ?>

	<!--  This would be the equivalent of the file field--field_foo.tpl.php. -->
	<?php foreach ($items as $item) : ?>
		<?php print render($item); ?>
	<?php endforeach; ?>

<?php else: ?>
	
	<!--  Everything else other than 'field_foo' goes here -->
	<!-- Nothing new with the code below. I think I got it from Zen. -->
	<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
	  <?php if (!$label_hidden) : ?>
		 <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
	  <?php endif; ?>
	  
	  <div class="field-items"<?php print $content_attributes; ?>>
		 <?php foreach ($items as $delta => $item) : ?>
			<div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
		 <?php endforeach; ?>
	  </div>
	</div>

<?php endif; ?>
koneru.46@gmail.com’s picture

or you can even use

   switch($element['#field_name']) {
   
}

glad it helped.

rolodmonkey’s picture

Install the Theme Developer module, it will be your best friend...

http://drupal.org/project/devel_themer

--

Read more at iRolo.net