Problem/Motivation

This issue is spun off from #3038254-60: Delegate media library access to the "thing" that opened the library.

The MediaLibraryState object is the crucial link in the way the Media Library system is stitched together. It is used for access checking, selection handling, and the construction of the Media Library UI itself. For all these reasons, it makes sense for it to be able to influence caching, but currently it does not carry any caching information.

Proposed resolution

Implement CacheableDependencyInterface, and its accompanying trait, on MediaLibraryState.

Remaining tasks

Do that.

User interface changes

None.

API changes

None, really.

Data model changes

None.

Release notes snippet

Probably none. This is a change to internal plumbing.

Issue fork drupal-3063343

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
1.29 KB

Initial patch.

borisson_’s picture

This look good apart from the missing test coverage, +1.

xjm’s picture

Category: Feature request » Task
Priority: Normal » Major
Status: Needs review » Needs work

This is probably at least a major task. :)

Wim Leers’s picture

Title: Add cacheability to MediaLibraryState » Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context
Priority: Major » Minor
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests
Related issues: +#3038254: Delegate media library access to the "thing" that opened the library
FileSize
2.27 KB
3.63 KB

This was opened in response to @effulgentsia's review at #3038254-60: Delegate media library access to the "thing" that opened the library. Crediting him.

This is not major, and does not affect the cacheability of any response. @effulgentsia merely pointed out that hardcoding some cache context is not semantically ideal, it'd be better for the object that causes that cache context to be added to implement CacheableDependencyInterface so that the hardcoding can be removed.

#2 should've removed the hardcoding, but did not yet. Also, this does not need test coverage. It's a pure refactoring.

phenaproxima’s picture

I know I originally wrote it, but +1 RTBC here. It's absolutely pure refactoring and has pre-existing test coverage.

larowlan’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
@@ -76,7 +69,10 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
+      if ($entity_access instanceof RefinableCacheableDependencyInterface) {
+        $entity_access->addCacheableDependency($state);
+      }
+      return $entity_access;

ick - anyone know if we have an issue to avoid this dance? (unrelated to this patch of course)

Committed a19a380 and pushed to 8.8.x. Thanks!

  • larowlan committed a19a380 on 8.8.x
    Issue #3063343 by Wim Leers, phenaproxima, effulgentsia: Make...
Wim Leers’s picture

ick - anyone know if we have an issue to avoid this dance? (unrelated to this patch of course)

No, we don't, and we can't. The reason: we didn't want to require AccessResultInterface to implement (Refinable)CacheableDependencyInterface. Put differently: we want code to be able to not care about cacheability of access result.

Or, rather: that is what we wanted a few years ago. Perhaps we feel differently about it today.

jibran’s picture

Perhaps we feel differently about it today.

Let's create a follow up to discuss this. I do think we have evolved during this time.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

phenaproxima’s picture

Status: Closed (fixed) » Needs work

Well, folks, this is some advanced issue necromancy.

The changes made in this issue appear to have been reverted by #2969678-63: Integrate Media Library with Content Moderation. I discussed with @seanB in Slack, who wrote that patch, and he suspects that the changes were reverted accidentally. That issue has no discussion about the refactoring done here, or indeed any reference to this issue. The reverted lines are also the kind of changes that committers could have easily mistaken for legitimate, especially since none of the committers involved in #2969678: Integrate Media Library with Content Moderation were involved in this one.

So I think his theory holds water. Big whoops.

Reopening this so that we can restore these changes to 8.9.x and 9.3.x (and ideally 9.2.x), exactly as they were.

phenaproxima’s picture

Removing patches.

phenaproxima’s picture

Version: 8.8.x-dev » 8.9.x-dev
Status: Needs work » Needs review
FileSize
3.38 KB

And, boom. I think this will apply to all target branches, but let's run tests to confirm.

starshaped’s picture

Status: Needs review » Reviewed & tested by the community

LGTM! Flipping to RTBC.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests
+++ b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
@@ -83,7 +76,10 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
-      return $process_result($entity_access);
+      if ($entity_access instanceof RefinableCacheableDependencyInterface) {

@@ -107,7 +103,11 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
+    $access = $entity_access->andIf($field_access);
+    if ($access instanceof RefinableCacheableDependencyInterface) {

When wouldn't this be an instance of RefinableCacheableDepdenencyInterface?

The fact this got reverted by accident indicates it probably should have had a test in the first instance.

Let's add one now to prevent a repeat regression.

larowlan’s picture

Answer to my comment above is in #10

No, we don't, and we can't. The reason: we didn't want to require AccessResultInterface to implement (Refinable)CacheableDependencyInterface. Put differently: we want code to be able to not care about cacheability of access result.

seanB’s picture

The fact this got reverted by accident indicates it probably should have had a test in the first instance.

To be fair, the accidental revert in my patch would have probably reverted any tests as well. I think I forgot to git pull before creating the patch that reverted it, and somehow we all overlooked the changes.

phenaproxima’s picture

To be fair, the accidental revert in my patch would have probably reverted any tests as well.

True, but "loss of test coverage" is something that always raises committer eyebrows, so IMHO @larowlan is correct that having test coverage would have increased its chances of being caught. :)

phenaproxima’s picture

Version: 8.9.x-dev » 9.3.x-dev

phenaproxima’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Wrote test coverage and switched to a merge request.

The thousandth merge request, in fact. 🥳 😎 🤓 There's something poetic about MR #1000 being a 2+ year-old issue that had to be resurrected due to an accidental revert. Maybe it's a good sign.

Anyway...removing the "needs tests" tag. Let's get this in.

seanB’s picture

Status: Needs review » Reviewed & tested by the community

Agreed, test seems pretty straightforward. Back to RTBC!

  • catch committed 9674e3a on 9.3.x
    Issue #3063343 by phenaproxima, Wim Leers, larowlan, seanB, effulgentsia...
catch’s picture

Title: Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context » [backport] Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context

Committed 9674e3a and pushed to 9.3.x. Thanks!

We're in a mini-freeze for 9.2.x a the moment due to the patch release, so leaving RTBC for backport.

8.9.x is security-only now so this won't get backported there.

  • catch committed 31c6513 on 9.2.x
    Issue #3063343 by phenaproxima, Wim Leers, larowlan, seanB, effulgentsia...
catch’s picture

Title: [backport] Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context » Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context
Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Cherry-picked to 9.2.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.