Commit 31c65138 authored by catch's avatar catch
Browse files

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

Issue #3063343 by phenaproxima, Wim Leers, larowlan, seanB, effulgentsia: Make MediaLibraryState implement CacheableDependencyInterface to remove the need for hardcoding a cache context

(cherry picked from commit 9674e3a4)
parent d79d8537
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -41,17 +41,10 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  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 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)

    // 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 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
    }

    $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;
  }

  /**
+24 −1
Original line number Diff line number Diff line
@@ -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 @@
 *
 * @see \Drupal\media_library\MediaLibraryOpenerInterface
 */
class MediaLibraryState extends ParameterBag {
class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface {

  /**
   * {@inheritdoc}
@@ -269,4 +271,25 @@ public function getOpenerParameters() {
    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 [];
  }

}
+7 −0
Original line number Diff line number Diff line
@@ -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 @@ public function testCreate($opener_id, array $allowed_media_type_ids, $selected_
    }
    $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());
  }

  /**