0

I'm lost with utilising com_fields in my component so that my users can create a custom field for a specific purpose and have it rendered in a modal popup.

So I have my component setup with a dashboard and the option for admins to create custom fields and groups, this all works fine and dandy. In the admin side edit.php of an item I can actually see a list of custom fields available that have been created and select one from the list as a reference to the specific item. Now I wish to display/render the chosen field in my modal popup. I assumed I should be able to use the following to render the field like this:

    <?php echo $this->form->renderField('id'); ?>
    <?php echo $this->form->renderField('field_id'); ?>
    <?php echo $this->form->renderField('user_id'); ?>
    <?php if ($this->item->field_id) : ?>
        <?php $fields = FieldsHelper::getFields('com_mycomponent.recordform', $this->item, true); ?>
        <?php echo $this->form->renderField($fields[0]->name); ?>
    <?php endif; ?>

My tmpl file is called modalrec.php and the model being used is recordform.php. I've seen mention of needing to have a plugin to trigger an event which I don't understand. Surely if I have the fields setup (in this case a radio field with 3 options) I can simply get the field and show the options available. But alas I can't see it. Any guidance appreciated.

1 Answer 1

0

Well I think I have it although if anyone else can suggest a better way, please do. So basically I have create xml entry for the field and have an empty fieldset/group in the form xml proper.

        <?php if ($this->item->field_id) : ?>
            <?php $fields = FieldsHelper::getFields('com_mycomponent.motionform', $this->item->field_id, true); ?>
            <?php 
                $options = '';
                foreach ($fields as $fld) {
                    if ($fld->id == $this->item->field_id) {
                        $opts = ArrayHelper::fromObject($fld->fieldparams->get('options'));
                        $fldParams = json_decode($fld->params);
                        foreach ($opts as $opt) {
                            $options .= '<option value="'.$opt['value'].'">'.Text::_($opt['name']).'</option>';
                        }
                        $xmlFld = new SimpleXMLElement(
                            '<field name="'.$fld->name.'" type="'.$fld->type.'" class="'.$fldParams->class.'" label="" description="" >'.$options.'</field>'
                        );
                        $this->form->setField($xmlFld,'custfldmotions',true,'custflds');
                    }
                }
            ?>
            <?php echo $this->form->renderFieldset('custflds'); ?>
        <?php endif; ?>

And in the form xml we have:

<?xml version="1.0" encoding="utf-8"?>
 . . . .
<fieldset name="custflds">
    <fields name="custfldmotions">
    </fields>
</fieldset>
1
  • Tweeked the options collection for only the field I was interested in. Mar 30 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.