ThunderRedirect.php in Thunder 6.2.x
File
modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php
View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\redirect\Entity\Redirect;
use Drupal\redirect\RedirectRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ThunderRedirect extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
protected $redirectRepository;
protected $languageManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('language_manager'), $container
->get('redirect.repository', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager, ?RedirectRepository $redirectRepository = NULL) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->languageManager = $languageManager;
$this->redirectRepository = $redirectRepository;
}
public function resolve(string $path, RefinableCacheableDependencyInterface $metadata) {
if (!$this->redirectRepository) {
return [];
}
$language = $this->languageManager
->getCurrentLanguage()
->getId();
$redirect = $this->redirectRepository
->findMatchingRedirect($path, [], $language);
if ($redirect instanceof Redirect) {
$urlObject = $redirect
->getRedirectUrl();
$metadata
->addCacheTags(array_merge($redirect
->getCacheTags(), [
'redirect_list',
]));
return [
'url' => $urlObject
->toString(TRUE)
->getGeneratedUrl(),
'status' => $redirect
->getStatusCode(),
];
}
return [];
}
}