Custom Fields in a module with a repeatable field

by Eoin   Last Updated May 26, 2018 16:10 PM

I'm trying to create a module which will display a list of custom fields and allow me to place one or more in that module.

module.xml

    <fields name="params">
        <fieldset name="basic">
            <field name="componentOfField" type="radio" default="0" label="MOD_CUSTOM_FIELDS_IN_A_MODULE_COMPONENT_OF_FIELD_LABEL" description="MOD_CUSTOM_FIELDS_IN_A_MODULE_COMPONENT_OF_FIELD_DESCRIPTION">
                <option value="0">MOD_CUSTOM_FIELDS_IN_A_MODULE_COMPONENT_OF_FIELD_LABEL_ARTICLE</option>
                <option value="1">MOD_CUSTOM_FIELDS_IN_A_MODULE_COMPONENT_OF_FIELD_LABEL_CONTACT</option>
                <option value="2">MOD_CUSTOM_FIELDS_IN_A_MODULE_COMPONENT_OF_FIELD_LABEL_USERS</option>
            </field>
            <field name="repeatableArticleFields"
                   description="MOD_CUSTOM_FIELDS_IN_A_MODULE_OPTION_DESC"
                   type="subform"
                   label="MOD_CUSTOM_FIELDS_IN_A_MODULE_OPTION_LABEL"
                   min="1"
                   max="1000"
                   formsource="/modules/mod_custom_fields_in_a_module/xml/articlesSubform.xml"
                   multiple="true"
                   buttons="add,remove"
                   layout="joomla.form.field.subform.repeatable"
                   showon="componentOfField:0"
                   groupByFieldset="false"/>
...

xml/articleSubform.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <field
            name="customFieldsListArticle"
            label="MOD_CUSTOM_FIELDS_IN_A_MODULE_FIELDS_LIST_LABEL"
            type="sql"
            default="10"
            sql_select="id, title"
            sql_from="#__fields"
            key_field="id"
            value_field="title"
            sql_filter="com_content.article"
            sql_group="title"

            header="MOD_CUSTOM_FIELDS_IN_A_MODULE_FIELDS_LIST_ARTICLE_DEFAULT_TEXT"
    />
</form>

tmpl/default.php

<?php

    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');

    $id = JFactory::getApplication()->input->get('id');

    $model =& JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
    $appParams = JFactory::getApplication()->getParams();
    $model->setState('params', $appParams);
    $item =& $model->getItem($id);
    $jcFields = FieldsHelper::getFields('com_content.article',  $item, True);

    foreach($jcFields as $jcField)
    {
        $jcFields[$jcField->name] = $jcField;
    }

echo "<h2>Field Value is equal to: " . $item->jcFields['test']->value . "</h2>";

This does not echo the field, however, I do see the field in print_r($jcField); so the field exists on that page.

Also, my subform information does not appear in print_r($params); and so I cannot call it. I see the subform working in the back end so I think the XML part is coded correctly.



Related Questions



Joomla custom parameter field for core module

Updated June 22, 2017 09:10 AM