Skip to content

Commit 18ad769

Browse files
pmelabfubhy
authored andcommitted
Redirect handling (#666)
1 parent 82cbf59 commit 18ad769

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\RedirectUrl;
4+
5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7+
use Drupal\redirect\Entity\Redirect;
8+
use GraphQL\Type\Definition\ResolveInfo;
9+
10+
/**
11+
* @GraphQLField(
12+
* id = "redirect_url_code",
13+
* secure = true,
14+
* name = "code",
15+
* description = @Translation("The redirect code."),
16+
* type = "Int",
17+
* provider="redirect",
18+
* parents = {"RedirectUrl"}
19+
* )
20+
*/
21+
class Code extends FieldPluginBase {
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
26+
if ($value instanceof Redirect) {
27+
yield $value->getStatusCode();
28+
}
29+
}
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\RedirectUrl;
4+
5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7+
use Drupal\redirect\Entity\Redirect;
8+
use GraphQL\Type\Definition\ResolveInfo;
9+
10+
/**
11+
* @GraphQLField(
12+
* id = "redirect_url_target",
13+
* secure = true,
14+
* name = "target",
15+
* description = @Translation("The redirect target."),
16+
* type = "Url",
17+
* provider="redirect",
18+
* parents = {"RedirectUrl"}
19+
* )
20+
*/
21+
class Target extends FieldPluginBase {
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
26+
if ($value instanceof Redirect) {
27+
yield $value->getRedirectUrl();
28+
}
29+
}
30+
31+
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/Route.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
1010
use Drupal\language\LanguageNegotiator;
1111
use Drupal\Core\Language\LanguageManagerInterface;
12+
use Drupal\redirect\Entity\Redirect;
13+
use Drupal\Tests\views_ui\Functional\ReportFieldsTest;
1214
use GraphQL\Type\Definition\ResolveInfo;
1315
use Symfony\Component\DependencyInjection\ContainerInterface;
1416
use Symfony\Component\HttpFoundation\Request;
@@ -126,10 +128,28 @@ protected function isLanguageAwareField() {
126128
return TRUE;
127129
}
128130

131+
/**
132+
* Get the redirect repository service.
133+
*
134+
* @return \Drupal\redirect\RedirectRepository
135+
*/
136+
protected function getRedirectRepository() {
137+
return \Drupal::service('redirect.repository');
138+
}
139+
129140
/**
130141
* {@inheritdoc}
131142
*/
132143
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
144+
if (\Drupal::moduleHandler()->moduleExists('redirect')) {
145+
$redirectRepository = $this->getRedirectRepository();
146+
$currentLanguage = $this->languageManager->getCurrentLanguage()->getId();
147+
if ($redirect = $redirectRepository->findMatchingRedirect($args['path'], [], $currentLanguage)) {
148+
yield $redirect;
149+
return;
150+
}
151+
}
152+
133153
if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($args['path'])) && $url->access()) {
134154
yield $url;
135155
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Types\Routing;
4+
5+
use Drupal\Core\Url;
6+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
7+
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
8+
use Drupal\redirect\Entity\Redirect;
9+
use GraphQL\Type\Definition\ResolveInfo;
10+
11+
/**
12+
* GraphQL type for redirects.
13+
*
14+
* @GraphQLType(
15+
* id = "redirect_url",
16+
* name = "RedirectUrl",
17+
* interfaces={"Url"},
18+
* provider="redirect",
19+
* )
20+
*/
21+
class RedirectUrl extends TypePluginBase {
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function applies($object, ResolveContext $context, ResolveInfo $info) {
27+
return $object instanceof Redirect;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)