Issue #3063343 by phenaproxima, Wim Leers, larowlan, seanB, effulgentsia: Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context
parent
a0a22f8d41
commit
9674e3a448
|
@ -41,17 +41,10 @@ class MediaLibraryFieldWidgetOpener implements MediaLibraryOpenerInterface {
|
|||
public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
|
||||
$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.
|
||||
foreach (['entity_type_id', 'bundle', 'field_name'] as $key) {
|
||||
if (empty($parameters[$key])) {
|
||||
return $process_result(AccessResult::forbidden("$key parameter is missing."));
|
||||
return AccessResult::forbidden("$key parameter is missing.")->addCacheableDependency($state);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,7 +76,10 @@ class MediaLibraryFieldWidgetOpener implements MediaLibraryOpenerInterface {
|
|||
|
||||
// If entity-level access is denied, there's no point in continuing.
|
||||
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.
|
||||
|
@ -107,7 +103,11 @@ class MediaLibraryFieldWidgetOpener implements MediaLibraryOpenerInterface {
|
|||
}
|
||||
|
||||
$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 @@
|
|||
namespace Drupal\media_library;
|
||||
|
||||
use Drupal\Component\Utility\Crypt;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
@ -37,7 +39,7 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|||
*
|
||||
* @see \Drupal\media_library\MediaLibraryOpenerInterface
|
||||
*/
|
||||
class MediaLibraryState extends ParameterBag {
|
||||
class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -269,4 +271,25 @@ class MediaLibraryState extends ParameterBag {
|
|||
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 [];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\Tests\media_library\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\media_library\MediaLibraryState;
|
||||
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
|
||||
|
@ -104,6 +106,11 @@ class MediaLibraryStateTest extends KernelTestBase {
|
|||
}
|
||||
$state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_type_id, $remaining_slots);
|
||||
$this->assertInstanceOf(MediaLibraryState::class, $state);
|
||||
|
||||
// Ensure that the state object carries cache metadata.
|
||||
$this->assertInstanceOf(CacheableDependencyInterface::class, $state);
|
||||
$this->assertSame(['url.query_args'], $state->getCacheContexts());
|
||||
$this->assertSame(Cache::PERMANENT, $state->getCacheMaxAge());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue