Commit ee7f7f2a authored by xjm's avatar xjm

Issue #2932226 by robpowell, marcoscano, phenaproxima, Darvanen, larowlan:...

Issue #2932226 by robpowell, marcoscano, phenaproxima, Darvanen, larowlan: Media Type entities don't validate machine name properly
parent 6eafe72d
......@@ -131,6 +131,13 @@ public function form(array $form, FormStateInterface $form_state) {
'#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.
......@@ -233,6 +240,18 @@ public function form(array $form, FormStateInterface $form_state) {
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.
*
......@@ -273,7 +292,7 @@ protected function getSourceSubFormState(array $form, FormStateInterface $form_s
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 @@ function ($item) {
->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));
}
......
......@@ -53,6 +53,15 @@ public function testMediaTypeCreationFormWithDefaultField() {
// 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.');
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment