Issue #2932226 by robpowell, marcoscano, phenaproxima, Darvanen, larowlan: Media Type entities don't validate machine name properly
parent
6eafe72da8
commit
ee7f7f2a25
|
@ -131,6 +131,13 @@ class MediaTypeForm extends EntityForm {
|
||||||
'#options' => $options,
|
'#options' => $options,
|
||||||
'#description' => $source_description,
|
'#description' => $source_description,
|
||||||
'#ajax' => ['callback' => '::ajaxHandlerData'],
|
'#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,
|
'#required' => TRUE,
|
||||||
// Once the media type is created, its source plugin cannot be changed
|
// Once the media type is created, its source plugin cannot be changed
|
||||||
// anymore.
|
// anymore.
|
||||||
|
@ -233,6 +240,18 @@ class MediaTypeForm extends EntityForm {
|
||||||
return $form;
|
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.
|
* Prepares workflow options to be used in the 'checkboxes' form element.
|
||||||
*
|
*
|
||||||
|
@ -273,7 +292,7 @@ class MediaTypeForm extends EntityForm {
|
||||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||||
parent::validateForm($form, $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.
|
// Let the selected plugin validate its settings.
|
||||||
$this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
|
$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']))
|
->setStatus((bool) $form_state->getValue(['options', 'status']))
|
||||||
->setNewRevision((bool) $form_state->getValue(['options', 'new_revision']));
|
->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.
|
// Let the selected plugin save its settings.
|
||||||
$this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
|
$this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,15 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
|
||||||
// Check that the plugin cannot be changed after it is set on type creation.
|
// Check that the plugin cannot be changed after it is set on type creation.
|
||||||
$assert_session->fieldDisabled('Media source');
|
$assert_session->fieldDisabled('Media source');
|
||||||
$assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
|
$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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue