src/EntityEmbedDisplay/EntityEmbedDisplayBase.php | 12 +++++++++++- src/EntityEmbedDisplay/EntityEmbedDisplayInterface.php | 4 ++-- .../FieldFormatterEntityEmbedDisplayBase.php | 9 +++++---- .../EntityEmbedDisplay/ImageFieldFormatter.php | 17 ++++++++++++----- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php b/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php index 314b142..9f1b644 100644 --- a/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php +++ b/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php @@ -3,6 +3,7 @@ namespace Drupal\entity_embed\EntityEmbedDisplay; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; @@ -69,7 +70,9 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac public function access(AccountInterface $account = NULL) { // Check that the plugin's registered entity types matches the current // entity type. - return $this->isValidEntityType(); + return AccessResult::allowedIf($this->isValidEntityType()) + // @see \Drupal\Core\Entity\EntityTypeManager + ->addCacheTags(['entity_types']); } /** @@ -245,6 +248,13 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac /** * Gets the entity from the current context. * + * @todo Where doe sthis come from? The value must come from somewhere, yet + * this does not implement any context-related interfaces. This is an *input*, + * so we need cache contexts and possibly cache tags to reflect where this + * came from. We need that for *everything* that this class does that relies + * on this, plus any of its subclasses. Right now, this is effectively a + * global that breaks cacheability metadata. + * * @return \Drupal\Core\Entity\EntityInterface */ public function getEntityFromContext() { diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayInterface.php b/src/EntityEmbedDisplay/EntityEmbedDisplayInterface.php index 2284444..6e9e6af 100644 --- a/src/EntityEmbedDisplay/EntityEmbedDisplayInterface.php +++ b/src/EntityEmbedDisplay/EntityEmbedDisplayInterface.php @@ -58,8 +58,8 @@ interface EntityEmbedDisplayInterface extends ConfigurablePluginInterface, Plugi * (optional) The user for which to check access, or NULL to check access * for the current user. Defaults to NULL. * - * @return bool - * TRUE if this Entity Embed Display plugin can be used, or FALSE otherwise. + * @return \Drupal\Core\Access\AccessResultInterface + * The access result. */ public function access(AccountInterface $account = NULL); diff --git a/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php b/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php index 79456c2..f681c56 100644 --- a/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php +++ b/src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php @@ -7,6 +7,7 @@ namespace Drupal\entity_embed\EntityEmbedDisplay; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FormatterPluginManager; @@ -112,12 +113,12 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa * {@inheritdoc} */ public function access(AccountInterface $account = NULL) { - if (!parent::access($account)) { - return FALSE; - } + return parent::access($account)->andIf($this->isApplicableFieldFormatter()); + } + protected function isApplicableFieldFormatter() { $definition = $this->formatterPluginManager->getDefinition($this->getDerivativeId()); - return $definition['class']::isApplicable($this->getFieldDefinition()); + return AccessResult::allowedIf($definition['class']::isApplicable($this->getFieldDefinition())); } /** diff --git a/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php index b75fd14..e66c5fc 100644 --- a/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php +++ b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php @@ -7,6 +7,7 @@ namespace Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Field\FormatterPluginManager; use Drupal\Core\Form\FormStateInterface; @@ -85,15 +86,21 @@ class ImageFieldFormatter extends FileFieldFormatter { * {@inheritdoc} */ public function access(AccountInterface $account = NULL) { - if (!parent::access($account)) { - return FALSE; - } + return parent::access($account)->andIf($this->isValidImage()); + } + + protected function isValidImage() { + $access = AccessResult::allowed(); + // @todo needs cacheability metadata for getEntityFromContext. + // @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase::getEntityFromContext() if ($entity = $this->getEntityFromContext()) { - return $this->imageFactory->get($entity->getFileUri())->isValid(); + $access = AccessResult::allowedIf($this->imageFactory->get($entity->getFileUri())->isValid()) + // See the above @todo, this is the best we can do for now. + ->addCacheableDependency($entity); } - return TRUE; + return $access; } /**