Problem/Motivation
If anyone other than administrator tries to upload a file from pc to the media library there is an ajax error and the file does not get uploaded.
This is not a problem with missing permissions, I have given the role(s) where upload fails every file and media permission.
Enabled permissions:
Access media overview
Create media
Delete any media
Delete own media
Update any media
Update own media
View media
View own unpublished media
Access the Files overview page
permissions that are not enabled:
Administer media
Administer media types
View all media revisions
It probably has some other origin that I could not yet determine.
If a non admin user clicks on 'Choose file' to upload file from computer to media library, this ajax error is generated:
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /en/media-library?media_library_opener_id=media_library.opener.field_widget&media_library_allowed_types%5Bimage%5D=image&media_library_allowed_types%5Bpdf%5D=pdf&media_library_selected_type=image&media_library_remaining=1&media_library_opener_parameters%5Bfield_widget_id%5D=preview_image&media_library_opener_parameters%5Bentity_type_id%5D=node&media_library_opener_parameters%5Bbundle%5D=product&media_library_opener_parameters%5Bfield_name%5D=preview_image&media_library_opener_parameters%5Bentity_id%5D=234&media_library_opener_parameters%5Brevision_id%5D=428&hash=jmA7Ffzote3dx-RapHgn63nutkpY3UlBso1PjrU9FuI&ajax_form=1
StatusText: OK
ResponseText: Error: Call to a member function getId() on null in Drupal\Core\Entity\EntityAccessControlHandler->access() (line 67 of /app/web/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php).
If I inspect the network calls in Chrome I see a call is made to:
brary_allowed_types%5Bimage%5D=image&media_library_allowed_types%5Bpdf%5D=pdf&media_library_selected_type=image&media_library_remaining=1&media_library_opener_parameters%5Bfield_widget_id%5D=preview_image&media_library_opener_parameters%5Bentity_type_id%5D=node&media_library_opener_parameters%5Bbundle%5D=product&media_library_opener_parameters%5Bfield_name%5D=preview_image&media_library_opener_parameters%5Bentity_id%5D=234&media_library_opener_parameters%5Brevision_id%5D=428&hash=jmA7Ffzote3dx-RapHgn63nutkpY3UlBso1PjrU9FuI&ajax_form=1&_wrapper_format=drupal_ajax
Which returns this response:
Error: Call to a member function getId() on null in Drupal\Core\Entity\EntityAccessControlHandler->access() (line 67 of /app/web/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php).
And indeed, if I debug EntityAccessControlHandler.php I can see that $entity is the correct node that I am editing, but $entity->language() returns null.
Proposed resolution
temporary workaround: provide a default langcode when it is null for whatever reason:
edit core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php
and change
$langcode = $entity->language()->getId();
to
$langcode = is_null($entity->language()) ? \Drupal::languageManager()->getCurrentLanguage()->getId() : $entity->language()->getId();
also edit core/modules/node/src/NodeGrantDatabaseStorage.php
and change
// Check for grants for this node and the correct langcode.
$nids = $query->andConditionGroup()
->condition('nid', $node->id())
->condition('langcode', $node->language()->getId());
to
// Check for grants for this node and the correct langcode.
$langcode = is_null($node->language()) ? \Drupal::languageManager()->getCurrentLanguage()->getId() : $node->language()->getId();
$nids = $query->andConditionGroup()
->condition('nid', $node->id())
->condition('langcode', $langcode);
I post this here so It is easy to find a temporary solution when googling the errors above, but of course, the actual root of the problem must be found (why for non admin users $entity->language() is null when trying to upload file to media library?)
Remaining tasks
I should find a root cause to be certain if this is only a local problem for my project or if other users have the exact same problem sometimes. In case its a local problem because of some of the projects code, add description here what caused it. In case of a general problem in some conditions: create patch first with temporary workaround to keep a project working but more importantly fix the underlying problem which has yet to be determined.