? ? Failure

User tests: Successful: Unsuccessful:

avatar izharaazmi
izharaazmi
10 May 2017

Currently we are already able to add remove form fields from JForm, update field attributes in the preprocessForm triggers. One thing that has always bothered me is the inability to add new options the the list type form fields.

What I propose here might not be the best solution, however this can be a good point to start with.


Summary of Changes

Allow adding a new list option to JForm fields.
Allow additional attributes to the option element created.
Allow adding <optgroup> groups in the list options. (will be used only if form field class cares for)

Testing Instructions

  • Create a new JForm instance with a list type form field.
  • Call $form->addFieldOption('fieldname', 'value', 'Option Text'); on the JForm instance.
  • See the output list options.

Expected result

The new option should be visible in the list dropdown.

Documentation Changes Required

Update JForm documentation.

avatar izharaazmi izharaazmi - open - 10 May 2017
avatar izharaazmi izharaazmi - change - 10 May 2017
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 10 May 2017
Category Libraries Unit Tests
avatar laoneo
laoneo - comment - 10 May 2017

Just wondering, JFormFieldList has an addOption function, would that not work?

avatar izharaazmi
izharaazmi - comment - 11 May 2017

Since 3.7. Great. I didn't notice it.

Now I see that the syntax by this PR is more straight and consistent with the $form->setFieldAttribute() etc. and also offers more features.

This one may still be considered IMO.

avatar laoneo
laoneo - comment - 11 May 2017

I would then probably make the existing function as feature reich as your one. IMO we should not have duplicate ways of adding options.

avatar izharaazmi
izharaazmi - comment - 11 May 2017

About the duplicate, I completely agree.

Though adding option should not be limited to JFormFieldList. See \JFormFieldText::getOptions()

I'd rather suggest modifying existing method to call this one in turn so as to not B/C failure, and also avoid code duplication.

avatar laoneo
laoneo - comment - 11 May 2017

Guess it is worth a try.

avatar Bakual
Bakual - comment - 11 May 2017

FormFieldText::getOptions()

That one is a different usecase than the one in JFormFieldList. The "options" in the text formfield are used for the datalist/suggestions. I wouldn't try to mix them as they support different attributes.

Adding an addFieldOption to the JForm class isn't wise as it would be useless for most formfields (only list types (checkboxes, radios, ... all extend that type) support oprions). I'd rather extend the existing JFormFieldList::addoption() with your proposed functionality. And if you really want you can add a similar method to JFormFieldText.

avatar izharaazmi
izharaazmi - comment - 11 May 2017

JFormFieldText method getSuggestions() was deprecated to make things
consistent. If these two are so much different in nature why did we do that?

avatar Bakual
Bakual - comment - 11 May 2017

JFormFieldText method getSuggestions() was deprecated to make things
consistent.

The initial reason for #3588 was a codestyle issue which couldn't be solved otherwise. When choosing a new method name I just took the one we use in other places. The method names are now consistent (which imho makes sense) but what it actually does will differ as datalist options and select (or radio and checkbox) options don't support the same attributes.

avatar izharaazmi izharaazmi - change - 11 May 2017
Labels Added: ? ?
avatar izharaazmi
izharaazmi - comment - 11 May 2017

I've made the changes. Please review.

avatar Bakual
Bakual - comment - 11 May 2017

I think the JFormFieldList method looks fine.
I don't like much the method in JForm. Why do you want that? One could as well just fetch the field and add it from there. The form method doesn't do anything else than some checks around
$this->getField($name, $group)->addOption($text, $value, $attributes, $optGroup);

avatar izharaazmi
izharaazmi - comment - 11 May 2017

Ok. Just one problem how do we know whether the $field->addOption is
callable. How do we avoid the overhead of checking it all the time in the
calling code.

avatar Bakual
Bakual - comment - 11 May 2017

Ok. Just one problem how do we know whether the $field->addOption is
callable. How do we avoid the overhead of checking it all the time in the
calling code.

I suppose in your extension code, you know which field you want to add some dynamic options. You will only call it for fields that allow them.

Of course if you're talking about a CCK then you don't know that, but then I would expect you to do those checks in your extension.

avatar izharaazmi
izharaazmi - comment - 13 May 2017

@Bakual Done!

avatar Gavakshi
Gavakshi - comment - 14 May 2017

I have tested this item successfully on 7853cce


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/15943.

avatar Gavakshi Gavakshi - test_item - 14 May 2017 - Tested successfully
avatar izharaazmi izharaazmi - change - 21 Aug 2021
Labels Added: ? ?
Removed: ? ?
avatar laoneo
laoneo - comment - 18 Mar 2022

Sorry that it has taken so long to respond to this pull request. It is a nice addition and we would like to reconsider it for Joomla 4.2. Since this change will throw a warning in PHP 8 for subclasses overriding this function, it can't be accepted in the current stage. One solution would be to make a new function and deprecate the old one. We are closing it in the meantime. When ready please reopen again.

avatar laoneo laoneo - close - 18 Mar 2022
avatar laoneo laoneo - change - 18 Mar 2022
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2022-03-18 07:53:59
Closed_By laoneo
Labels Added: ?
Removed: ?

Add a Comment

Login with GitHub to post a comment