klox7 VIP
Total posts: 914
24 Dec 2013 04:37

I read this article about custom templates . I noticed there is $item->fields_by_group. Does this mean I can display in record a group of fields (with labels, results,...) not just single field? I tried something but it didn't work.

Last Modified: 02 Mar 2014


Sergey
Total posts: 13,748
24 Dec 2013 05:16

It means that there fields_by_group is 2 levels array. First level is groups and second is fields. Every frou nas list of the fields.

This array is used if you what to display fields grouped by groups.

foreach($item->$item->fields_by_group AS $group_name => $fields)

{

echo '

'.$group_name.'

'

foreach ($fields as $field_id => $field)

{

echo $field->result;

}

}

You can group with tabs, sliders or anything else.


klox7 VIP
Total posts: 914
26 Dec 2013 06:08

So if I will like to display one group and all fields in it do I have to use group id or group name? Ex. $item->fields_by_group[group_id]?


Sergey
Total posts: 13,748
27 Dec 2013 00:36

Group name.


klox7 VIP
Total posts: 914
27 Dec 2013 07:56

If anyone wants to output group of fields in custom template here is the solution that works for me. In the 3rd line word 'your_group_name' is hardcoded. So if you change your group name in administrator it won't change here. If somebody knows how to solve this then by my guest.

<?php 

        if(isset($this->item->fields_by_groups['your_group_name'])):?>

            <?php group_start($this, 'your_group_name', 'tab-'.$i++);?>

            <dl class="dl-horizontal fields-list">

                <?php foreach ($this->item->fields_by_groups['your_group_name'] as $field_id => $field):?>

                    <dt id="<?php echo 'dt-'.$field_id; ?>" class="<?php echo $field->fieldclass;?>">

                        <?php if($field->params->get('core.show_lable') > 1):?>

                            <label id="<?php echo $field->id;?>-lbl">

                                <?php echo $field->label; ?>

                                <?php if($field->params->get('core.icon')):?>

                                    <?php echo HTMLFormatHelper::icon($field->params->get('core.icon'));  ?>

                                <?php endif;?>

                            </label>

                            <?php if($field->params->get('core.label_break') > 1):?>

                            <?php endif;?>

                        <?php endif;?>

                    </dt>

                    <dd id="<?php echo 'dd-'.$field_id; ?>" class="<?php echo $field->fieldclass;?><?php echo ($field->params->get('core.label_break') > 1 ? ' line-brk' : NULL) ?>">

                        <?php echo $field->result; ?>

                    </dd>

                <?php endforeach;?>

            </dl>

            <?php unset($this->item->fields_by_groups['your_group_name']);?>

        <?php endif;?>

Sergey
Total posts: 13,748
30 Dec 2013 06:50

Why group names and not IDs? Because when I list fields I need group names to create blocks/tabs but I absolutely do not need group IDs. If I use IDs I also have to add one query to get group names by IDs. Few queries there and few queries there and Cobalt will be as slow as any other CCK. That is not good. For performance sake I used names.


Sergey
Total posts: 13,748
06 Jan 2014 00:22

Am I right with my thoughts about using language constants?

Absolutely.

Powered by Cobalt