From eca67efcd87fa9df82faaade76a674d80d8b671d Mon Sep 17 00:00:00 2001
From: Joao Ventura <joao@venturas.org>
Date: Thu, 12 Nov 2015 17:28:25 +0100
Subject: Issue #2017711 by alansaviolobo, jsbalsera, plopesc, netsensei:
 Create FieldTypePluginManager::getOptions()

---
 .../Drupal/Core/Field/FieldTypePluginManager.php   | 30 ++++++++++++++++++++++
 .../Core/Field/FieldTypePluginManagerInterface.php | 15 +++++++++++
 .../field_ui/src/Form/FieldStorageAddForm.php      |  8 +-----
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
index 3d896b2..e6dcdbd 100644
--- a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
+++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
@@ -32,6 +32,13 @@ class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePl
   protected $typedDataManager;
 
   /**
+   * An array of field type options.
+   *
+   * @var array
+   */
+  protected $fieldTypeOptions;
+
+  /**
    * Constructs the FieldTypePluginManager object
    *
    * @param \Traversable $namespaces
@@ -163,4 +170,27 @@ public function getPluginClass($type) {
     return $plugin_definition['class'];
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getOptions($field_type = NULL) {
+    if (!isset($this->fieldTypeOptions)) {
+      // Gather valid field types.
+      $options = array();
+      $grouped_definitions = $this->getGroupedDefinitions($this->getUiDefinitions());
+      foreach ($grouped_definitions as $category => $field_types) {
+        foreach ($field_types as $name => $type) {
+          $options[$category][$name] = $type['label'];
+        }
+      }
+      $this->fieldTypeOptions = $options;
+    }
+
+    if (isset($field_type)) {
+      return !empty($this->fieldTypeOptions[$field_type]) ? $this->fieldTypeOptions[$field_type] : array();
+    }
+
+    return $this->fieldTypeOptions;
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
index 2c8af73..f824333 100644
--- a/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
+++ b/core/lib/Drupal/Core/Field/FieldTypePluginManagerInterface.php
@@ -100,4 +100,19 @@ public function getUiDefinitions();
    */
   public function getPluginClass($type);
 
+  /**
+   * Returns an array of field type options.
+   *
+   * This will only return fields that can be added in the UI and have a default
+   * widget and formatter.
+   *
+   * @param string $field_type
+   *   A field type category.
+   *
+   * @return array
+   *   Nested array of all field labels keyed by category and field type machine
+   *   name.
+   */
+  public function getOptions($field_type = NULL);
+
 }
diff --git a/core/modules/field_ui/src/Form/FieldStorageAddForm.php b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
index 5f087a6..e4280ae 100644
--- a/core/modules/field_ui/src/Form/FieldStorageAddForm.php
+++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php
@@ -117,13 +117,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
     $this->entityTypeId = $form_state->get('entity_type_id');
     $this->bundle = $form_state->get('bundle');
 
-    // Gather valid field types.
-    $field_type_options = array();
-    foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
-      foreach ($field_types as $name => $field_type) {
-        $field_type_options[$category][$name] = $field_type['label'];
-      }
-    }
+    $field_type_options = $this->fieldTypePluginManager->getOptions();
 
     $form['add'] = array(
       '#type' => 'container',
-- 
2.6.0

