GotoAction.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Drupal\action\Plugin\Action;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Drupal\Core\Access\AccessResult;
  5. use Drupal\Core\Action\ConfigurableActionBase;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  8. use Drupal\Core\Session\AccountInterface;
  9. use Drupal\Core\Utility\UnroutedUrlAssemblerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15. * Redirects to a different URL.
  16. *
  17. * @Action(
  18. * id = "action_goto_action",
  19. * label = @Translation("Redirect to URL"),
  20. * type = "system"
  21. * )
  22. */
  23. class GotoAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
  24. /**
  25. * The event dispatcher service.
  26. *
  27. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  28. */
  29. protected $dispatcher;
  30. /**
  31. * The unrouted URL assembler service.
  32. *
  33. * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
  34. */
  35. protected $unroutedUrlAssembler;
  36. /**
  37. * Constructs a GotoAction object.
  38. *
  39. * @param array $configuration
  40. * A configuration array containing information about the plugin instance.
  41. * @param string $plugin_id
  42. * The plugin ID for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
  46. * The tempstore factory.
  47. * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
  48. * The unrouted URL assembler service.
  49. */
  50. public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher, UnroutedUrlAssemblerInterface $url_assembler) {
  51. parent::__construct($configuration, $plugin_id, $plugin_definition);
  52. $this->dispatcher = $dispatcher;
  53. $this->unroutedUrlAssembler = $url_assembler;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  59. return new static($configuration, $plugin_id, $plugin_definition, $container->get('event_dispatcher'), $container->get('unrouted_url_assembler'));
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function execute($object = NULL) {
  65. $url = $this->configuration['url'];
  66. // Leave external URLs unchanged, and assemble others as absolute URLs
  67. // relative to the site's base URL.
  68. if (!UrlHelper::isExternal($url)) {
  69. $parts = UrlHelper::parse($url);
  70. // @todo '<front>' is valid input for BC reasons, may be removed by
  71. // https://www.drupal.org/node/2421941
  72. if ($parts['path'] === '<front>') {
  73. $parts['path'] = '';
  74. }
  75. $uri = 'base:' . $parts['path'];
  76. $options = [
  77. 'query' => $parts['query'],
  78. 'fragment' => $parts['fragment'],
  79. 'absolute' => TRUE,
  80. ];
  81. // Treat this as if it's user input of a path relative to the site's
  82. // base URL.
  83. $url = $this->unroutedUrlAssembler->assemble($uri, $options);
  84. }
  85. $response = new RedirectResponse($url);
  86. $listener = function ($event) use ($response) {
  87. $event->setResponse($response);
  88. };
  89. // Add the listener to the event dispatcher.
  90. $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function defaultConfiguration() {
  96. return [
  97. 'url' => '',
  98. ];
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  104. $form['url'] = [
  105. '#type' => 'textfield',
  106. '#title' => t('URL'),
  107. '#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']),
  108. '#default_value' => $this->configuration['url'],
  109. '#required' => TRUE,
  110. ];
  111. return $form;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  117. $this->configuration['url'] = $form_state->getValue('url');
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
  123. $access = AccessResult::allowed();
  124. return $return_as_object ? $access : $access->isAllowed();
  125. }
  126. }