Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 3 additions & 36 deletions src/Plugin/GraphQL/DataProducer/Field/EntityReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
Expand Down Expand Up @@ -63,6 +62,8 @@
*/
class EntityReference extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

use EntityReferenceTrait;

/**
* The entity type manager service.
*
Expand Down Expand Up @@ -161,41 +162,7 @@ public function resolve(EntityInterface $entity, $field, ?string $language, ?arr

$resolver = $this->entityBuffer->add($type, $ids);
return new Deferred(function () use ($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context) {
$entities = $resolver() ?: [];
$entities = array_filter($entities, function (EntityInterface $entity) use ($language, $bundles, $access, $accessOperation, $accessUser, $context) {
if (isset($bundles) && !in_array($entity->bundle(), $bundles)) {
return FALSE;
}

// Get the correct translation.
if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) {
$entity = $entity->getTranslation($language);
$entity->addCacheContexts(["static:language:{$language}"]);
}

// Check if the passed user (or current user if none is passed) has
// access to the entity, if not return NULL.
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return FALSE;
}
}

return TRUE;
});

if (empty($entities)) {
$type = $this->entityTypeManager->getDefinition($type);
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$tags = $type->getListCacheTags();
$context->addCacheTags($tags);
return NULL;
}

return $entities;
return $this->getReferencedEntities($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
Expand Down Expand Up @@ -36,7 +35,6 @@
* ),
* "language" = @ContextDefinition("string",
* label = @Translation("Language"),
* multiple = TRUE,
* required = FALSE
* ),
* "bundle" = @ContextDefinition("string",
Expand Down Expand Up @@ -64,6 +62,8 @@
*/
class EntityReferenceLayoutRevisions extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

use EntityReferenceTrait;

/**
* The entity type manager service.
*
Expand Down Expand Up @@ -166,42 +166,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag

$resolver = $this->entityRevisionBuffer->add($type, $vids);
return new Deferred(function () use ($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context) {
$entities = $resolver() ?: [];

$entities = array_filter($entities, function (EntityInterface $entity) use ($language, $bundles, $access, $accessOperation, $accessUser, $context) {
if (isset($bundles) && !in_array($entity->bundle(), $bundles)) {
return FALSE;
}

// Get the correct translation.
if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) {
$entity = $entity->getTranslation($language);
$entity->addCacheContexts(["static:language:{$language}"]);
}

// Check if the passed user (or current user if none is passed) has
// access to the entity, if not return NULL.
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return FALSE;
}
}

return TRUE;
});

if (empty($entities)) {
$type = $this->entityTypeManager->getDefinition($type);
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$tags = $type->getListCacheTags();
$context->addCacheTags($tags);
return NULL;
}

return $entities;
return $this->getReferencedEntities($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context);
});
}

Expand Down
41 changes: 3 additions & 38 deletions src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
Expand Down Expand Up @@ -36,7 +35,6 @@
* ),
* "language" = @ContextDefinition("string",
* label = @Translation("Language"),
* multiple = TRUE,
* required = FALSE
* ),
* "bundle" = @ContextDefinition("string",
Expand Down Expand Up @@ -64,6 +62,8 @@
*/
class EntityReferenceRevisions extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

use EntityReferenceTrait;

/**
* The entity type manager service.
*
Expand Down Expand Up @@ -166,42 +166,7 @@ public function resolve(EntityInterface $entity, string $field, ?string $languag

$resolver = $this->entityRevisionBuffer->add($type, $vids);
return new Deferred(function () use ($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context) {
$entities = $resolver() ?: [];

$entities = array_filter($entities, function (EntityInterface $entity) use ($language, $bundles, $access, $accessOperation, $accessUser, $context) {
if (isset($bundles) && !in_array($entity->bundle(), $bundles)) {
return FALSE;
}

// Get the correct translation.
if (isset($language) && $language != $entity->language()->getId() && $entity instanceof TranslatableInterface) {
$entity = $entity->getTranslation($language);
$entity->addCacheContexts(["static:language:{$language}"]);
}

// Check if the passed user (or current user if none is passed) has
// access to the entity, if not return NULL.
if ($access) {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return FALSE;
}
}

return TRUE;
});

if (empty($entities)) {
$type = $this->entityTypeManager->getDefinition($type);
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$tags = $type->getListCacheTags();
$context->addCacheTags($tags);
return NULL;
}

return $entities;
return $this->getReferencedEntities($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context);
});
}

Expand Down
98 changes: 98 additions & 0 deletions src/Plugin/GraphQL/DataProducer/Field/EntityReferenceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Field;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\TranslatableInterface;

trait EntityReferenceTrait {

/**
* Get referenced entities my checking language and access.
*
* @param string $type
* @param string|null $language
* @param array|null $bundles
* @param bool $access
* @param \Drupal\Core\Session\AccountInterface|NULL $accessUser
* @param string $accessOperation
* @param \Closure $resolver
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return array|null
*/
protected function getReferencedEntities($type, $language, $bundles, $access, $accessUser, $accessOperation, $resolver, $context) {
$entities = $resolver() ?: [];

$entities = $this->getTranslated($entities, $language);
$entities = $this->filterAccessible($entities, $bundles, $access, $accessUser, $accessOperation, $context);

if (empty($entities)) {
$type = $this->entityTypeManager->getDefinition($type);
/** @var \Drupal\Core\Entity\EntityTypeInterface $type */
$tags = $type->getListCacheTags();
$context->addCacheTags($tags);
return NULL;
}

return $entities;
}

/**
* Get the referenced entities in the language of the referencer.
*
* @param array $entities
* @param string $language
*
* @return array
*/
private function getTranslated($entities, $language) {
if ($language) {
$entities = array_map(function (EntityInterface $entity) use ($language) {
if ($language !== $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->hasTranslation($language)) {
$entity = $entity->getTranslation($language);
}

$entity->addCacheContexts(["static:language:{$language}"]);
return $entity;
}, $entities);
}

return $entities;
}

/**
* Filter out not accessible entities.
*
* @param array $entities
* @param array|null $bundles
* @param bool $access
* @param \Drupal\Core\Session\AccountInterface|NULL $accessUser
* @param string $accessOperation
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
*
* @return array
*/
private function filterAccessible($entities, $bundles, $access, $accessUser, $accessOperation, $context) {
$entities = array_filter($entities, function (EntityInterface $entity) use ($bundles, $access, $accessOperation, $accessUser, $context) {
if (isset($bundles) && !in_array($entity->bundle(), $bundles)) {
return FALSE;
}

// Check if the passed user (or current user if none is passed) has
// access to the entity, if not return NULL.
if ($access) {
/* @var $accessResult \Drupal\Core\Access\AccessResultInterface */
$accessResult = $entity->access($accessOperation, $accessUser, TRUE);
$context->addCacheableDependency($accessResult);
if (!$accessResult->isAllowed()) {
return FALSE;
}
}
return TRUE;
});

return $entities;
}

}