1

I made a custom field and using it in a subform looking somewhat like this:

<config>
    <fields name="params">
        <fieldset name="basic" addfieldpath="/administrator/components/com_mycomp/models/fields">
            <field name="base_url" type="url" required="true" default="" label="base_url" />
            <field name="examplefield" type="text" required="true" default="" label="examplefield" />

            <field name="items" type="subform" multiple="true" min="1" layout="joomla.form.field.subform.repeatable-table" buttons="add,remove" groupByFieldset="false" label="items">
                <form>
                    <fieldset name="section1" label="Section1">
                        <field name="item" label="item" type="myfield" />
                    </fieldset>
                </form>
            </field>
        </fieldset>
    </fields>
</config>

The custom field "myfield" needs to access the value from the base_url field in the parent form:

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('list');

class JFormFieldMyfield extends JFormFieldList
{
    protected $type = 'Myfield';

    public function getOptions() {

        // need to access base_url field value

        // not working with subform
        // $base_url = $this->form->getFieldset('basic', 'jform')['jform_params_base_url']->value;

Before i made a subform out of this i could do it like this:

$base_url = $this->form->getFieldset('basic', 'jform')['jform_params_base_url']->value;

Whats the best way to access values of fields in the parent form?

2 Answers 2

0

I don't think there's a proper way of doing this without having to subclass the subform field and possibly the form class as well. But if your field is "one use only", i.e. you only mean it to be used in a specific form for your own purposes, you could try using Joomla\CMS\Form\Form::getInstance() with the name of the form.

use Joomla\CMS\Form\Form;

// Get the form instance. Replace com_plugins.plugin with the name of your form.
$parentForm = Form::getInstance('com_plugins.plugin');

Get the field value.
$baseUrl = $parentForm->getValue('base_url', 'params');
1
  • That didn't work for me. Found a solution i will post.
    – Mike
    Commented Feb 23, 2021 at 11:29
0

This is the way it worked for me:

$plugin = JPluginHelper::getPlugin('system', 'myplugin');
$params = new JRegistry($plugin->params);
$baseUrl = $params->get('base_url', '');

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.