function WidgetPluginManager::getOptions

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getOptions()
  2. 10 core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getOptions()
  3. 11.x core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getOptions()

Returns an array of widget type options for a field type.

Parameters

string|null $field_type: (optional) The name of a field type, or NULL to retrieve all widget options. Defaults to NULL.

Return value

array If no field type is provided, returns a nested array of all widget types, keyed by field type human name.

File

core/lib/Drupal/Core/Field/WidgetPluginManager.php, line 168

Class

WidgetPluginManager
Plugin type manager for field widgets.

Namespace

Drupal\Core\Field

Code

public function getOptions($field_type = NULL) {
    if (!isset($this->widgetOptions)) {
        $options = [];
        $field_types = $this->fieldTypeManager
            ->getDefinitions();
        $widget_types = $this->getDefinitions();
        uasort($widget_types, [
            'Drupal\\Component\\Utility\\SortArray',
            'sortByWeightElement',
        ]);
        foreach ($widget_types as $name => $widget_type) {
            foreach ($widget_type['field_types'] as $widget_field_type) {
                // Check that the field type exists.
                if (isset($field_types[$widget_field_type])) {
                    $options[$widget_field_type][$name] = $widget_type['label'];
                }
            }
        }
        $this->widgetOptions = $options;
    }
    if (isset($field_type)) {
        return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : [];
    }
    return $this->widgetOptions;
}

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