Skip to content
Snippets Groups Projects
Verified Commit a19a3806 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3063343 by Wim Leers, phenaproxima, effulgentsia: Make...

Issue #3063343 by Wim Leers, phenaproxima, effulgentsia: Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context
parent 90a21e1c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -38,17 +38,10 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) { ...@@ -38,17 +38,10 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) {
public function checkAccess(MediaLibraryState $state, AccountInterface $account) { public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
$parameters = $state->getOpenerParameters() + ['entity_id' => NULL]; $parameters = $state->getOpenerParameters() + ['entity_id' => NULL];
$process_result = function ($result) {
if ($result instanceof RefinableCacheableDependencyInterface) {
$result->addCacheContexts(['url.query_args']);
}
return $result;
};
// Forbid access if any of the required parameters are missing. // Forbid access if any of the required parameters are missing.
foreach (['entity_type_id', 'bundle', 'field_name'] as $key) { foreach (['entity_type_id', 'bundle', 'field_name'] as $key) {
if (empty($parameters[$key])) { if (empty($parameters[$key])) {
return $process_result(AccessResult::forbidden("$key parameter is missing.")); return AccessResult::forbidden("$key parameter is missing.")->addCacheableDependency($state);
} }
} }
...@@ -76,7 +69,10 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account) ...@@ -76,7 +69,10 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
// If entity-level access is denied, there's no point in continuing. // If entity-level access is denied, there's no point in continuing.
if (!$entity_access->isAllowed()) { if (!$entity_access->isAllowed()) {
return $process_result($entity_access); if ($entity_access instanceof RefinableCacheableDependencyInterface) {
$entity_access->addCacheableDependency($state);
}
return $entity_access;
} }
// If the entity has not been loaded, create it in memory now. // If the entity has not been loaded, create it in memory now.
...@@ -100,7 +96,11 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account) ...@@ -100,7 +96,11 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
} }
$field_access = $access_handler->fieldAccess('edit', $field_definition, $account, $items, TRUE); $field_access = $access_handler->fieldAccess('edit', $field_definition, $account, $items, TRUE);
return $process_result($entity_access->andIf($field_access)); $access = $entity_access->andIf($field_access);
if ($access instanceof RefinableCacheableDependencyInterface) {
$access->addCacheableDependency($state);
}
return $access;
} }
/** /**
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Drupal\media_library; namespace Drupal\media_library;
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -42,7 +44,7 @@ ...@@ -42,7 +44,7 @@
* subject to change in minor releases. External code should not instantiate * subject to change in minor releases. External code should not instantiate
* or extend this class. * or extend this class.
*/ */
class MediaLibraryState extends ParameterBag { class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -274,4 +276,25 @@ public function getOpenerParameters() { ...@@ -274,4 +276,25 @@ public function getOpenerParameters() {
return $this->get('media_library_opener_parameters', []); return $this->get('media_library_opener_parameters', []);
} }
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return ['url.query_args'];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return [];
}
} }
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