class OptGroup

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Form/OptGroup.php \Drupal\Core\Form\OptGroup

Provides helpers for HTML option groups.

Hierarchy

Expanded class hierarchy of OptGroup

6 files declare their use of OptGroup
EntityReferenceItem.php in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
InOperator.php in core/modules/views/src/Plugin/views/filter/InOperator.php
ListItemBase.php in core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
OptGroupTest.php in core/tests/Drupal/Tests/Core/Form/OptGroupTest.php
OptionsDefaultFormatter.php in core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php

... See full list

4 string references to 'OptGroup'
AssertContentTrait::getSelectedItem in core/tests/Drupal/KernelTests/AssertContentTrait.php
Get the selected value from a select field.
EntityReferenceAdminTest::assertFieldSelectOptions in core/modules/field/tests/src/FunctionalJavascript/EntityReference/EntityReferenceAdminTest.php
Checks if a select element contains the specified options.
FormTest::validateSelectSorting in core/modules/system/tests/src/Functional/Form/FormTest.php
Validates that the options are in the right order in a select.
form_select_options in core/includes/form.inc
Converts the options in a select element into a structured array for output.

File

core/lib/Drupal/Core/Form/OptGroup.php, line 8

Namespace

Drupal\Core\Form
View source
class OptGroup {
  
  /**
   * Allows PHP array processing of multiple select options with the same value.
   *
   * Used for form select elements which need to validate HTML option groups
   * and multiple options which may return the same value. Associative PHP
   * arrays cannot handle these structures, since they share a common key.
   *
   * @param array $array
   *   The form options array to process.
   *
   * @return array
   *   An array with all hierarchical elements flattened to a single array.
   */
  public static function flattenOptions(array $array) {
    $options = [];
    static::doFlattenOptions($array, $options);
    return $options;
  }
  
  /**
   * Iterates over an array building a flat array with duplicate keys removed.
   *
   * This function also handles cases where objects are passed as array values.
   *
   * @param array $array
   *   The form options array to process.
   * @param array $options
   *   The array of flattened options.
   */
  protected static function doFlattenOptions(array $array, array &$options) {
    foreach ($array as $key => $value) {
      if (is_object($value) && isset($value->option)) {
        static::doFlattenOptions($value->option, $options);
      }
      elseif (is_array($value)) {
        static::doFlattenOptions($value, $options);
      }
      else {
        $options[$key] = $value;
      }
    }
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.