class FieldStorageAddController

Controller for building the field type links.

@internal

Hierarchy

Expanded class hierarchy of FieldStorageAddController

1 file declares its use of FieldStorageAddController
RouteSubscriber.php in core/modules/field_ui/src/Routing/RouteSubscriber.php

File

core/modules/field_ui/src/Controller/FieldStorageAddController.php, line 25

Namespace

Drupal\field_ui\Controller
View source
final class FieldStorageAddController extends ControllerBase {
    use AjaxHelperTrait;
    
    /**
     * The name of the entity type.
     *
     * @var string
     */
    protected $entityTypeId;
    
    /**
     * The entity bundle.
     *
     * @var string
     */
    protected $bundle;
    
    /**
     * Constructs a new FieldStorageAddController.
     */
    public function __construct(FieldTypePluginManagerInterface $fieldTypePluginManager, FieldTypeCategoryManagerInterface $fieldTypeCategoryManager, PrivateTempStore $tempStore) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('plugin.manager.field.field_type'), $container->get('plugin.manager.field.field_type_category'), $container->get('tempstore.private')
            ->get('field_ui'));
    }
    
    /**
     * Deletes stored field data and builds the field selection links.
     *
     * @param string $entity_type_id
     *   The name of the entity type.
     * @param string $bundle
     *   The entity bundle.
     * @param string $field_name
     *   The field name.
     *
     * @return array
     *   The field selection links.
     */
    public function resetField(string $entity_type_id, string $bundle, string $field_name) {
        // Delete stored field data in case user changes field type.
        $this->tempStore
            ->delete("{$entity_type_id}:{$field_name}");
        return $this->getFieldSelectionLinks($entity_type_id, $bundle);
    }
    
    /**
     * Builds the field selection links.
     *
     * @param string $entity_type_id
     *   The name of the entity type.
     * @param string $bundle
     *   The entity bundle.
     *
     * @return array
     *   The field selection links.
     */
    public function getFieldSelectionLinks(string $entity_type_id, string $bundle) {
        $build = [];
        $this->entityTypeId = $entity_type_id;
        $this->bundle = $bundle;
        $ui_definitions = $this->fieldTypePluginManager
            ->getEntityTypeUiDefinitions($entity_type_id);
        $field_type_options = $unique_definitions = [];
        $grouped_definitions = $this->fieldTypePluginManager
            ->getGroupedDefinitions($ui_definitions, 'label', 'id');
        $category_definitions = $this->fieldTypeCategoryManager
            ->getDefinitions();
        // Invoke a hook to get category properties.
        foreach ($grouped_definitions as $category => $field_types) {
            foreach ($field_types as $name => $field_type) {
                $unique_definitions[$category][$name] = [
                    'unique_identifier' => $name,
                ] + $field_type;
                if ($this->fieldTypeCategoryManager
                    ->hasDefinition($category)) {
                    $category_plugin = $this->fieldTypeCategoryManager
                        ->createInstance($category, $unique_definitions[$category][$name], $category_definitions[$category]);
                    $field_type_options[$category_plugin->getPluginId()] = [
                        'unique_identifier' => $name,
                    ] + $field_type;
                }
                else {
                    $field_type_options[(string) $field_type['label']] = [
                        'unique_identifier' => $name,
                    ] + $field_type;
                }
            }
        }
        $build['add-label'] = [
            '#type' => 'label',
            '#title' => $this->t('Choose a type of field'),
            '#title_display' => 'before',
            '#required' => TRUE,
        ];
        $build['add'] = [
            '#type' => 'container',
            '#attributes' => [
                'class' => 'add-field-container',
            ],
        ];
        $field_type_options_radios = [];
        foreach ($field_type_options as $id => $field_type) {
            
            /** @var \Drupal\Core\Field\FieldTypeCategoryInterface $category_info */
            $category_info = $this->fieldTypeCategoryManager
                ->createInstance($field_type['category'], $field_type);
            $entity_type = $this->entityTypeManager()
                ->getDefinition($this->entityTypeId);
            $display_as_group = !$category_info instanceof FallbackFieldTypeCategory;
            $route_parameters = [
                'entity_type' => $this->entityTypeId,
                'bundle' => $this->bundle,
                'display_as_group' => $display_as_group ? 'true' : 'false',
                'selected_field_type' => $category_info->getPluginId(),
            ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
            $cleaned_class_name = Html::getClass($field_type['unique_identifier']);
            $field_type_options_radios[$id] = [
                '#type' => 'html_tag',
                '#tag' => 'a',
                '#attributes' => [
                    'class' => [
                        'field-option',
                        'use-ajax',
                    ],
                    'role' => 'button',
                    'tabindex' => '0',
                    'data-dialog-type' => 'modal',
                    'data-dialog-options' => Json::encode([
                        'width' => 1100,
                        'title' => $this->t('Add field: @type', [
                            '@type' => $category_info->getLabel(),
                        ]),
                    ]),
                    'href' => Url::fromRoute("field_ui.field_storage_config_add_sub_{$this->entityTypeId}", $route_parameters)
                        ->toString(),
                ],
                '#weight' => $category_info->getWeight(),
                'thumb' => [
                    '#type' => 'container',
                    '#attributes' => [
                        'class' => [
                            'field-option__thumb',
                        ],
                    ],
                    'icon' => [
                        '#type' => 'container',
                        '#attributes' => [
                            'class' => [
                                'field-option__icon',
                                $display_as_group ? "field-icon-{$field_type['category']}" : "field-icon-{$cleaned_class_name}",
                            ],
                        ],
                    ],
                ],
                // Store some data we later need.
'#data' => [
                    '#group_display' => $display_as_group,
                ],
                'words' => [
                    '#type' => 'container',
                    '#attributes' => [
                        'class' => [
                            'field-option__words',
                        ],
                    ],
                    'label' => [
                        '#attributes' => [
                            'class' => [
                                'field-option__label',
                            ],
                        ],
                        '#type' => 'html_tag',
                        '#tag' => 'span',
                        '#value' => $category_info->getLabel(),
                    ],
                    'description' => [
                        '#type' => 'container',
                        '#attributes' => [
                            'class' => [
                                'field-option__description',
                            ],
                        ],
                        '#markup' => $category_info->getDescription(),
                    ],
                ],
            ];
            if ($libraries = $category_info->getLibraries()) {
                $field_type_options_radios[$id]['#attached']['library'] = $libraries;
            }
        }
        uasort($field_type_options_radios, [
            SortArray::class,
            'sortByWeightProperty',
        ]);
        $build['add']['new_storage_type'] = $field_type_options_radios;
        $build['#attached']['library'][] = 'field_ui/drupal.field_ui';
        $build['#attached']['library'][] = 'field_ui/drupal.field_ui.manage_fields';
        $build['#attached']['library'][] = 'core/drupal.dialog.ajax';
        return $build;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 2
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 1
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 2
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 1
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 1
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
FieldStorageAddController::$bundle protected property The entity bundle.
FieldStorageAddController::$entityTypeId protected property The name of the entity type.
FieldStorageAddController::create public static function Instantiates a new instance of the implementing class using autowiring. Overrides AutowireTrait::create
FieldStorageAddController::getFieldSelectionLinks public function Builds the field selection links.
FieldStorageAddController::resetField public function Deletes stored field data and builds the field selection links.
FieldStorageAddController::__construct public function Constructs a new FieldStorageAddController.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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