Problem/Motivation

In some situation options array might be empty. Which leads to
TypeError: Drupal\Core\Form\OptGroup::flattenOptions(): Argument #1 ($array) must be of type array, null given, called in modules/contrib/webform/src/Plugin/WebformElement/OptionsBase.php on line 765 in Drupal\Core\Form\OptGroup::flattenOptions() (line 23 of core/lib/Drupal/Core/Form/OptGroup.php)

Steps to reproduce

- Create a class which extends Select plugin (Drupal\webform\Plugin\WebformElement\Select)
- Override the "form" method to hide the options from webform screen
-

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state): array {
    $form = parent::form($form, $form_state);
    // This removes the Webform select option value / text input.
    $form['options']['options'] = [];
    $form['options']['options_display_container'] = [];
    return $form;
  }

- Why 👆? -> I am overriding the option from my form Element to something like this and don't want users to select or modify the options on front-end.

After the override, the backend will look like this.
Backend Preview Image

Snippet from the class extended Core\Element\Select.

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input !== FALSE) {
      return $input;
    }
    $current_year = date('Y');
    $range = range($current_year, $current_year + 7);
    $options = array_combine($range, $range);
    $element['#options'] = $options;
    $element['#default_value'] = $current_year;
  }

This will render front-end of the form.

Front-end of custom widget

- Add custom element to webform.
- Try to edit it and you will get above error and a warning.

Proposed resolution

- Add check if the value exists
- webform/src/Plugin/WebformElement/OptionsBase.php

Remaining tasks

- Create patch
- Create tests

User interface changes

- None

API changes

- NA

Data model changes

- NA

Issue fork webform-3324914

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

amjad1233 created an issue. See original summary.

amjad1233’s picture

Title: Warning: Undefined array key "#options" » Old style patch
StatusFileSize
new758 bytes
amjad1233’s picture

Title: Old style patch » Warning: Undefined array key "#options" leads to TypeError: Drupal\Core\Form\OptGroup::flattenOptions():
amjad1233’s picture

Issue summary: View changes
StatusFileSize
new301.77 KB
new118.32 KB

  • 22fa2af committed on 6.2.x
    Issue #3324914 by amjad1233: Warning: Undefined array key "#options"...

  • e5baf14 committed on 6.1.x
    Issue #3324914 by amjad1233: Warning: Undefined array key "#options"...

  • e5baf14 committed on 6.x
    Issue #3324914 by amjad1233: Warning: Undefined array key "#options"...
jrockowitz’s picture

Status: Active » Fixed

  • e5baf14 committed on 6.2.x
    Issue #3324914 by amjad1233: Warning: Undefined array key "#options"...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.