diff --git a/src/Entity/MediaBundle.php b/src/Entity/MediaBundle.php index 0b16cdd..e7d3295 100644 --- a/src/Entity/MediaBundle.php +++ b/src/Entity/MediaBundle.php @@ -255,20 +255,22 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface if (!$update) { // When creating new bundles, add also a source field, if configured to // do so. - if (!$this->isSyncing() && empty($this->getTypeConfiguration()['source_field']) && !empty($this->getTypeConfiguration()['create_source_field'])) { - $media_type_manager = \Drupal::service('plugin.manager.media_entity.type'); - /** @var \Drupal\media_entity\MediaTypeInterface $instance */ - $instance = $media_type_manager->createInstance($this->getType()->getPluginId(), $this->getTypeConfiguration()); - $default_field = $instance->getDefaultSourceField(); + $media_type_manager = \Drupal::service('plugin.manager.media_entity.type'); + /** @var \Drupal\media_entity\MediaTypeInterface $instance */ + $instance = $media_type_manager->createInstance($this->getType()->getPluginId(), $this->getTypeConfiguration()); + $configuration = $instance->getConfiguration(); - if (!empty($default_field['storage'])) { + $source_field_info = $instance->getDefaultSourceField(); + if (!$this->isSyncing() && !empty($source_field_info)) { + + foreach ($source_field_info as $source_key => $field_details) { // Save the field storage and create the instance. - $default_field['storage']->save(); + $field_details['storage']->save(); FieldConfig::create([ 'entity_type' => 'media', - 'field_name' => $default_field['field_name'], - 'label' => $default_field['label'], + 'field_name' => $field_details['field_name'], + 'label' => $field_details['label'], 'required' => TRUE, 'bundle' => $this->id(), ])->save(); @@ -281,8 +283,8 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface 'mode' => 'default', 'status' => TRUE, ]); - $form_display->setComponent($default_field['field_name'], [ - 'type' => $default_field['field_widget'], + $form_display->setComponent($field_details['field_name'], [ + 'type' => $field_details['widget'], ])->save(); // Make the field visible on the media entity itself. @@ -293,13 +295,21 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface 'mode' => 'default', 'status' => TRUE, ]); - $display->setComponent($default_field['field_name'], [ - 'type' => $default_field['field_formatter'], + $display->setComponent($field_details['field_name'], [ + 'type' => $field_details['formatter'], ])->save(); + $configuration[$source_key] = $field_details['field_name']; } + // Save the source field(s) name(s) to the plugin type configuration. + \Drupal::configFactory() + ->getEditable('media_entity.bundle.' . $this->id()) + ->set('type_configuration', $configuration) + ->save(); + } + } } diff --git a/src/MediaTypeInterface.php b/src/MediaTypeInterface.php index 32a49e5..47e6994 100644 --- a/src/MediaTypeInterface.php +++ b/src/MediaTypeInterface.php @@ -88,22 +88,28 @@ interface MediaTypeInterface extends PluginInspectionInterface, ConfigurablePlug /** * Provide a default source field. * - * Plugins defining media bundles are suggested to implement this method and - * create a field that will be used as default source field, on bundle - * creation. Make sure this method returns unsaved entities, once these are - * only going to be saved and used if the user marked the option to use the - * default field provided. + * Plugins defining media bundles are required to implement this method and + * create at least one (unsaved) field that will be used as default source + * field(s), on bundle creation. * * @return array - * An associative array containing the following keys: - * 'storage' => \Drupal\field\Entity\FieldStorageConfig A config entity for - * this field storage. + * An associative array of fields that this plugin considers as source + * fields, keyed by the string used in the configuration form to reference + * this/these field(s). In a typical scenario, the array will contain only + * one value, and this first-level key will likely be "source_field". Other + * plugins could refer to their source fields with keys such as + * "main_source_field" + "secondary_source_field" or anything similar. + * Each value of this array will also be an associative array with the + * following structure: + * 'storage' => \Drupal\field\Entity\FieldStorageConfig An unsaved config + * entity for this field storage. * 'field_name' => (string) The name to be used for the field instance. * 'label' => (string) The label to be used for the field instance. - * 'field_widget' => (string) The id of the widget to use. - * 'field_formatter' => (string) The id of the formatter to use. + * 'widget' => (string) The id of the widget to use. + * 'formatter' => (string) The id of the formatter to use. + * * Note that the storage config entity need to be returned unsaved, and the - * main form will be responsible for saving them. + * main form will be responsible for saving it when processing the submit. */ public function getDefaultSourceField();