3 namespace Drupal\action\Plugin\Action;
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Action\ConfigurableActionBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\Utility\UnroutedUrlAssemblerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14 use Symfony\Component\HttpFoundation\RedirectResponse;
15 use Symfony\Component\HttpKernel\KernelEvents;
18 * Redirects to a different URL.
21 * id = "action_goto_action",
22 * label = @Translation("Redirect to URL"),
26 class GotoAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
29 * The event dispatcher service.
31 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
33 protected $dispatcher;
36 * The unrouted URL assembler service.
38 * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
40 protected $unroutedUrlAssembler;
43 * Constructs a new DeleteNode object.
45 * @param array $configuration
46 * A configuration array containing information about the plugin instance.
47 * @param string $plugin_id
48 * The plugin ID for the plugin instance.
49 * @param mixed $plugin_definition
50 * The plugin implementation definition.
51 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
52 * The tempstore factory.
53 * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
54 * The unrouted URL assembler service.
56 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher, UnroutedUrlAssemblerInterface $url_assembler) {
57 parent::__construct($configuration, $plugin_id, $plugin_definition);
59 $this->dispatcher = $dispatcher;
60 $this->unroutedUrlAssembler = $url_assembler;
66 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
67 return new static($configuration, $plugin_id, $plugin_definition, $container->get('event_dispatcher'), $container->get('unrouted_url_assembler'));
73 public function execute($object = NULL) {
74 $url = $this->configuration['url'];
75 // Leave external URLs unchanged, and assemble others as absolute URLs
76 // relative to the site's base URL.
77 if (!UrlHelper::isExternal($url)) {
78 $parts = UrlHelper::parse($url);
79 // @todo '<front>' is valid input for BC reasons, may be removed by
80 // https://www.drupal.org/node/2421941
81 if ($parts['path'] === '<front>') {
84 $uri = 'base:' . $parts['path'];
86 'query' => $parts['query'],
87 'fragment' => $parts['fragment'],
90 // Treat this as if it's user input of a path relative to the site's
92 $url = $this->unroutedUrlAssembler->assemble($uri, $options);
94 $response = new RedirectResponse($url);
95 $listener = function ($event) use ($response) {
96 $event->setResponse($response);
98 // Add the listener to the event dispatcher.
99 $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
105 public function defaultConfiguration() {
114 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
116 '#type' => 'textfield',
117 '#title' => t('URL'),
118 '#description' => t('The URL to which the user should be redirected. This can be an internal URL like /node/1234 or an external URL like @url.', ['@url' => 'http://example.com']),
119 '#default_value' => $this->configuration['url'],
128 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
129 $this->configuration['url'] = $form_state->getValue('url');
135 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
136 $access = AccessResult::allowed();
137 return $return_as_object ? $access : $access->isAllowed();