3 namespace Drupal\Core\Field;
5 use Drupal\Component\Plugin\Factory\DefaultFactory;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
10 use Drupal\Core\Plugin\DefaultPluginManager;
11 use Drupal\Core\TypedData\TypedDataManagerInterface;
14 * Plugin manager for 'field type' plugins.
16 * @ingroup field_types
18 class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePluginManagerInterface {
20 use CategorizingPluginManagerTrait;
23 * The typed data manager.
25 * @var \Drupal\Core\TypedData\TypedDataManagerInterface
27 protected $typedDataManager;
30 * Constructs the FieldTypePluginManager object
32 * @param \Traversable $namespaces
33 * An object that implements \Traversable which contains the root paths
34 * keyed by the corresponding namespace to look for plugin implementations.
35 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
36 * Cache backend instance to use.
37 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
39 * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
40 * The typed data manager.
42 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager) {
43 parent::__construct('Plugin/Field/FieldType', $namespaces, $module_handler, 'Drupal\Core\Field\FieldItemInterface', 'Drupal\Core\Field\Annotation\FieldType');
44 $this->alterInfo('field_info');
45 $this->setCacheBackend($cache_backend, 'field_types_plugins');
46 $this->typedDataManager = $typed_data_manager;
52 * Creates a field item, which is not part of an entity or field item list.
54 * @param string $field_type
55 * The field type, for which a field item should be created.
56 * @param array $configuration
57 * The plugin configuration array, i.e. an array with the following keys:
58 * - field_definition: The field definition object, i.e. an instance of
59 * Drupal\Core\Field\FieldDefinitionInterface.
61 * @return \Drupal\Core\Field\FieldItemInterface
62 * The instantiated object.
64 public function createInstance($field_type, array $configuration = []) {
65 $configuration['data_definition'] = $configuration['field_definition']->getItemDefinition();
66 return $this->typedDataManager->createInstance("field_item:$field_type", $configuration);
72 public function createFieldItemList(FieldableEntityInterface $entity, $field_name, $values = NULL) {
73 // Leverage prototyping of the Typed Data API for fast instantiation.
74 return $this->typedDataManager->getPropertyInstance($entity->getTypedData(), $field_name, $values);
80 public function createFieldItem(FieldItemListInterface $items, $index, $values = NULL) {
81 // Leverage prototyping of the Typed Data API for fast instantiation.
82 return $this->typedDataManager->getPropertyInstance($items, $index, $values);
88 public function processDefinition(&$definition, $plugin_id) {
89 parent::processDefinition($definition, $plugin_id);
90 if (!isset($definition['list_class'])) {
91 $definition['list_class'] = '\Drupal\Core\Field\FieldItemList';
94 // Ensure that every field type has a category.
95 if (empty($definition['category'])) {
96 $definition['category'] = $this->t('General');
103 public function getDefaultStorageSettings($type) {
104 $plugin_definition = $this->getDefinition($type, FALSE);
105 if (!empty($plugin_definition['class'])) {
106 $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
107 return $plugin_class::defaultStorageSettings();
115 public function getDefaultFieldSettings($type) {
116 $plugin_definition = $this->getDefinition($type, FALSE);
117 if (!empty($plugin_definition['class'])) {
118 $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
119 return $plugin_class::defaultFieldSettings();
127 public function getUiDefinitions() {
128 $definitions = $this->getDefinitions();
130 // Filter out definitions that can not be configured in Field UI.
131 $definitions = array_filter($definitions, function ($definition) {
132 return empty($definition['no_ui']);
135 // Add preconfigured definitions.
136 foreach ($definitions as $id => $definition) {
137 if (is_subclass_of($definition['class'], '\Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface')) {
138 foreach ($definition['class']::getPreconfiguredOptions() as $key => $option) {
139 $definitions['field_ui:' . $id . ':' . $key] = [
140 'label' => $option['label'],
143 if (isset($option['category'])) {
144 $definitions['field_ui:' . $id . ':' . $key]['category'] = $option['category'];
156 public function getPluginClass($type) {
157 $plugin_definition = $this->getDefinition($type, FALSE);
158 return $plugin_definition['class'];