You are here

public function GotoAction::execute in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Action/Plugin/Action/GotoAction.php \Drupal\Core\Action\Plugin\Action\GotoAction::execute()
  2. 10 core/lib/Drupal/Core/Action/Plugin/Action/GotoAction.php \Drupal\Core\Action\Plugin\Action\GotoAction::execute()

Executes the plugin.

Overrides ExecutableInterface::execute

File

core/lib/Drupal/Core/Action/Plugin/Action/GotoAction.php, line 73

Class

GotoAction
Redirects to a different URL.

Namespace

Drupal\Core\Action\Plugin\Action

Code

public function execute($object = NULL) {
  $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);
  }
  $response = new RedirectResponse($url);
  $listener = function ($event) use ($response) {
    $event
      ->setResponse($response);
  };

  // Add the listener to the event dispatcher.
  $this->dispatcher
    ->addListener(KernelEvents::RESPONSE, $listener);
}