Commit 58d974e9 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 0703c3f8
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1009,6 +1009,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
+4 −1
Original line number Diff line number Diff line
@@ -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');
}

/**
+34 −0
Original line number Diff line number Diff line
@@ -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.');
  }

}