diff --git a/media_entity.module b/media_entity.module index f06bc00..9b157e5 100644 --- a/media_entity.module +++ b/media_entity.module @@ -6,7 +6,6 @@ */ use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\field\Entity\FieldConfig; use Drupal\media_entity\Entity\MediaBundle; /** @@ -84,36 +83,3 @@ function media_entity_copy_icons($source, $destination) { } } } - -/** - * Implements hook_ENTITY_TYPE_insert(). - */ -function media_entity_media_bundle_insert(MediaBundle $media_bundle) { - // When creating new bundles, add also a source field, if configured to do so. - if (!$media_bundle->isSyncing() && empty($media_bundle->getTypeConfiguration()['source_field']) && $media_bundle->getCreateSourceField()) { - $media_type_manager = \Drupal::service('plugin.manager.media_entity.type'); - $instance = $media_type_manager->createInstance($media_bundle->getType()->getPluginId(), $media_bundle->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' => $media_bundle->id(), - ])->save(); - // Make the field visible on the form display. - $form_display = entity_get_form_display('media', $media_bundle->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', $media_bundle->id(), 'default'); - $display->setComponent($default_field['field_name'], [ - 'type' => $default_field['field_formatter'], - ])->save(); - } - } -} diff --git a/src/Entity/MediaBundle.php b/src/Entity/MediaBundle.php index 5227d50..277f53a 100644 --- a/src/Entity/MediaBundle.php +++ b/src/Entity/MediaBundle.php @@ -4,10 +4,12 @@ namespace Drupal\media_entity\Entity; 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. @@ -127,13 +129,6 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface public $status = TRUE; /** - * A flag indicating if we should create a source field upon save. - * - * @var bool - */ - public $create_source_field = TRUE; - - /** * {@inheritdoc} */ public function id() { @@ -238,22 +233,59 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface /** * {@inheritdoc} */ - public function getCreateSourceField() { - return $this->create_source_field; + public function shouldCreateNewRevision() { + return $this->new_revision; } /** * {@inheritdoc} */ - public function shouldCreateNewRevision() { - return $this->new_revision; + public function setNewRevision($new_revision) { + $this->new_revision = $new_revision; } /** * {@inheritdoc} */ - public function setNewRevision($new_revision) { - $this->new_revision = $new_revision; + 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 c11c83e..6ae83bb 100644 --- a/src/MediaBundleForm.php +++ b/src/MediaBundleForm.php @@ -170,22 +170,6 @@ class MediaBundleForm extends EntityForm { $form['type_configuration'][$plugin->getPluginId()] += $instance->buildConfigurationForm([], $form_state); } - // Add a checkbox to allow the field being created automattically on save. - if (!empty($form['type_configuration'][$plugin->getPluginId()]['source_field']) && empty($plugin_configuration['source_field'])) { - $has_source_field_options = !empty($form['type_configuration'][$plugin->getPluginId()]['source_field']['#options']); - $form['type_configuration'][$plugin->getPluginId()]['create_source_field'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Create source field automatically when saving this form.'), - '#description' => $this->t('If checked, a default field will be created and used as a source field. You can change this setting later on.'), - '#default_value' => $bundle->getCreateSourceField(), - // Try to place this checkbox close to the sourcefield select element. - '#weight' => !empty($form['type_configuration'][$plugin->getPluginId()]['source_field']['#weight']) ? $form['type_configuration'][$plugin->getPluginId()]['source_field']['#weight'] + 0.01 : -2, - '#access' => !$has_source_field_options, - ]; - // If there are no options, the dropdown is meaningless. - $form['type_configuration'][$plugin->getPluginId()]['source_field']['#access'] = $has_source_field_options || !$bundle->getCreateSourceField(); - } - // Field mapping configuration. $form['field_mapping'] = [ '#type' => 'fieldset', diff --git a/src/MediaBundleInterface.php b/src/MediaBundleInterface.php index 6bc850a..97e02f9 100644 --- a/src/MediaBundleInterface.php +++ b/src/MediaBundleInterface.php @@ -89,14 +89,6 @@ interface MediaBundleInterface extends ConfigEntityInterface, RevisionableEntity public function getStatus(); /** - * Returns the flag that indicate if a source field should be auto-created. - * - * @return bool - * TRUE if the field should be created when saving the bundle. - */ - public function getCreateSourceField(); - - /** * Sets whether a new revision should be created by default. * * @param bool $new_revision diff --git a/src/MediaTypeInterface.php b/src/MediaTypeInterface.php index 9e84257..32a49e5 100644 --- a/src/MediaTypeInterface.php +++ b/src/MediaTypeInterface.php @@ -96,7 +96,7 @@ interface MediaTypeInterface extends PluginInspectionInterface, ConfigurablePlug * * @return array * An associative array containing the following keys: - * 'config' => \Drupal\field\Entity\FieldStorageConfig A config entity for + * '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.