danielbidala VIP
Total posts: 153
02 Apr 2015 19:17

HI all

I want to display filed label in article template even if the field value is not set.

I try this way:

<?php echo $item->fields_by_id[179]->label; ?>

But it isn't work. Any idea?

Last Modified: 20 Apr 2015



Jeff VIP
Total posts: 745
03 Apr 2015 02:58

You didn't put <?php if(isset($item->fields_by_id[179])): ?>

before <?php echo $item->fields_by_id[179]->label; ?>

did you?


danielbidala VIP
Total posts: 153
03 Apr 2015 06:23

No matter how you try, if the field is empty (not set) you won't get the label.

I try this way:

<?php if(isset($item->fields_by_id[179])): ?>
        <label><strong><?php echo $item->fields_by_id[179]->label; ?></strong></label>
        <?php echo $item->fields_by_id[179]->result; ?>
<?php else: ?>
        <label><strong><?php echo $item->fields_by_id[179]->label; ?></strong></label>
<?php endif; ?>

I realized that you don't need to use if isset statement. If the field is set you will get the value and the label this way:

<?php echo $item->fields_by_id[179]->label; ?>
<?php echo $item->fields_by_id[179]->result; ?>

But if the field is empty (not set) the above method doesn't work.


pepperstreet VIP
Total posts: 3,837
03 Apr 2015 13:22

Maybe something that should be improved in COBALT 9. A parameter for output behavior... Either a global setting per type, or even per field.

  • Show/hide empty fields: YES/NO
  • Choose Placeholder for empty fields:

( PS: @Jeff Like in Joomlapolis' CB global config or JSN Easy-Profile)


Example parameter from CB (CommunityBuilder)
cb_global_config_show_empty_options


Jeff VIP
Total posts: 745
04 Apr 2015 01:12

Workaround:

<?php if(isset($item->fields_by_id[179])): ?>
        <label><strong><?php echo $item->fields_by_id[179]->label; ?></strong></label>
        <?php echo $item->fields_by_id[179]->result; ?>
<?php else: ?>
        <label><strong>Custom label text</strong></label>
<?php endif; ?>

Sergey
Total posts: 13,748
20 Apr 2015 06:13

Actualy if you want to show your label always, you just show it. You do not need to use echo $item->fields_by_id[179]->label in explicit field call, because you know the label already.

<div>
    <div>
        <label><strong>Custom label text</strong></label>
    </div>
    <div>
        <?php if(isset($item->fields_by_id[179])): ?>
            <?php echo $item->fields_by_id[179]->result; ?>
        <?php endif; ?>
    </div>
</div>
Powered by Cobalt