You are here

class ThunderEntitySubRequest in Thunder 6.2.x

Get data from a sub request to the URL of an entity.

Plugin annotation


@DataProducer(
  id = "thunder_entity_sub_request",
  name = @Translation("Entity sub request"),
  description = @Translation("Get data from a sub request to the URL of an entity."),
  produces = @ContextDefinition("any",
    label = @Translation("The data")
  ),
  consumes = {
    "path" = @ContextDefinition("string",
      label = @Translation("The path to request")
    ),
    "key" = @ContextDefinition("string",
      label = @Translation("The data key")
    )
  }
)

Hierarchy

Expanded class hierarchy of ThunderEntitySubRequest

File

modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntitySubRequest.php, line 34

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\DataProducer
View source
class ThunderEntitySubRequest extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The HTTP kernel service.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static($configuration, $pluginId, $pluginDefinition, $container
      ->get('http_kernel'));
  }

  /**
   * Breadcrumb constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition.
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The HTTP kernel service.
   */
  public function __construct(array $configuration, string $pluginId, $pluginDefinition, HttpKernelInterface $httpKernel) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->httpKernel = $httpKernel;
  }

  /**
   * Resolve data from a sub request.
   *
   * @param string $path
   *   The path to request.
   * @param string $key
   *   The key, where the data is stored in the sub request.
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
   *   The cacheable dependency interface.
   *
   * @return mixed
   *   The data.
   *
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function resolve(string $path, string $key, RefinableCacheableDependencyInterface $metadata) {
    $request = Request::create($path, 'GET', [
      MainContentViewSubscriber::WRAPPER_FORMAT => 'thunder_gqls',
    ]);

    /** @var \Symfony\Component\HttpFoundation\JsonResponse $response */
    $response = $this->httpKernel
      ->handle($request);
    if ($response
      ->getStatusCode() !== 200) {
      return '';
    }
    $content = (string) $response
      ->getContent();
    return Json::decode($content)[$key];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThunderEntitySubRequest::$httpKernel protected property The HTTP kernel service.
ThunderEntitySubRequest::create public static function @codeCoverageIgnore Overrides ContainerFactoryPluginInterface::create
ThunderEntitySubRequest::resolve public function Resolve data from a sub request.
ThunderEntitySubRequest::__construct public function Breadcrumb constructor.