summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2018-01-07 21:40:29 (GMT)
committerxjm2018-01-07 21:40:29 (GMT)
commitee7f7f2a25929f11f4981c50eebfdce58f040ab7 (patch)
treefe057bb8bcaaf6cf02ca580ce40dee444dfdb3db
parent6eafe72da8509397b8541abcd1a181dcd3043ff8 (diff)
Issue #2932226 by robpowell, marcoscano, phenaproxima, Darvanen, larowlan: Media Type entities don't validate machine name properly
-rw-r--r--core/modules/media/src/MediaTypeForm.php23
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php9
2 files changed, 30 insertions, 2 deletions
diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php
index 170a34f..7766af2 100644
--- a/core/modules/media/src/MediaTypeForm.php
+++ b/core/modules/media/src/MediaTypeForm.php
@@ -131,6 +131,13 @@ class MediaTypeForm extends EntityForm {
'#options' => $options,
'#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.
@@ -234,6 +241,18 @@ class MediaTypeForm extends EntityForm {
}
/**
+ * 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.
*
* @return array
@@ -273,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));
}
@@ -296,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));
}
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php
index 652aca8..4f491cc 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php
@@ -53,6 +53,15 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
// Check that the plugin cannot be changed after it is set on type creation.
$assert_session->fieldDisabled('Media source');
$assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
+
+ // Check that a new type with the same machine name cannot be created.
+ $this->drupalGet('admin/structure/media/add');
+ $page->fillField('label', $label);
+ $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
+ $page->selectFieldOption('Media source', 'test');
+ $assert_session->assertWaitOnAjaxRequest();
+ $page->pressButton('Save');
+ $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.');
}
/**