Breadcrumbs.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Breadcrumbs/Breadcrumbs.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Breadcrumbs;
use Drupal\Core\Breadcrumb\BreadcrumbManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
use Drupal\graphql\GraphQL\Cache\CacheableValue;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class Breadcrumbs extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $subRequestBuffer;
protected $breadcrumbManager;
protected $routeMatch;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('graphql.buffer.subrequest'), $container
->get('breadcrumb'), $container
->get('current_route_match'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, SubRequestBuffer $subRequestBuffer, BreadcrumbManager $breadcrumbManager, RouteMatchInterface $routeMatch) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->subRequestBuffer = $subRequestBuffer;
$this->breadcrumbManager = $breadcrumbManager;
$this->routeMatch = $routeMatch;
}
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof Url) {
$resolve = $this->subRequestBuffer
->add($value, function () {
$links = $this->breadcrumbManager
->build($this->routeMatch)
->getLinks();
return $links;
});
return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) {
$response = $resolve();
$links = $response
->getValue();
foreach ($links as $link) {
(yield new CacheableValue($link, [
$response,
]));
}
};
}
}
}