You are here

public function EntityRevisionBuffer::resolveBufferArray in GraphQL 8.4

Resolve the buffer as an array.

Simplifies sub-class implementations by concealing the object storage details of the buffer object.

Parameters

array $buffer: The buffer as an array.

Return value

array The resolved/loaded items.

Overrides BufferBase::resolveBufferArray

File

src/GraphQL/Buffers/EntityRevisionBuffer.php, line 59

Class

EntityRevisionBuffer
Entity revision buffer.

Namespace

Drupal\graphql\GraphQL\Buffers

Code

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.

  /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
  $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);
}