Loading core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php +5 −0 Original line number Diff line number Diff line Loading @@ -1007,6 +1007,11 @@ public static function validateRequired(array $element, FormStateInterface $form return; } // If user has no access, the validation isn't needed. if (isset($element['#access']) && !$element['#access']) { return; } $field_state = static::getFieldState($element, $form_state); // Trigger error if the field is required and no media is present. Although // the Form API's default validation would also catch this, the validation Loading core/modules/media_library/tests/modules/media_library_test/media_library_test.module +4 −1 Original line number Diff line number Diff line Loading @@ -27,7 +27,10 @@ function media_library_test_media_create_access(AccountInterface $account, array * Implements hook_entity_field_access(). */ 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'); $deny_fields = \Drupal::state()->get('media_library_test_entity_field_access_deny_fields', []); // Always deny the field_media_no_access field. $deny_fields[] = 'field_media_no_access'; return AccessResult::forbiddenIf(in_array($field_definition->getName(), $deny_fields, TRUE), 'Field access denied by test module'); } /** Loading core/modules/media_library/tests/src/FunctionalJavascript/WidgetAccessTest.php +34 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Tests\media_library\FunctionalJavascript; use Drupal\field\Entity\FieldConfig; use Drupal\media\Entity\Media; use Drupal\media_library\MediaLibraryState; use Drupal\user\Entity\Role; Loading Loading @@ -136,4 +137,37 @@ public function testWidgetAccess() { $assert_session->responseContains('Access denied'); } /** * Tests the widget with a required field that the user can't access. */ public function testRequiredFieldNoAccess() { // Make field_single_media_type required. $fieldConfig = FieldConfig::loadByName('node', 'basic_page', 'field_single_media_type'); assert($fieldConfig instanceof FieldConfig); $fieldConfig->setRequired(TRUE) ->save(); // Deny access to the field. \Drupal::state()->set('media_library_test_entity_field_access_deny_fields', ['field_single_media_type']); $user = $this->drupalCreateUser([ 'access administration pages', 'access content', 'create basic_page content', 'create type_one media', 'view media', ]); $this->drupalLogin($user); $this->drupalGet('node/add/basic_page'); $this->assertSession()->elementNotExists('css', '.field--name-field-single-media-type'); $this->submitForm([ 'title[0][value]' => $this->randomMachineName(), ], 'Save'); $this->assertSession()->elementNotExists('css', '.messages--error'); $this->assertSession()->pageTextNotContains('Single media type field is required.'); } } Loading
core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php +5 −0 Original line number Diff line number Diff line Loading @@ -1007,6 +1007,11 @@ public static function validateRequired(array $element, FormStateInterface $form return; } // If user has no access, the validation isn't needed. if (isset($element['#access']) && !$element['#access']) { return; } $field_state = static::getFieldState($element, $form_state); // Trigger error if the field is required and no media is present. Although // the Form API's default validation would also catch this, the validation Loading
core/modules/media_library/tests/modules/media_library_test/media_library_test.module +4 −1 Original line number Diff line number Diff line Loading @@ -27,7 +27,10 @@ function media_library_test_media_create_access(AccountInterface $account, array * Implements hook_entity_field_access(). */ 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'); $deny_fields = \Drupal::state()->get('media_library_test_entity_field_access_deny_fields', []); // Always deny the field_media_no_access field. $deny_fields[] = 'field_media_no_access'; return AccessResult::forbiddenIf(in_array($field_definition->getName(), $deny_fields, TRUE), 'Field access denied by test module'); } /** Loading
core/modules/media_library/tests/src/FunctionalJavascript/WidgetAccessTest.php +34 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Tests\media_library\FunctionalJavascript; use Drupal\field\Entity\FieldConfig; use Drupal\media\Entity\Media; use Drupal\media_library\MediaLibraryState; use Drupal\user\Entity\Role; Loading Loading @@ -136,4 +137,37 @@ public function testWidgetAccess() { $assert_session->responseContains('Access denied'); } /** * Tests the widget with a required field that the user can't access. */ public function testRequiredFieldNoAccess() { // Make field_single_media_type required. $fieldConfig = FieldConfig::loadByName('node', 'basic_page', 'field_single_media_type'); assert($fieldConfig instanceof FieldConfig); $fieldConfig->setRequired(TRUE) ->save(); // Deny access to the field. \Drupal::state()->set('media_library_test_entity_field_access_deny_fields', ['field_single_media_type']); $user = $this->drupalCreateUser([ 'access administration pages', 'access content', 'create basic_page content', 'create type_one media', 'view media', ]); $this->drupalLogin($user); $this->drupalGet('node/add/basic_page'); $this->assertSession()->elementNotExists('css', '.field--name-field-single-media-type'); $this->submitForm([ 'title[0][value]' => $this->randomMachineName(), ], 'Save'); $this->assertSession()->elementNotExists('css', '.messages--error'); $this->assertSession()->pageTextNotContains('Single media type field is required.'); } }