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.

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.

- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | CleanShot 2022-12-02 at 16.04.02@2x.png | 118.32 KB | amjad1233 |
| #5 | CleanShot 2022-12-02 at 16.04.48@2x.png | 301.77 KB | amjad1233 |
| #3 | 3324914-warning-undefined-array.patch | 758 bytes | amjad1233 |
Issue fork webform-3324914
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
Comment #3
amjad1233Comment #4
amjad1233Comment #5
amjad1233Comment #9
jrockowitz commented