Skip to content
Snippets Groups Projects
Commit b8485e75 authored by catch's avatar catch
Browse files

Issue #3267401 by nils.destoop, acbramley, smustgrave: Media library is...

Issue #3267401 by nils.destoop, acbramley, smustgrave: Media library is showing 'is required message' while the user has no access to the field

(cherry picked from commit f8f6ad83)
parent 190c3118
No related branches found
No related tags found
4 merge requests!8506Draft: Issue #3456536 by ibrahim tameme,!5646Issue #3350972 by nod_: [random test failure]...,!5600Issue #3350972 by nod_: [random test failure]...,!5343Issue #3305066 by quietone, Rename RedirectLeadingSlashesSubscriber
......@@ -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
......
......@@ -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');
}
/**
......
......@@ -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;
......@@ -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.');
}
}
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