diff --git a/media_entity.module b/media_entity.module index d54f773..9b157e5 100644 --- a/media_entity.module +++ b/media_entity.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\media_entity\Entity\MediaBundle; /** * Implements hook_help(). diff --git a/src/Entity/MediaBundle.php b/src/Entity/MediaBundle.php index 8833f82..277f53a 100644 --- a/src/Entity/MediaBundle.php +++ b/src/Entity/MediaBundle.php @@ -2,12 +2,14 @@ namespace Drupal\media_entity\Entity; -use Drupal\Core\Entity\EntityDescriptionInterface; use Drupal\Core\Config\Entity\ConfigEntityBundleBase; +use Drupal\Core\Entity\EntityDescriptionInterface; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityWithPluginCollectionInterface; use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; use Drupal\media_entity\MediaBundleInterface; use Drupal\media_entity\MediaInterface; +use Drupal\field\Entity\FieldConfig; /** * Defines the Media bundle configuration entity. @@ -44,6 +46,7 @@ use Drupal\media_entity\MediaInterface; * "type_configuration", * "field_map", * "status", + * "create_source_field", * }, * links = { * "add-form" = "/admin/structure/media/add", @@ -121,7 +124,7 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface /** * Default status of this media bundle. * - * @var array + * @var bool */ public $status = TRUE; @@ -241,4 +244,48 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface $this->new_revision = $new_revision; } + /** + * {@inheritdoc} + */ + public function postSave(EntityStorageInterface $storage, $update = TRUE) { + parent::postSave($storage, $update); + + 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(); + + if (!empty($default_field['storage'])) { + // Save the field storage and create the instance. + $default_field['storage']->save(); + FieldConfig::create([ + 'entity_type' => 'media', + 'field_name' => $default_field['field_name'], + 'label' => $default_field['label'], + 'required' => TRUE, + 'bundle' => $this->id(), + ])->save(); + + // Make the field visible on the form display. + $form_display = entity_get_form_display('media', $this->id(), 'default'); + $form_display->setComponent($default_field['field_name'], [ + 'type' => $default_field['field_widget'], + ])->save(); + + // Make the field visible on the media entity itself. + $display = entity_get_display('media', $this->id(), 'default'); + $display->setComponent($default_field['field_name'], [ + 'type' => $default_field['field_formatter'], + ])->save(); + } + + } + } + + } + } diff --git a/src/MediaBundleForm.php b/src/MediaBundleForm.php index 36365f3..6ae83bb 100644 --- a/src/MediaBundleForm.php +++ b/src/MediaBundleForm.php @@ -313,6 +313,14 @@ class MediaBundleForm extends EntityForm { $plugin_configuration = !empty($this->configurableInstances[$plugin]['plugin_config']) ? $this->configurableInstances[$plugin]['plugin_config'] : []; $instance = $this->mediaTypeManager->createInstance($plugin, $plugin_configuration); $instance->submitConfigurationForm($form, $form_state); + + // Save the flag to auto-create a source field. + $this->entity->create_source_field = $form_state->getValue([ + 'type_configuration', + $plugin, + 'create_source_field', + ]); + } /** diff --git a/src/MediaTypeBase.php b/src/MediaTypeBase.php index a472e7d..0041851 100644 --- a/src/MediaTypeBase.php +++ b/src/MediaTypeBase.php @@ -3,14 +3,14 @@ namespace Drupal\media_entity; use Drupal\Component\Plugin\PluginBase; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Config\Config; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Component\Utility\NestedArray; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\Form\FormStateInterface; /** * Base implementation of media type plugin. @@ -158,4 +158,11 @@ abstract class MediaTypeBase extends PluginBase implements MediaTypeInterface, C return 'media:' . $media->bundle() . ':' . $media->uuid(); } + /** + * {@inheritdoc} + */ + public function getDefaultSourceField() { + return []; + } + } diff --git a/src/MediaTypeInterface.php b/src/MediaTypeInterface.php index 2a171c2..32a49e5 100644 --- a/src/MediaTypeInterface.php +++ b/src/MediaTypeInterface.php @@ -2,8 +2,8 @@ namespace Drupal\media_entity; -use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface; +use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; /** @@ -85,4 +85,26 @@ interface MediaTypeInterface extends PluginInspectionInterface, ConfigurablePlug */ public function getDefaultName(MediaInterface $media); + /** + * 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. + * + * @return array + * An associative array containing the following keys: + * 'storage' => \Drupal\field\Entity\FieldStorageConfig A 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. + * Note that the storage config entity need to be returned unsaved, and the + * main form will be responsible for saving them. + */ + public function getDefaultSourceField(); + }