You are here

protected function BlocksByRegion::resolveValues in GraphQL 8.3

Retrieve the list of field values.

Always returns a list of field values. Even for single value fields. Single/multi field handling is responsibility of the base class.

Parameters

mixed $value: The current object value.

array $args: Field arguments.

$context: The resolve context.

\GraphQL\Type\Definition\ResolveInfo $info: The resolve info object.

Return value

\Generator The value generator.

Overrides FieldPluginBase::resolveValues

File

modules/graphql_core/src/Plugin/GraphQL/Fields/Blocks/BlocksByRegion.php, line 122

Class

BlocksByRegion
List all blocks within a theme region.

Namespace

Drupal\graphql_core\Plugin\GraphQL\Fields\Blocks

Code

protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
  if ($value instanceof Url) {
    $activeTheme = $this->themeManager
      ->getActiveTheme();
    $blockStorage = $this->entityTypeManager
      ->getStorage('block');
    $blocks = $blockStorage
      ->loadByProperties([
      'theme' => $activeTheme
        ->getName(),
      'region' => $args['region'],
    ]);
    $resolve = $this->subRequestBuffer
      ->add($value, function () use ($blocks) {
      $blocks = array_filter($blocks, function (Block $block) {
        return array_reduce(iterator_to_array($block
          ->getVisibilityConditions()), function ($value, ConditionInterface $condition) {
          return $value && !$condition
            ->isNegated() == $condition
            ->evaluate();
        }, TRUE);
      });
      uasort($blocks, '\\Drupal\\Block\\Entity\\Block::sort');
      return $blocks;
    });
    return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) {

      /** @var \Drupal\graphql\GraphQL\Cache\CacheableValue $response */
      $response = $resolve();
      $blocks = array_map(function (Block $block) {
        $plugin = $block
          ->getPlugin();
        if ($plugin instanceof BlockContentBlock) {
          return $this->entityRepository
            ->loadEntityByUuid('block_content', $plugin
            ->getDerivativeId());
        }
        else {
          return $block;
        }
      }, $response
        ->getValue());
      foreach ($blocks as $block) {
        (yield new CacheableValue($block, [
          $response,
        ]));
      }
    };
  }
}