Skip to content
Snippets Groups Projects
Unverified Commit 16edf0a2 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3224530 by phenaproxima, effulgentsia: Pass the media library state object to createAccess()

parent 65fb3829
No related branches found
No related tags found
17 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1896Issue #2940605: Can only intentionally re-render an entity with references 20 times,!1101Issue #2412669 by claudiu.cristea, Julfabre, sidharrell, catch, daffie,...,!1039Issue #2556069 by claudiu.cristea, bnjmnm, lauriii, pfrenssen, Tim Bozeman,...,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!1012Issue #3226887: Hreflang on non-canonical content pages,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links,!594Put each entity type table into a details element on admin/config/regional/content-language,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493,!512Issue #3207771: Menu UI node type form documentation points to non-existent function,!485Sets the autocomplete attribute for username/password input field on login form.,!449Issue #2784233: Allow multiple vocabularies in the taxonomy filter,!231Issue #2671162: summary text wysiwyg patch working fine on 9.2.0-dev,!43Resolve #3173180: Add UI for 'loading' html attribute to images,!30Issue #3182188: Updates composer usage to point at ./vendor/bin/composer
......@@ -285,7 +285,11 @@ protected function buildMediaTypeMenu(MediaLibraryState $state) {
protected function buildMediaTypeAddForm(MediaLibraryState $state) {
$selected_type_id = $state->getSelectedTypeId();
if (!$this->entityTypeManager->getAccessControlHandler('media')->createAccess($selected_type_id)) {
$access_handler = $this->entityTypeManager->getAccessControlHandler('media');
$context = [
'media_library_state' => $state,
];
if (!$access_handler->createAccess($selected_type_id, NULL, $context)) {
return [];
}
......
......@@ -11,6 +11,18 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\media_library_test\Form\TestNodeFormOverride;
/**
* Implements hook_ENTITY_TYPE_create_access().
*/
function media_library_test_media_create_access(AccountInterface $account, array $context, $entity_bundle) {
if (isset($context['media_library_state'])) {
/** @var \Drupal\media_library\MediaLibraryState $state */
$state = $context['media_library_state'];
return AccessResult::forbiddenIf($state->getSelectedTypeId() === 'deny_access');
}
return AccessResult::neutral();
}
/**
* Implements hook_entity_field_access().
*/
......
......@@ -11,6 +11,7 @@
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media_library\MediaLibraryState;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\views\Views;
......@@ -22,6 +23,7 @@
class MediaLibraryAccessTest extends KernelTestBase {
use UserCreationTrait;
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
......@@ -368,6 +370,34 @@ public function testViewAccess() {
$this->assertAccess($access_result, FALSE, 'The media library view does not exist.');
}
/**
* Tests that the media library respects arbitrary access to the add form.
*/
public function testAddFormAccess(): void {
// Access is denied if the media library is trying to create media whose
// type name is 'deny_access'. Also create a second media type that we *can*
// add, so we can be certain that the add form is otherwise visible.
// @see media_library_test_media_create_access()
$media_types = [
$this->createMediaType('image', ['id' => 'deny_access'])->id(),
$this->createMediaType('image')->id(),
];
$account = $this->createUser(['create media']);
$this->setCurrentUser($account);
/** @var \Drupal\media_library\MediaLibraryUiBuilder $ui_builder */
$ui_builder = $this->container->get('media_library.ui_builder');
$state = MediaLibraryState::create('test', $media_types, $media_types[0], 1);
$build = $ui_builder->buildUi($state);
$this->assertEmpty($build['content']['form']);
$state = MediaLibraryState::create('test', $media_types, $media_types[1], 1);
$build = $ui_builder->buildUi($state);
$this->assertNotEmpty($build['content']['form']);
}
/**
* Asserts various aspects of an access result.
*
......
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