Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / media / src / MediaTypeForm.php
index 80f7d211617dfa2a87a64bd381f80d9650116a89..7766af2e567d63be3dd65acd453ecaf0cd21d6ba 100644 (file)
@@ -15,6 +15,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Form controller for media type forms.
+ *
+ * @internal
  */
 class MediaTypeForm extends EntityForm {
 
@@ -116,14 +118,30 @@ class MediaTypeForm extends EntityForm {
       '#attributes' => ['id' => 'source-dependent'],
     ];
 
+    if ($source) {
+      $source_description = $this->t('<em>The media source cannot be changed after the media type is created.</em>');
+    }
+    else {
+      $source_description = $this->t('Media source that is responsible for additional logic related to this media type.');
+    }
     $form['source_dependent']['source'] = [
       '#type' => 'select',
       '#title' => $this->t('Media source'),
       '#default_value' => $source ? $source->getPluginId() : NULL,
       '#options' => $options,
-      '#description' => $this->t('Media source that is responsible for additional logic related to this media type.'),
+      '#description' => $source_description,
       '#ajax' => ['callback' => '::ajaxHandlerData'],
+      // Rebuilding the form as part of the AJAX request is a workaround to
+      // enforce machine_name validation.
+      // @todo This was added as part of #2932226 and it should be removed once
+      //   https://www.drupal.org/project/drupal/issues/2557299 solves it in a
+      //   more generic way.
+      '#executes_submit_callback' => TRUE,
+      '#submit' => [[static::class, 'rebuildSubmit']],
       '#required' => TRUE,
+      // Once the media type is created, its source plugin cannot be changed
+      // anymore.
+      '#disabled' => !empty($source),
     ];
 
     if (!$source) {
@@ -222,6 +240,18 @@ class MediaTypeForm extends EntityForm {
     return $form;
   }
 
+  /**
+   * Form submission handler to rebuild the form on select submit.
+   *
+   * @param array $form
+   *   Full form array.
+   * @param \Drupal\Core\Form\FormStateInterface $form_state
+   *   Current form state.
+   */
+  public static function rebuildSubmit(array &$form, FormStateInterface $form_state) {
+    $form_state->setRebuild();
+  }
+
   /**
    * Prepares workflow options to be used in the 'checkboxes' form element.
    *
@@ -262,7 +292,7 @@ class MediaTypeForm extends EntityForm {
   public function validateForm(array &$form, FormStateInterface $form_state) {
     parent::validateForm($form, $form_state);
 
-    if ($form['source_dependent']['source_configuration']) {
+    if (isset($form['source_dependent']['source_configuration'])) {
       // Let the selected plugin validate its settings.
       $this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
     }
@@ -285,7 +315,7 @@ class MediaTypeForm extends EntityForm {
       ->setStatus((bool) $form_state->getValue(['options', 'status']))
       ->setNewRevision((bool) $form_state->getValue(['options', 'new_revision']));
 
-    if ($form['source_dependent']['source_configuration']) {
+    if (isset($form['source_dependent']['source_configuration'])) {
       // Let the selected plugin save its settings.
       $this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
     }
@@ -325,30 +355,19 @@ class MediaTypeForm extends EntityForm {
 
       // Add the new field to the default form and view displays for this
       // media type.
-      $field_name = $source_field->getName();
-      $field_type = $source_field->getType();
-
       if ($source_field->isDisplayConfigurable('form')) {
-        // Use the default widget and settings.
-        $component = \Drupal::service('plugin.manager.field.widget')
-          ->prepareConfiguration($field_type, []);
-
         // @todo Replace entity_get_form_display() when #2367933 is done.
         // https://www.drupal.org/node/2872159.
-        entity_get_form_display('media', $media_type->id(), 'default')
-          ->setComponent($field_name, $component)
-          ->save();
+        $display = entity_get_form_display('media', $media_type->id(), 'default');
+        $source->prepareFormDisplay($media_type, $display);
+        $display->save();
       }
       if ($source_field->isDisplayConfigurable('view')) {
-        // Use the default formatter and settings.
-        $component = \Drupal::service('plugin.manager.field.formatter')
-          ->prepareConfiguration($field_type, []);
-
         // @todo Replace entity_get_display() when #2367933 is done.
         // https://www.drupal.org/node/2872159.
-        entity_get_display('media', $media_type->id(), 'default')
-          ->setComponent($field_name, $component)
-          ->save();
+        $display = entity_get_display('media', $media_type->id(), 'default');
+        $source->prepareViewDisplay($media_type, $display);
+        $display->save();
       }
     }