dcummings0001
Total posts: 2
17 Oct 2016 15:49

I am new to Cobalt templates and not well versed in PHP so my question may be very basic but I’ve been unable to resolve how to use a Boolean field in a Cobalt Type that I’ve created to track the status of a playing field for a local soccer club website.

In my template, I am able to obtain the Boolean field and display the Cobalt image/text. For example, when the field record’s Boolean field is set to true, the following code displays the tic.png and “Yes”:

<?php $field = $item->fields_by_id[64]; // Field Status ?>
<?php echo $field->result; ?>

What I am trying to do, instead, is to change the CSS class based upon the state of the record’s Boolean field using an if/else. However, the system fails even with the simplest if statements I’ve tried. For example:

This fails....

<?php $field = $item->fields_by_id[64]; // Field Status ?>
<?php if( $field->result == 'true' ) ?>
  // CSS selection code will be placed here.
<?php endif; ?>

And this fails....

<?php $field = $item->fields_by_id[64]; // Field Status ?>
<?php if( ($field->result as bool) == 'true' ) ?>
  // CSS selection code will be placed here.
<?php endif; ?>

Any insight you could provide to me would be greatly appreciated. Thank you!

Last Modified: 20 Oct 2016


pepperstreet VIP
Total posts: 3,837
18 Oct 2016 01:17

Welcome to the World of Cobalt ;)

dcummings0001 is to change the CSS class based upon the state of the record’s Boolean field using an if/else.

Hello dcummings0001, not sure if I understand your question.

Do you want to optain the class of the boolean field?
Or use its value to set a certain class on another field or element?
"CSS selection code" is the value of the boolean field?


pepperstreet VIP
Total posts: 3,837
18 Oct 2016 01:59

dcummings0001 result as bool) == 'true'

value vs. result

The boolean field could have the following value:

empty (default can be set in field parameters)
0
1

If you want to check for the simple status, then you should check the raw value:

$item->fields_by_id[999]->value == 1


The result is the final output, including additional output formatting from the field template.

If you want to use the text-label, you might strip out the HTML tags from the result:

echo strip_tags($item->fields_by_id[999]->result)

Since the yes/no texts are field parameters, you could also access them directly:

$item->fields_by_id[999]->params->get('params.true')

$item->fields_by_id[999]->params->get('params.false')


dcummings0001
Total posts: 2
20 Oct 2016 02:17

Yes, checking for simple status is fine for what I need to do.

Thank you!


Sergey
Total posts: 13,748
20 Oct 2016 05:17

Right, as @papperstreet has already mentione you have to use ->value instead of ->result. Value of boolean is either 1 or -1

<?php $field = $item->fields_by_id[64];?>
<span class="boolean-<?php echo ($field->value == 1 ? 'true' : 'false') ?>">
    <?php echo $field->result; ?>
</span>

Somethign like this as I see it.

Powered by Cobalt