Skip to content
Snippets Groups Projects
Commit ff4351ab authored by Angie Byron's avatar Angie Byron
Browse files

Issue #3094059 by Sam152, seanB, phenaproxima, oknate, 5n00py,...

Issue #3094059 by Sam152, seanB, phenaproxima, oknate, 5n00py, andrewmacpherson, matthieuscarset, dion-jensen: The media library widget "Add media" button incorrectly submits form handlers of forms it is embedded in
parent e669b4e8
No related branches found
No related tags found
6 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards
......@@ -478,7 +478,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Add a button that will load the Media library in a modal using AJAX.
$element['open_button'] = [
'#type' => 'submit',
'#type' => 'button',
'#value' => $this->t('Add media'),
'#name' => $field_name . '-media-library-open-button' . $id_suffix,
'#attributes' => [
......@@ -497,7 +497,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'message' => $this->t('Opening media library.'),
],
],
'#submit' => [],
// Allow the media library to be opened even if there are form errors.
'#limit_validation_errors' => [],
];
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\media_library_test\Form\TestNodeFormOverride;
/**
* Implements hook_entity_field_access().
......@@ -16,3 +17,13 @@
function media_library_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
return AccessResult::forbiddenIf($field_definition->getName() === 'field_media_no_access', 'Field access denied by test module');
}
/**
* Implements hook_entity_type_alter().
*/
function media_library_test_entity_type_alter(array &$entity_types) {
if (isset($entity_types['node'])) {
$entity_types['node']->setFormClass('default', TestNodeFormOverride::class);
$entity_types['node']->setFormClass('edit', TestNodeFormOverride::class);
}
}
<?php
namespace Drupal\media_library_test\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeForm;
/**
* Override NodeForm to test media library form submission semantics.
*/
class TestNodeFormOverride extends NodeForm {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
if (in_array('open_button', $triggering_element['#parents'], TRUE)) {
throw new \Exception('The media library widget open_button element should not trigger form submit.');
}
parent::submitForm($form, $form_state);
}
}
......@@ -480,6 +480,12 @@ public function testWidget() {
// setting for the entity reference field is null. All types should be
// allowed in this case.
$menu = $this->openMediaLibraryForField('field_null_types_media');
// Assert that the button to open the media library does not submit the
// parent form. We can do this by checking if the validation of the parent
// form is not triggered.
$assert_session->pageTextNotContains('Title field is required.');
$this->assertTrue($menu->hasLink('Type One'));
$this->assertTrue($menu->hasLink('Type Two'));
$this->assertTrue($menu->hasLink('Type Three'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment