905cdb4f1bb1281065a66343d55d187fd5960878
[yaffs-website] / web / core / lib / Drupal / Core / Field / FieldTypePluginManager.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
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;
12
13 /**
14  * Plugin manager for 'field type' plugins.
15  *
16  * @ingroup field_types
17  */
18 class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePluginManagerInterface {
19
20   use CategorizingPluginManagerTrait;
21
22   /**
23    * The typed data manager.
24    *
25    * @var \Drupal\Core\TypedData\TypedDataManagerInterface
26    */
27   protected $typedDataManager;
28
29   /**
30    * Constructs the FieldTypePluginManager object
31    *
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
38    *   The module handler.
39    * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
40    *   The typed data manager.
41    */
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;
47   }
48
49   /**
50    * {@inheritdoc}
51    *
52    * Creates a field item, which is not part of an entity or field item list.
53    *
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.
60    *
61    * @return \Drupal\Core\Field\FieldItemInterface
62    *   The instantiated object.
63    */
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);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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);
75   }
76
77   /**
78    * {@inheritdoc}
79    */
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);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
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';
92     }
93
94     // Ensure that every field type has a category.
95     if (empty($definition['category'])) {
96       $definition['category'] = $this->t('General');
97     }
98   }
99
100   /**
101    * {@inheritdoc}
102    */
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();
108     }
109     return [];
110   }
111
112   /**
113    * {@inheritdoc}
114    */
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();
120     }
121     return [];
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function getUiDefinitions() {
128     $definitions = $this->getDefinitions();
129
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']);
133     });
134
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'],
141           ] + $definition;
142
143           if (isset($option['category'])) {
144             $definitions['field_ui:' . $id . ':' . $key]['category'] = $option['category'];
145           }
146         }
147       }
148     }
149
150     return $definitions;
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function getPluginClass($type) {
157     $plugin_definition = $this->getDefinition($type, FALSE);
158     return $plugin_definition['class'];
159   }
160
161 }