Change record status: 
Project: 
Introduced in branch: 
9.3.x
Introduced in version: 
9.3.0
Description: 

As of #3224530: Pass the media library state object to createAccess(), the Media Library module will pass a value object containing its current state (an instance of Drupal\media_library\MediaLibraryState) in hook_ENTITY_TYPE_create_access()'s $context parameter, when determining access to the "add media" section of the media library. This will allow contrib and custom modules to implement complex access logic for the form, for example:

use Drupal\Core\Session\AccountInterface;

function mymodule_media_create_access(AccountInterface $account, array $context, $entity_bundle) {
  if (isset($context['media_library_state'])) {
    // $context['media_library_state'] is an instance of Drupal\media_library\MediaLibraryState. Use it as needed to
    // make an access decision.
  }
}

To be clear, the resulting access decision will only apply to the "add media" section of the media library (i.e., the part that lets the user create new media items), in both the field widget and in CKEditor. It does NOT influence access to the section of the media library that allows selecting existing media, or access to the media library as a whole.

Impacts: 
Module developers