EntityRevisionBuffer.php in GraphQL 8.4
File
src/GraphQL/Buffers/EntityRevisionBuffer.php
View source
<?php
namespace Drupal\graphql\GraphQL\Buffers;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class EntityRevisionBuffer extends BufferBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function add(string $type, $vid) : \Closure {
$item = new \ArrayObject([
'type' => $type,
'vid' => $vid,
]);
return $this
->createBufferResolver($item);
}
protected function getBufferId($item) {
return $item['type'];
}
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));
$storage = $this->entityTypeManager
->getStorage($type);
$entities = $storage
->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);
}
}