Hi

Sorry if this has been asked but I'm really struggling here. I've just got back from holiday so maybe I'm just being thick.

I've created a custom views-view-fields.tpl.php file in which I display various HTML depending on what's in put in a field

For this content-type I have a boolean checkbox field, which I've set to "on" for true and "off" for false. I know that it's working as I have <?php print $field->content; ?> or $fields['field_under_offer']->content; and it displays "on" or "off" depending on whether it's checked or not. But I actually want to display different HTML depending on this.

I have an if statement <?php if ($fields['field_under_offer']->content === "on"): ?>

but it just doesn't work. Can someone tell me what the correct way of getting the checkbox value is?

Here's my full code. thanks very much for any help

<?php if ($field->label === "Under Offer") { ?>
		<?php if (isset($field->content)): ?>
			<?php if ($field->content === "on"): ?>
			<?php print $field->content; ?>
			<?php print "This is on"; ?>
			<?php else : ?>
			<?php print $field->content; ?>
			<?php print "This is off"; ?>
			<?php endif; ?>
		<?php endif; ?>
	<?php } ?>

Comments

ntg’s picture

Hello James,
I'm trying to do something similar but I can't get it to work. I tried your method but i had no luck.
Did you manage to find solution?

Thank you :)

Che Molava’s picture

Is the last bit of code from views-view-fields.tpl.php file? If so, is $field obtained by looping through the $fields array? Can you tell us what it does print out?

I know that it's working as I have print $field->content; or $fields['field_under_offer']->content; and it displays "on" or "off" depending on whether it's checked or not.

Try print "<pre>" . $field->content . "</pre>" ; and see whether any markup has been added.
If so you can try if ( FALSE !== strpos($field->content, 'on') ) or if ( $field->raw == 'on' ) .

millionleaves’s picture

I had a scenario where I wanted to use a boolean field on a node to control whether a Read More link was shown when displaying the node.

This code worked for me:

<?php if ($field_show_read_more_link[0]['value']) : ?>
  <a class="read-more" href="<?php print $node_url; ?>"><?php print t('Read More'); ?></
<?php endif; ?>

This checked if the boolean value ($field_show_read_more) was set to 1, and then displayed the Read More link if it did.

I provide Drupal, Drupal Commerce and CiviCRM development services for customers in New Zealand and beyond

Katy Jockelson’s picture

Thank you millionleaves, that worked well for me.