Verified Commit e8568a29 authored by Dave Long's avatar Dave Long
Browse files

Issue #3371301 by lauriii, srishtiiee, longwave, smustgrave: Retrieve field...

Issue #3371301 by lauriii, srishtiiee, longwave, smustgrave: Retrieve field type category information in \Drupal\Core\Field\FieldTypePluginManager::getGroupedDefinitions
parent a5cd2d7a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -15,4 +15,5 @@ date_time:
  description: 'Field to store date and time values.'
  weight: -10
general:
  label: 'General'
  class: \Drupal\Core\Field\FallbackFieldTypeCategory
+1 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ protected function getDiscovery(): YamlDiscovery {
   * {@inheritdoc}
   */
  public function getFallbackPluginId($plugin_id, array $configuration = []): string {
    return 'general';
    return FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY;
  }

}
+6 −0
Original line number Diff line number Diff line
@@ -8,4 +8,10 @@
 * Defines an interface for field type category managers.
 */
interface FieldTypeCategoryManagerInterface extends PluginManagerInterface {

  /**
   * Fallback category for field types.
   */
  const FALLBACK_CATEGORY = 'general';

}
+32 −12
Original line number Diff line number Diff line
@@ -18,11 +18,6 @@
 */
class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePluginManagerInterface {

  /**
   * Default category for field types.
   */
  const DEFAULT_CATEGORY = 'general';

  use CategorizingPluginManagerTrait {
    getGroupedDefinitions as protected getGroupedDefinitionsTrait;
  }
@@ -107,11 +102,11 @@ public function processDefinition(&$definition, $plugin_id) {

    if ($definition['category'] instanceof TranslatableMarkup) {
      @trigger_error('Using a translatable string as a category for field type is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3364271', E_USER_DEPRECATED);
      $definition['category'] = static::DEFAULT_CATEGORY;
      $definition['category'] = FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY;
    }
    elseif (empty($definition['category'])) {
      // Ensure that every field type has a category.
      $definition['category'] = static::DEFAULT_CATEGORY;
      $definition['category'] = FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY;
    }
  }

@@ -164,20 +159,45 @@ public function getFieldSettingsSummary(FieldDefinitionInterface $field_definiti
  }

  /**
   * {@inheritdoc}
   * Gets sorted field type definitions grouped by category.
   *
   * In addition to grouping, both categories and its entries are sorted,
   * whereas plugin definitions are sorted by label.
   *
   * @param array[]|null $definitions
   *   (optional) The plugin definitions to group. If omitted, all plugin
   *   definitions are used.
   * @param string $label_key
   *   (optional) The array key to use as the label of the field type.
   * @param string $category_label_key
   *   (optional) The array key to use as the label of the category.
   *
   * @return array[]
   *   Keys are category names, and values are arrays of which the keys are
   *   plugin IDs and the values are plugin definitions.
   */
  public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
  public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label', $category_label_key = 'label') {
    $grouped_categories = $this->getGroupedDefinitionsTrait($definitions, $label_key);
    $category_info = $this->fieldTypeCategoryManager->getDefinitions();

    // Ensure that all the referenced categories exist.
    foreach ($grouped_categories as $group => $definitions) {
      if (!isset($category_info[$group]) && $group !== static::DEFAULT_CATEGORY) {
      if (!isset($category_info[$group])) {
        assert(FALSE, "\"$group\" must be defined in MODULE_NAME.field_type_categories.yml");
        $grouped_categories[static::DEFAULT_CATEGORY] += $definitions;
        if (!isset($grouped_categories[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY])) {
          $grouped_categories[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY] = [];
        }
        $grouped_categories[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY] += $definitions;
        unset($grouped_categories[$group]);
      }
    }

    return $grouped_categories;
    $normalized_grouped_categories = [];
    foreach ($grouped_categories as $group => $definitions) {
      $normalized_grouped_categories[(string) $category_info[$group][$category_label_key]] = $definitions;
    }

    return $normalized_grouped_categories;
  }

  /**
+4 −1
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
    ];

    $field_type_options = $unique_definitions = [];
    $grouped_definitions = $this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions());
    $grouped_definitions = $this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions(), 'label', 'id');
    // Invoke a hook to get category properties.
    foreach ($grouped_definitions as $category => $field_types) {
      foreach ($field_types as $name => $field_type) {
@@ -165,6 +165,9 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
          $category_plugin = $this->fieldTypeCategoryManager->createInstance($category, $unique_definitions[$category][$name]);
          $field_type_options[$category_plugin->getPluginId()] = ['unique_identifier' => $name] + $field_type;
        }
        else {
          $field_type_options[(string) $field_type['label']] = ['unique_identifier' => $name] + $field_type;
        }
      }
    }
    $form['add-label'] = [
Loading