GotoAction.php 4.41 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Action\Plugin\Action;
4

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 13 14 15
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
16 17 18 19 20 21 22 23 24 25

/**
 * Redirects to a different URL.
 *
 * @Action(
 *   id = "action_goto_action",
 *   label = @Translation("Redirect to URL"),
 *   type = "system"
 * )
 */
26
class GotoAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
27

28 29 30 31 32 33 34 35
  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
36
   * The unrouted URL assembler service.
37
   *
38
   * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
39
   */
40
  protected $unroutedUrlAssembler;
41 42

  /**
43
   * Constructs a GotoAction object.
44 45 46 47 48
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
49
   * @param mixed $plugin_definition
50 51 52
   *   The plugin implementation definition.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   The tempstore factory.
53 54
   * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
   *   The unrouted URL assembler service.
55
   */
56
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher, UnroutedUrlAssemblerInterface $url_assembler) {
57 58 59
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->dispatcher = $dispatcher;
60
    $this->unroutedUrlAssembler = $url_assembler;
61 62 63 64 65
  }

  /**
   * {@inheritdoc}
   */
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'));
68 69
  }

70 71 72 73
  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    $url = $this->configuration['url'];
    // Leave external URLs unchanged, and assemble others as absolute URLs
    // relative to the site's base URL.
    if (!UrlHelper::isExternal($url)) {
      $parts = UrlHelper::parse($url);
      // @todo '<front>' is valid input for BC reasons, may be removed by
      //   https://www.drupal.org/node/2421941
      if ($parts['path'] === '<front>') {
        $parts['path'] = '';
      }
      $uri = 'base:' . $parts['path'];
      $options = [
        'query' => $parts['query'],
        'fragment' => $parts['fragment'],
        'absolute' => TRUE,
      ];
      // Treat this as if it's user input of a path relative to the site's
      // base URL.
      $url = $this->unroutedUrlAssembler->assemble($uri, $options);
    }
94
    $response = new RedirectResponse($url);
95
    $listener = function ($event) use ($response) {
96 97 98 99
      $event->setResponse($response);
    };
    // Add the listener to the event dispatcher.
    $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
100 101 102 103 104
  }

  /**
   * {@inheritdoc}
   */
105
  public function defaultConfiguration() {
106
    return [
107
      'url' => '',
108
    ];
109 110 111 112 113
  }

  /**
   * {@inheritdoc}
   */
114
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
115
    $form['url'] = [
116 117
      '#type' => 'textfield',
      '#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 120
      '#default_value' => $this->configuration['url'],
      '#required' => TRUE,
121
    ];
122 123 124 125 126 127
    return $form;
  }

  /**
   * {@inheritdoc}
   */
128
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
129
    $this->configuration['url'] = $form_state->getValue('url');
130 131
  }

132 133 134 135 136 137 138 139
  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access->isAllowed();
  }

140
}