Undefined array key "#other_option"

Created on 10 July 2023, over 1 year ago
Updated 14 December 2023, about 1 year ago

Proposed resolution

In 'src/Element/ElementBase.php' line 141, it seems like the array key is now $element['#option']

🐛 Bug report
Status

Needs review

Version

4.1

Component

Code

Created by

🇫🇷France efil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @efil
  • 🇮🇳India Yogesh Kambari

    Hello efil,
    Will you please add steps to reproduce the issue.

  • 🇺🇸United States qqmyers

    FWIW - this may be a backward compatibility issue. I used a prior version of this module w/o issue but the 4.1.0 version gave me the warning above. Looking at the code for my form I had
    $form['QDR']['profile_qdr_institution_type']['#other'] = t('Other (please type a value)');.
    Changing that to
    $form['QDR']['profile_qdr_institution_type']['#other_option'] = t('Other (please type a value)');
    resolved the warning.

    Alternately, changing line 141 to use '#other' (not '#option' as proposed above) also resolved the issue.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    21 pass
  • 🇳🇱Netherlands roderik Amsterdam,NL / Budapest,HU

    This is about using the element in Form API.

    Two things are kind-of distinct:

    1) The issue text (though it's wrong), and comment #3: the fact that there used to be a '#other' value.

    This is from D7 times. (AFAICT it silently disappeared during the rewrite, and it was then added back in How can we change the 'Other' field title or label Fixed .

    People might still be trying to use it, because it was mentioned in the usage example that is linked from the project page. I now edited the page to split it in a D8 and D7 example.

    @qqmyers: AFAICT this means that in previous 4.x version, your "#other" was just ignored, and you got the default value ('-Other-') in 4.0.0.

    2) The issue title: the PHP warning, which appeared in 4.1.0.

    This (code assuming $element['#other_option'] is always set) was added by mistake in #3207231: "Other" option on taxonomy term field should only be available to users with permission , because the issue only dealt with Field API fields and there are no automated tests that properly exercise Form API situations.

    Patch attached. It's easy to fix because the static::addOtherOption() call has a default of '', so we just pass that.

    Also adding something else to the patch: a default NULL for '#default_value' , so that the same PHP warning does not happen if you don't specify '#default_value' in the element definition. I don't see anything wrong with this.

    (This needs a functional test that puts a minimal element definition on a form... to make sure no new warnings get introduced in the future. But without a test... at least the error gets fixed.)

  • 🇨🇷Costa Rica estebanvalerio.h

    Just tested the patch on a project and I can confirm that it worked fine and error was removed

Production build 0.71.5 2024