-
Notifications
You must be signed in to change notification settings - Fork 202
Entity revisions data producer #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fubhy
merged 11 commits into
drupal-graphql:8.x-4.x
from
rthideaway:new-data-producers/entity-revisions
Oct 15, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
025335c
Entity revision buffer.
rthideaway b03ee55
Entity revision data producer.
rthideaway 5bf1e03
Add provider info to definition.
rthideaway b64d0e0
Add access checks.
rthideaway a7f7f14
Merge branch '8.x-4.x' into new-data-producers/entity-revisions
fubhy 5236ec0
Merge branch '8.x-4.x' into new-data-producers/entity-revisions
rthideaway 8674441
Fix access check.
rthideaway a3ec861
Merge branch '8.x-4.x' into new-data-producers/entity-revisions
rthideaway 37f094f
Get the language version of entities before access check.
rthideaway cbf9210
Optimize language filtering.
rthideaway d50b0d1
Merge branch '8.x-4.x' into new-data-producers/entity-revisions
fubhy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\graphql\GraphQL\Buffers; | ||
|
|
||
| use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
|
|
||
| /** | ||
| * Entity revision buffer. | ||
| */ | ||
| class EntityRevisionBuffer extends BufferBase { | ||
|
|
||
| /** | ||
| * The entity type manager service. | ||
| * | ||
| * @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
| */ | ||
| protected $entityTypeManager; | ||
|
|
||
| /** | ||
| * EntityBuffer constructor. | ||
| * | ||
| * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
| * The entity type manager service. | ||
| */ | ||
| public function __construct(EntityTypeManagerInterface $entityTypeManager) { | ||
| $this->entityTypeManager = $entityTypeManager; | ||
| } | ||
|
|
||
| /** | ||
| * Add an item to the buffer. | ||
| * | ||
| * @param string $type | ||
| * The entity type of the given entity ids. | ||
| * @param array|int $vid | ||
| * The entity revision id(s) to load. | ||
| * | ||
| * @return \Closure | ||
| * The callback to invoke to load the result for this buffer item. | ||
| */ | ||
| public function add(string $type, $vid): \Closure { | ||
| $item = new \ArrayObject([ | ||
| 'type' => $type, | ||
| 'vid' => $vid, | ||
| ]); | ||
|
|
||
| return $this->createBufferResolver($item); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function getBufferId($item) { | ||
| return $item['type']; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function resolveBufferArray(array $buffer) { | ||
| $type = reset($buffer)['type']; | ||
| $vids = array_map(function (\ArrayObject $item) { | ||
| return (array) $item['vid']; | ||
| }, $buffer); | ||
|
|
||
| $vids = call_user_func_array('array_merge', $vids); | ||
| $vids = array_values(array_unique($vids)); | ||
|
|
||
| // Load the buffered entities. | ||
| $entities = $this->entityTypeManager | ||
| ->getStorage($type) | ||
| ->loadMultipleRevisions($vids); | ||
|
|
||
| return array_map(function ($item) use ($entities) { | ||
| if (is_array($item['vid'])) { | ||
| return array_reduce($item['vid'], function ($carry, $current) use ($entities) { | ||
| if (!empty($entities[$current])) { | ||
| return $carry + [$current => $entities[$current]]; | ||
| } | ||
|
|
||
| return $carry; | ||
| }, []); | ||
| } | ||
|
|
||
| return isset($entities[$item['vid']]) ? $entities[$item['vid']] : NULL; | ||
| }, $buffer); | ||
| } | ||
|
|
||
| } |
211 changes: 211 additions & 0 deletions
211
src/Plugin/GraphQL/DataProducer/Field/EntityReferenceRevisions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Field; | ||
|
|
||
| use Drupal\Core\Cache\RefinableCacheableDependencyInterface; | ||
| 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; | ||
| use Drupal\graphql\GraphQL\Execution\FieldContext; | ||
| use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; | ||
| use Drupal\graphql\GraphQL\Buffers\EntityRevisionBuffer; | ||
| use GraphQL\Deferred; | ||
| use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
|
||
| /** | ||
| * Loads the entity reference revisions. | ||
| * | ||
| * @DataProducer( | ||
| * id = "entity_reference_revisions", | ||
| * name = @Translation("Entity reference revisions"), | ||
| * description = @Translation("Loads entities from an entity reference revisions field."), | ||
| * provider = "entity_reference_revisions", | ||
| * produces = @ContextDefinition("entity", | ||
| * label = @Translation("Entity"), | ||
| * multiple = TRUE | ||
| * ), | ||
| * consumes = { | ||
| * "entity" = @ContextDefinition("entity", | ||
| * label = @Translation("Parent entity") | ||
| * ), | ||
| * "field" = @ContextDefinition("string", | ||
| * label = @Translation("Field name") | ||
| * ), | ||
| * "language" = @ContextDefinition("string", | ||
| * label = @Translation("Language"), | ||
| * multiple = TRUE, | ||
| * required = FALSE | ||
| * ), | ||
| * "bundle" = @ContextDefinition("string", | ||
| * label = @Translation("Entity bundle(s)"), | ||
| * multiple = TRUE, | ||
| * required = FALSE | ||
| * ), | ||
| * "access" = @ContextDefinition("boolean", | ||
| * label = @Translation("Check access"), | ||
| * required = FALSE, | ||
| * default_value = TRUE | ||
| * ), | ||
| * "access_user" = @ContextDefinition("entity:user", | ||
| * label = @Translation("User"), | ||
| * required = FALSE, | ||
| * default_value = NULL | ||
| * ), | ||
| * "access_operation" = @ContextDefinition("string", | ||
| * label = @Translation("Operation"), | ||
| * required = FALSE, | ||
| * default_value = "view" | ||
| * ) | ||
| * } | ||
| * ) | ||
| */ | ||
| class EntityReferenceRevisions extends DataProducerPluginBase implements ContainerFactoryPluginInterface { | ||
|
|
||
| /** | ||
| * The entity type manager service. | ||
| * | ||
| * @var \Drupal\Core\Entity\EntityTypeManager | ||
| */ | ||
| protected $entityTypeManager; | ||
|
|
||
| /** | ||
| * The entity revision buffer service. | ||
| * | ||
| * @var \Drupal\graphql\GraphQL\Buffers\EntityRevisionBuffer | ||
| */ | ||
| protected $entityRevisionBuffer; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * | ||
| * @codeCoverageIgnore | ||
| */ | ||
| public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
| return new static( | ||
| $configuration, | ||
| $plugin_id, | ||
| $plugin_definition, | ||
| $container->get('entity_type.manager'), | ||
| $container->get('graphql.buffer.entity_revision') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * EntityLoad constructor. | ||
| * | ||
| * @param array $configuration | ||
| * The plugin configuration array. | ||
| * @param string $pluginId | ||
| * The plugin id. | ||
| * @param array $pluginDefinition | ||
| * The plugin definition array. | ||
| * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager | ||
| * The entity type manager service. | ||
| * @param \Drupal\graphql\GraphQL\Buffers\EntityRevisionBuffer $entityRevisionBuffer | ||
| * The entity revision buffer service. | ||
| * | ||
| * @codeCoverageIgnore | ||
| */ | ||
| public function __construct( | ||
| array $configuration, | ||
| string $pluginId, | ||
| array $pluginDefinition, | ||
| EntityTypeManager $entityTypeManager, | ||
| EntityRevisionBuffer $entityRevisionBuffer | ||
| ) { | ||
| parent::__construct($configuration, $pluginId, $pluginDefinition); | ||
| $this->entityTypeManager = $entityTypeManager; | ||
| $this->entityRevisionBuffer = $entityRevisionBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves entity reference revisions for a given field of a given entity. | ||
| * | ||
| * May optionally respect the entity bundles and language. | ||
| * | ||
| * @param \Drupal\Core\Entity\EntityInterface $entity | ||
| * The entity. | ||
| * @param string $field | ||
| * The field of a given entity to get entity reference revisions for. | ||
| * @param string|null $language | ||
| * Optional. Language to be respected for retrieved entities. | ||
| * @param array|null $bundles | ||
| * Optional. List of bundles to be respected for retrieved entities. | ||
| * @param bool $access | ||
| * Whether check for access or not. Default is true. | ||
| * @param \Drupal\Core\Session\AccountInterface|null $accessUser | ||
| * User entity to check access for. Default is null. | ||
| * @param string $accessOperation | ||
| * Operation to check access for. Default is view. | ||
| * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context | ||
| * The caching context related to the current field. | ||
| * | ||
| * @return \GraphQL\Deferred|null | ||
| * A promise that will return entities or NULL if there aren't any. | ||
| */ | ||
| public function resolve(EntityInterface $entity, string $field, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context): ?Deferred { | ||
| if (!$entity instanceof FieldableEntityInterface || !$entity->hasField($field)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| $definition = $entity->getFieldDefinition($field); | ||
| if ($definition->getType() !== 'entity_reference_revisions') { | ||
| return NULL; | ||
| } | ||
|
|
||
| $definition = $entity->getFieldDefinition($field); | ||
| $type = $definition->getSetting('target_type'); | ||
| if (($values = $entity->get($field)) && $values instanceof EntityReferenceFieldItemListInterface) { | ||
| $vids = array_map(function ($value) { | ||
| return $value['target_revision_id']; | ||
| }, $values->getValue()); | ||
|
|
||
| $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)) { | ||
fubhy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 $accessResult \Drupal\Core\Access\AccessResultInterface */ | ||
| $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 NULL; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.