Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\media;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Field\FieldTypePluginManagerInterface;
|
Chris@0
|
9 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
10 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
11 use Drupal\Core\Plugin\PluginBase;
|
Chris@0
|
12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Base implementation of media source plugin.
|
Chris@0
|
17 */
|
Chris@0
|
18 abstract class MediaSourceBase extends PluginBase implements MediaSourceInterface, ContainerFactoryPluginInterface {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Plugin label.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var string
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $label;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The entity type manager service.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $entityTypeManager;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The entity field manager service.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $entityFieldManager;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The field type plugin manager service.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $fieldTypeManager;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * The config factory service.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $configFactory;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Constructs a new class instance.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param array $configuration
|
Chris@0
|
59 * A configuration array containing information about the plugin instance.
|
Chris@0
|
60 * @param string $plugin_id
|
Chris@0
|
61 * The plugin_id for the plugin instance.
|
Chris@0
|
62 * @param mixed $plugin_definition
|
Chris@0
|
63 * The plugin implementation definition.
|
Chris@0
|
64 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@0
|
65 * Entity type manager service.
|
Chris@0
|
66 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
|
Chris@0
|
67 * Entity field manager service.
|
Chris@0
|
68 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
|
Chris@0
|
69 * The field type plugin manager service.
|
Chris@0
|
70 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
71 * The config factory service.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory) {
|
Chris@0
|
74 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
75 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
76 $this->entityFieldManager = $entity_field_manager;
|
Chris@0
|
77 $this->fieldTypeManager = $field_type_manager;
|
Chris@0
|
78 $this->configFactory = $config_factory;
|
Chris@0
|
79
|
Chris@0
|
80 // Add the default configuration of the media source to the plugin.
|
Chris@0
|
81 $this->setConfiguration($configuration);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
88 return new static(
|
Chris@0
|
89 $configuration,
|
Chris@0
|
90 $plugin_id,
|
Chris@0
|
91 $plugin_definition,
|
Chris@0
|
92 $container->get('entity_type.manager'),
|
Chris@0
|
93 $container->get('entity_field.manager'),
|
Chris@0
|
94 $container->get('plugin.manager.field.field_type'),
|
Chris@0
|
95 $container->get('config.factory')
|
Chris@0
|
96 );
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * {@inheritdoc}
|
Chris@0
|
101 */
|
Chris@0
|
102 public function setConfiguration(array $configuration) {
|
Chris@0
|
103 $this->configuration = NestedArray::mergeDeep(
|
Chris@0
|
104 $this->defaultConfiguration(),
|
Chris@0
|
105 $configuration
|
Chris@0
|
106 );
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * {@inheritdoc}
|
Chris@0
|
111 */
|
Chris@0
|
112 public function getConfiguration() {
|
Chris@0
|
113 return $this->configuration;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function defaultConfiguration() {
|
Chris@0
|
120 return [
|
Chris@0
|
121 'source_field' => '',
|
Chris@0
|
122 ];
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function getMetadata(MediaInterface $media, $attribute_name) {
|
Chris@0
|
129 switch ($attribute_name) {
|
Chris@0
|
130 case 'default_name':
|
Chris@0
|
131 return 'media:' . $media->bundle() . ':' . $media->uuid();
|
Chris@0
|
132
|
Chris@0
|
133 case 'thumbnail_uri':
|
Chris@0
|
134 $default_thumbnail_filename = $this->pluginDefinition['default_thumbnail_filename'];
|
Chris@0
|
135 return $this->configFactory->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 return NULL;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * {@inheritdoc}
|
Chris@0
|
143 */
|
Chris@0
|
144 public function calculateDependencies() {
|
Chris@0
|
145 return [];
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Get the source field options for the media type form.
|
Chris@0
|
150 *
|
Chris@0
|
151 * This returns all fields related to media entities, filtered by the allowed
|
Chris@0
|
152 * field types in the media source annotation.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @return string[]
|
Chris@0
|
155 * A list of source field options for the media type form.
|
Chris@0
|
156 */
|
Chris@0
|
157 protected function getSourceFieldOptions() {
|
Chris@0
|
158 // If there are existing fields to choose from, allow the user to reuse one.
|
Chris@0
|
159 $options = [];
|
Chris@0
|
160 foreach ($this->entityFieldManager->getFieldStorageDefinitions('media') as $field_name => $field) {
|
Chris@0
|
161 $allowed_type = in_array($field->getType(), $this->pluginDefinition['allowed_field_types'], TRUE);
|
Chris@0
|
162 if ($allowed_type && !$field->isBaseField()) {
|
Chris@0
|
163 $options[$field_name] = $field->getLabel();
|
Chris@0
|
164 }
|
Chris@0
|
165 }
|
Chris@0
|
166 return $options;
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * {@inheritdoc}
|
Chris@0
|
171 */
|
Chris@0
|
172 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
173 $options = $this->getSourceFieldOptions();
|
Chris@0
|
174 $form['source_field'] = [
|
Chris@0
|
175 '#type' => 'select',
|
Chris@0
|
176 '#title' => $this->t('Field with source information'),
|
Chris@0
|
177 '#default_value' => $this->configuration['source_field'],
|
Chris@0
|
178 '#empty_option' => $this->t('- Create -'),
|
Chris@0
|
179 '#options' => $options,
|
Chris@0
|
180 '#description' => $this->t('Select the field that will store essential information about the media item. If "Create" is selected a new field will be automatically created.'),
|
Chris@0
|
181 ];
|
Chris@0
|
182
|
Chris@0
|
183 if (!$options && $form_state->get('operation') === 'add') {
|
Chris@0
|
184 $form['source_field']['#access'] = FALSE;
|
Chris@0
|
185 $field_definition = $this->fieldTypeManager->getDefinition(reset($this->pluginDefinition['allowed_field_types']));
|
Chris@0
|
186 $form['source_field_message'] = [
|
Chris@0
|
187 '#markup' => $this->t('%field_type field will be automatically created on this type to store the essential information about the media item.', [
|
Chris@0
|
188 '%field_type' => $field_definition['label'],
|
Chris@0
|
189 ]),
|
Chris@0
|
190 ];
|
Chris@0
|
191 }
|
Chris@0
|
192 elseif ($form_state->get('operation') === 'edit') {
|
Chris@0
|
193 $form['source_field']['#access'] = FALSE;
|
Chris@0
|
194 $fields = $this->entityFieldManager->getFieldDefinitions('media', $form_state->get('type')->id());
|
Chris@0
|
195 $form['source_field_message'] = [
|
Chris@0
|
196 '#markup' => $this->t('%field_name field is used to store the essential information about the media item.', [
|
Chris@0
|
197 '%field_name' => $fields[$this->configuration['source_field']]->getLabel(),
|
Chris@0
|
198 ]),
|
Chris@0
|
199 ];
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 return $form;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * {@inheritdoc}
|
Chris@0
|
207 */
|
Chris@0
|
208 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * {@inheritdoc}
|
Chris@0
|
213 */
|
Chris@0
|
214 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
215 foreach (array_intersect_key($form_state->getValues(), $this->configuration) as $config_key => $config_value) {
|
Chris@0
|
216 $this->configuration[$config_key] = $config_value;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 // If no source field is explicitly set, create it now.
|
Chris@0
|
220 if (empty($this->configuration['source_field'])) {
|
Chris@0
|
221 $field_storage = $this->createSourceFieldStorage();
|
Chris@0
|
222 $field_storage->save();
|
Chris@0
|
223 $this->configuration['source_field'] = $field_storage->getName();
|
Chris@0
|
224 }
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Creates the source field storage definition.
|
Chris@0
|
229 *
|
Chris@0
|
230 * By default, the first field type listed in the plugin definition's
|
Chris@0
|
231 * allowed_field_types array will be the generated field's type.
|
Chris@0
|
232 *
|
Chris@0
|
233 * @return \Drupal\field\FieldStorageConfigInterface
|
Chris@0
|
234 * The unsaved field storage definition.
|
Chris@0
|
235 */
|
Chris@0
|
236 protected function createSourceFieldStorage() {
|
Chris@0
|
237 return $this->entityTypeManager
|
Chris@0
|
238 ->getStorage('field_storage_config')
|
Chris@0
|
239 ->create([
|
Chris@0
|
240 'entity_type' => 'media',
|
Chris@0
|
241 'field_name' => $this->getSourceFieldName(),
|
Chris@0
|
242 'type' => reset($this->pluginDefinition['allowed_field_types']),
|
Chris@0
|
243 ]);
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 /**
|
Chris@0
|
247 * Returns the source field storage definition.
|
Chris@0
|
248 *
|
Chris@0
|
249 * @return \Drupal\Core\Field\FieldStorageDefinitionInterface|null
|
Chris@0
|
250 * The field storage definition or NULL if it doesn't exists.
|
Chris@0
|
251 */
|
Chris@0
|
252 protected function getSourceFieldStorage() {
|
Chris@0
|
253 // Nothing to do if no source field is configured yet.
|
Chris@0
|
254 $field = $this->configuration['source_field'];
|
Chris@0
|
255 if ($field) {
|
Chris@0
|
256 // Even if we do know the name of the source field, there's no
|
Chris@0
|
257 // guarantee that it exists.
|
Chris@0
|
258 $fields = $this->entityFieldManager->getFieldStorageDefinitions('media');
|
Chris@0
|
259 return isset($fields[$field]) ? $fields[$field] : NULL;
|
Chris@0
|
260 }
|
Chris@0
|
261 return NULL;
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 /**
|
Chris@0
|
265 * {@inheritdoc}
|
Chris@0
|
266 */
|
Chris@0
|
267 public function getSourceFieldDefinition(MediaTypeInterface $type) {
|
Chris@0
|
268 // Nothing to do if no source field is configured yet.
|
Chris@0
|
269 $field = $this->configuration['source_field'];
|
Chris@0
|
270 if ($field) {
|
Chris@0
|
271 // Even if we do know the name of the source field, there is no
|
Chris@0
|
272 // guarantee that it already exists.
|
Chris@0
|
273 $fields = $this->entityFieldManager->getFieldDefinitions('media', $type->id());
|
Chris@0
|
274 return isset($fields[$field]) ? $fields[$field] : NULL;
|
Chris@0
|
275 }
|
Chris@0
|
276 return NULL;
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 /**
|
Chris@0
|
280 * {@inheritdoc}
|
Chris@0
|
281 */
|
Chris@0
|
282 public function createSourceField(MediaTypeInterface $type) {
|
Chris@0
|
283 $storage = $this->getSourceFieldStorage() ?: $this->createSourceFieldStorage();
|
Chris@0
|
284 return $this->entityTypeManager
|
Chris@0
|
285 ->getStorage('field_config')
|
Chris@0
|
286 ->create([
|
Chris@0
|
287 'field_storage' => $storage,
|
Chris@0
|
288 'bundle' => $type->id(),
|
Chris@0
|
289 'label' => $this->pluginDefinition['label'],
|
Chris@0
|
290 'required' => TRUE,
|
Chris@0
|
291 ]);
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 /**
|
Chris@0
|
295 * Determine the name of the source field.
|
Chris@0
|
296 *
|
Chris@0
|
297 * @return string
|
Chris@0
|
298 * The source field name. If one is already stored in configuration, it is
|
Chris@0
|
299 * returned. Otherwise, a new, unused one is generated.
|
Chris@0
|
300 */
|
Chris@0
|
301 protected function getSourceFieldName() {
|
Chris@0
|
302 $base_id = 'field_media_' . $this->getPluginId();
|
Chris@0
|
303 $tries = 0;
|
Chris@0
|
304 $storage = $this->entityTypeManager->getStorage('field_storage_config');
|
Chris@0
|
305
|
Chris@0
|
306 // Iterate at least once, until no field with the generated ID is found.
|
Chris@0
|
307 do {
|
Chris@0
|
308 $id = $base_id;
|
Chris@0
|
309 // If we've tried before, increment and append the suffix.
|
Chris@0
|
310 if ($tries) {
|
Chris@0
|
311 $id .= '_' . $tries;
|
Chris@0
|
312 }
|
Chris@0
|
313 $field = $storage->load('media.' . $id);
|
Chris@0
|
314 $tries++;
|
Chris@0
|
315 } while ($field);
|
Chris@0
|
316
|
Chris@0
|
317 return $id;
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 }
|