Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5695453
Load translated referenced entities before array_filter and abstract …
drupov Feb 11, 2020
cabd2ff
docs(custom): Fix typo in custom.md (#963)
Kwadz Feb 17, 2020
7bb6567
Add note about debugging produers. (#970)
dasjo Feb 22, 2020
147a3c3
Added a page to document how to use image data. (#969)
dasjo Feb 22, 2020
cb9c748
Add graphiql-explorer.(#971)
signalwerk Feb 22, 2020
176a154
Fix undefined index error in form. (#947)
Mrjamy Feb 22, 2020
df9a8e0
Add a "defaultValue" to the resolver builder. (#941)
pmelab Feb 22, 2020
5e25f7c
Refactor entity buffer resolver so that duplicate entity ids are not …
justin-longbottom Feb 22, 2020
ad926b1
Allow to get field name from field context. (#929)
rthideaway Feb 22, 2020
ccd05f2
Accept EntityTypeManagerInterface in TaxonomyLoadTree (#951)
Kingdutch Feb 22, 2020
16b0d24
Fix TaxonomyLoadTree::resolve return type hint. (#953)
Kingdutch Feb 22, 2020
42c5ee8
Entity definition data producers. (#860)
rthideaway Feb 22, 2020
b666606
Added entity reference layout revisions plugin. (#968)
dasjo Feb 22, 2020
6127915
chore(travis): PHP 7.4 is not failing anymore on Drupal core
klausi Mar 2, 2020
ea212aa
Merge branch '8.x-4.x' into fix-entity_reference-language
fubhy Mar 4, 2020
2f7989b
Add missing import for AccountInterface in EntityReference data producer
drupov Mar 6, 2020
5722c37
Rename helper method for getting referenced entities and make it prot…
drupov Mar 6, 2020
ef5aa44
Add comments and abstractions in EntityReferenceTrait
drupov Mar 6, 2020
18f43d2
Merge branch '8.x-4.x' into fix-entity_reference-language
drupov Apr 1, 2020
638ae5a
Merge branch '8.x-4.x' into fix-entity_reference-language
drupov Feb 9, 2021
82ec653
fix(data-producer): Use trait logic for EntityReferenceLayoutRevision…
drupov Feb 9, 2021
27fdcad
fix(data-producer): Add trait usage to EntityReferenceRevisions and E…
drupov Feb 9, 2021
c969e25
fix(data-producer): Fix tests
drupov Feb 9, 2021
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 @@ -64,6 +63,8 @@
*/
class EntityReferenceLayoutRevisions extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

use EntityReferenceTrait;

/**
* The entity type manager service.
*
Expand Down Expand Up @@ -166,42 +167,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
40 changes: 3 additions & 37 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 @@ -64,6 +63,8 @@
*/
class EntityReferenceRevisions extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

use EntityReferenceTrait;

/**
* The entity type manager service.
*
Expand Down Expand Up @@ -166,42 +167,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;
}

}