Problem/Motivation
Entity configuration:
- Install Inline Entity Form
- Create a media entity bundle "Media Test"
- Add a media reference field to a content type to reference "Media Test" only
- Select "Create referenced entities if they don't already exist"
- Under "Manage form display" set the widget to "Inline entity form - Simple"
User configuration:
- Add a new user
- Set relevant permissions for creating the content type and entity type
- Set relevant permissions for viewing unpublished and editing the entity types
- Do not select "administer media"
Log in as the new user and try to create a new page with reference media. The following error is displayed:
This entity (media: media_test) cannot be referenced.
See Drupal\media\Plugin\EntityReferenceSelection\MediaSelection:
/**
* {@inheritdoc}
*/
public function validateReferenceableNewEntities(array $entities) {
$entities = parent::validateReferenceableNewEntities($entities);
// Mirror the conditions checked in buildEntityQuery().
if (!$this->currentUser->hasPermission('administer media')) {
$entities = array_filter($entities, function ($media) {
/** @var \Drupal\media\MediaInterface $media */
return $media->isPublished();
});
}
return $entities;
}
Giving the user the permission "administer media" allows the node and referenced media entity to be created. I think it would be preferable for this permission to not be required.
Proposed resolution
The logic in validateReferenceableNewEntities is matched to the logic in buildEntityQuery - which does not allow the user access to see unpublished media that they should have access to.
In this case the user has access to their own media through the permission "view own unpublished media" - or they may have access to all unpublished media through the permission "view all unpublished content" provided by content moderation.
A similar issue exists for the node selection handler #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it that proposes to introduce an additional check for view all unpublished content and the equivalent view own unpublished content permission. The solution for the media selection handler should follow the solution for the node selection handler. I believe this should follow the same backwards comparability handling and therefore makes sense to postpone this until the node issue lands.
https://www.drupal.org/project/drupal/issues/2845144#comment-15387486 offers a good summary of the BC need.
Remaining tasks
Alter the build query and validate referenceable new entities logic to account for the additional permissions that grant the user access to unpublished mediaExpand test coverage for these permissions- Include the handling from #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it which will introduce the backwards compatibility setting on the base handler.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3110677
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
imclean commentedI've enabled the permission "view unpublished media" but this check doesn't take it into account:
Comment #3
imclean commentedComment #4
imclean commentedComment #6
abrahame commentedHave the same issue in my current project so I created a patch which works for me.
Comment #7
abrahame commentedComment #11
j-barnes commented@AbrahamE - Working great for me, thanks for the patch!
Comment #12
joachim commentedI think it would be better if this method kept the current layout of having just one return.
Also, the second check to the admin permission isn't necessary.
Won't we still need this if the current user doesn't have the permission to view an unpublished entity they own?
Should say 'media' not 'content', as content means nodes.
Do we need to handle the case where the user doesn't have the permission?
Comment #13
akram khanCreated patch against 9.4.x and address comment #12
Comment #14
akram khanUpdating patch #12 as custom command failed and attaching the reroll file
Comment #17
daniel korteI’m changing this to a Bug Report since I would expect this behavior from the
view own unpublished mediapermission, but let me know if I’m wrong here.I cleaned things up and addressed comment #12 concerns. Also, I noticed the existing patches don’t apply to the Media Library Grid/Table Widget which also has this issue. I updated the Views config to mirror what is being used on the Media Library admin page with the
media_statusplugin. Everything appears to be working with theview own unpublished mediapermission now.Comment #18
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #19
daniel korteComment #21
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #22
daniel korteTry again...
Comment #23
smustgrave commentedDon't think this matches the solution in the MR
As a bug will need a failing test case showing the issue.
Comment #24
jessmm commentedThis appears to not only be limited to referencing a users own unpublished media. We have also experienced users not being able to reference unpublished media from other users. In this case, the user has the permissions to 'create media' and 'update any media'.
Comment #25
ericgsmith commentedComment #26
ericgsmith commentedComment #27
ericgsmith commentedRelated issue #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it is talking along similar lines but for content, not media. To me this feels like a shared issue around unpublished entities being hidden from users who have access to them.
Comment #28
ericgsmith commentedI think similar to the related issue - this check should also take the permission
view any unpublished contentinto account since view any unpublished content also applies to mediaPerhaps this is realistically postponed until #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it lands so that the BC impact can be the same, once that config in the related issue is added it can be used here>
Comment #29
ericgsmith commentedI have started expanding test coverage for the selection handler - end of the day for me so assigning to me as I intent to continue working on this tomorrow, still need find where the validate new references is tested.
Added additional permission check mentioned in #28 - have not added this to the validate method yet - that is still to be done.
I still believe this will need to wait for #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it to land as that introduces a BC layer for this logic change to the base handler - I think it makes sense to wait for that issue to land rather than trying to work that in now and likely this could be postponed as a follow up - but I will try complete the remaining work soon as I need this functionality now and do not have a concern about it being a BC break.
Comment #30
ericgsmith commentedI have expanded some tests - from a quick look I couldn't find existing tests covering the new entities behaviour - the validation constraint looked to be the appropriate place to add that.
Once #2845144: Users without 'bypass node access' permission can't reference unpublished content even if they have access to it lands with the
include_unpublished_entitiessetting on the base handler that can be incorporated here. Setting to postponed based on that.