2

I am making my custom form field types and I would like to make them globally available for all forms everywhere in Joomla.

Is there any certain location that can be used for this purpose, other than libraries/joomla/form/fields, as it is generally considered a bad practice to adding custom files there?

I guess I could manually adding the path in every XML for my custom field, but again it sounds very handy to me if could simply add the fields in a «global scope» like described above.

1
  • 2
    I think the best place to add them would be libraries/yourlibrary/form/field and then use JLoader perhaps to import them. Congrats on the 10k milestone btw :)
    – Lodder
    Commented May 7, 2016 at 14:11

1 Answer 1

1

You can make plugin and use onContentPrepareForm() to add new field to every form possible:

public function onContentPrepareForm($form, $data)
{
    // You can add some filtering here and add fields only to selected components
    if ($form->getName() != 'com_content.article')
    {
        return true;
    }

    // Ensure you're dealing with Form
    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');

        return false;
    }

    // If everything is ok - load form file
    JForm::addFormPath(dirname(__FILE__) . '/myform');
    $form->loadFile('myform', false);

    return true;
}

Data from fields added this way is stored in "attribs" column. The only flaw is component must support onContentPrepareForm event. You can also add some custom data handling using onContentDeforeSave() and onContentAfterSave() functions.

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.