You are here

public function FormatterPluginManager::getOptions in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/FormatterPluginManager.php \Drupal\Core\Field\FormatterPluginManager::getOptions()

Returns an array of formatter options for a field type.

Parameters

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

Return value

array If no field type is provided, returns a nested array of all formatters, keyed by field type.

File

core/lib/Drupal/Core/Field/FormatterPluginManager.php, line 169

Class

FormatterPluginManager
Plugin type manager for field formatters.

Namespace

Drupal\Core\Field

Code

public function getOptions($field_type = NULL) {
  if (!isset($this->formatterOptions)) {
    $options = [];
    $field_types = $this->fieldTypeManager
      ->getDefinitions();
    $formatter_types = $this
      ->getDefinitions();
    uasort($formatter_types, [
      'Drupal\\Component\\Utility\\SortArray',
      'sortByWeightElement',
    ]);
    foreach ($formatter_types as $name => $formatter_type) {
      foreach ($formatter_type['field_types'] as $formatter_field_type) {

        // Check that the field type exists.
        if (isset($field_types[$formatter_field_type])) {
          $options[$formatter_field_type][$name] = $formatter_type['label'];
        }
      }
    }
    $this->formatterOptions = $options;
  }
  if ($field_type) {
    return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : [];
  }
  return $this->formatterOptions;
}