You are here

class Route in GraphQL 8.3

Retrieve a route object based on a path.

Plugin annotation


@GraphQLField(
  id = "url_route",
  secure = true,
  name = "route",
  description = @Translation("Loads a route by its path."),
  type = "Url",
  arguments = {
    "path" = "String!"
  }
)

Hierarchy

Expanded class hierarchy of Route

File

modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/Route.php, line 29

Namespace

Drupal\graphql_core\Plugin\GraphQL\Fields\Routing
View source
class Route extends FieldPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The path validator service.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * The language negotiator service.
   *
   * @var \Drupal\language\LanguageNegotiator
   */
  protected $languageNegotiator;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * @var \Drupal\redirect\RedirectRepository
   */
  protected $redirectRepository;

  /**
   * @var InboundPathProcessorInterface
   */
  protected $pathProcessor;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('path.validator'), $container
      ->get('language_negotiator', ContainerInterface::NULL_ON_INVALID_REFERENCE), $container
      ->get('language_manager'), $container
      ->get('redirect.repository', ContainerInterface::NULL_ON_INVALID_REFERENCE), $container
      ->get('path_processor_manager'));
  }

  /**
   * Route constructor.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition.
   * @param \Drupal\Core\Path\PathValidatorInterface $pathValidator
   *   The path validator service.
   * @param \Drupal\language\LanguageNegotiator|null $languageNegotiator
   *   The language negotiator.
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
   *   The language manager.
   * @param \Drupal\redirect\RedirectRepository $redirectRepository
   *   The redirect repository, if redirect module is active.
   * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface
   *   An inbound path processor, to clean paths before redirect lookups.
   */
  public function __construct(array $configuration, $pluginId, $pluginDefinition, PathValidatorInterface $pathValidator, $languageNegotiator, $languageManager, $redirectRepository, $pathProcessor) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->redirectRepository = $redirectRepository;
    $this->pathProcessor = $pathProcessor;
    $this->pathValidator = $pathValidator;
    $this->languageNegotiator = $languageNegotiator;
    $this->languageManager = $languageManager;
  }

  /**
   * {@inheritdoc}
   *
   * Execute routing in language context.
   *
   * Language context has to be inferred from the path prefix, but set before
   * `resolveValues` is invoked.
   */
  public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {

    // For now we just take the "url" negotiator into account.
    if ($this->languageManager
      ->isMultilingual() && $this->languageNegotiator) {
      if ($negotiator = $this->languageNegotiator
        ->getNegotiationMethodInstance('language-url')) {
        $context
          ->setContext('language', $negotiator
          ->getLangcode(Request::create($args['path'])), $info);
      }
      else {
        $context
          ->setContext('language', $this->languageManager
          ->getDefaultLanguage()
          ->getId(), $info);
      }
    }
    return parent::resolve($value, $args, $context, $info);
  }

  /**
   * {@inheritdoc}
   *
   * Route field is always language aware since it sets it's context from
   * the prefix.
   */
  protected function isLanguageAwareField() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
    if ($this->redirectRepository) {
      $currentLanguage = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
      $processedPath = $this->pathProcessor
        ->processInbound($args['path'], Request::create($args['path']));
      if ($redirect = $this->redirectRepository
        ->findMatchingRedirect($processedPath, [], $currentLanguage)) {
        (yield $redirect);
        return;
      }
    }
    if (($url = $this->pathValidator
      ->getUrlIfValidWithoutAccessCheck($args['path'])) && $url
      ->access()) {
      (yield $url);
    }
    else {
      (yield (new CacheableValue(NULL))
        ->addCacheTags([
        '4xx-response',
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArgumentAwarePluginTrait::buildArgumentDefault protected function Builds an argument's default value.
ArgumentAwarePluginTrait::buildArgumentDescription protected function Builds an argument's description.
ArgumentAwarePluginTrait::buildArguments protected function Builds the list of arguments.
ArgumentAwarePluginTrait::buildArgumentType protected function Builds an argument's type.
CacheablePluginTrait::buildCacheContexts protected function
DeprecatablePluginTrait::buildDeprecationReason protected function
DescribablePluginTrait::buildDescription protected function
FieldPluginBase::$isLanguageAware protected property Static cache for `isLanguageAwareField()`
FieldPluginBase::$languageContext protected property The language context service.
FieldPluginBase::$renderer protected property The renderer service. 1
FieldPluginBase::createInstance public static function Overrides FieldPluginInterface::createInstance
FieldPluginBase::getCacheDependencies protected function Retrieve the list of cache dependencies for a given value and arguments. 1
FieldPluginBase::getDefinition public function Returns the plugin's type or field definition for the schema. Overrides FieldPluginInterface::getDefinition
FieldPluginBase::getLanguageContext protected function Get the language context instance.
FieldPluginBase::getRenderer protected function Get the renderer service.
FieldPluginBase::resolveDeferred protected function
FieldPluginBase::unwrapResult protected function Unwrap the resolved values.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Route::$languageManager protected property The language manager.
Route::$languageNegotiator protected property The language negotiator service.
Route::$pathProcessor protected property
Route::$pathValidator protected property The path validator service.
Route::$redirectRepository protected property
Route::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Route::isLanguageAwareField protected function Route field is always language aware since it sets it's context from the prefix. Overrides FieldPluginBase::isLanguageAwareField
Route::resolve public function Execute routing in language context. Overrides FieldPluginBase::resolve
Route::resolveValues public function Retrieve the list of field values. Overrides FieldPluginBase::resolveValues
Route::__construct public function Route constructor. Overrides PluginBase::__construct
TypedPluginTrait::buildType protected function