EntityUuidBuffer.php in GraphQL 8.4
File
src/GraphQL/Buffers/EntityUuidBuffer.php
View source
<?php
namespace Drupal\graphql\GraphQL\Buffers;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class EntityUuidBuffer extends BufferBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function add($type, $uuid) {
$item = new \ArrayObject([
'type' => $type,
'uuid' => $uuid,
]);
return $this
->createBufferResolver($item);
}
protected function getBufferId($item) {
return $item['type'];
}
public function resolveBufferArray(array $buffer) {
$type = reset($buffer)['type'];
$entityType = $this->entityTypeManager
->getDefinition($type);
if (!($uuid_key = $entityType
->getKey('uuid'))) {
throw new EntityStorageException("Entity type {$type} does not support UUIDs.");
}
$uuids = array_map(function (\ArrayObject $item) {
return (array) $item['uuid'];
}, $buffer);
$uuids = call_user_func_array('array_merge', $uuids);
$uuids = array_values(array_unique($uuids));
$entities = $this->entityTypeManager
->getStorage($type)
->loadByProperties([
$uuid_key => $uuids,
]);
$entities = array_reduce(array_map(function (EntityInterface $entity) {
return [
$entity
->uuid() => $entity,
];
}, $entities), 'array_merge', []);
return array_map(function ($item) use ($entities) {
if (is_array($item['uuid'])) {
return array_reduce($item['uuid'], function ($carry, $current) use ($entities) {
if (!empty($entities[$current])) {
return $carry + [
$current => $entities[$current],
];
}
return $carry;
}, []);
}
return isset($entities[$item['uuid']]) ? $entities[$item['uuid']] : NULL;
}, $buffer);
}
}
Classes
Name |
Description |
EntityUuidBuffer |
Collects entity UUIDs per entity type and loads them all at once in the end. |