/**
* Form controller for media type forms.
+ *
+ * @internal
*/
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) {
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.
*
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));
}
->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));
}
// 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();
}
}