GotoAction.php 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\action\Plugin\Action\GotoAction.
 */

namespace Drupal\action\Plugin\Action;

use Drupal\Core\Annotation\Action;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Action\ConfigurableActionBase;
13
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
webchick's avatar
webchick committed
14
use Drupal\Core\Routing\PathBasedGeneratorInterface;
15 16 17 18
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
19 20 21 22 23 24 25 26 27 28

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

31 32 33 34 35 36 37 38 39 40
  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
   * The url generator service.
   *
webchick's avatar
webchick committed
41
   * @var \Drupal\Core\Routing\PathBasedGeneratorInterface
42 43 44 45 46 47 48 49 50 51 52 53 54 55
   */
  protected $urlGenerator;

  /**
   * Constructs a new DeleteNode object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   The tempstore factory.
webchick's avatar
webchick committed
56
   * @param \Drupal\Core\Routing\PathBasedGeneratorInterface $url_generator
57 58
   *   The url generator service.
   */
webchick's avatar
webchick committed
59
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EventDispatcherInterface $dispatcher, PathBasedGeneratorInterface $url_generator) {
60 61 62 63 64 65 66 67 68 69 70 71 72
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->dispatcher = $dispatcher;
    $this->urlGenerator = $url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('event_dispatcher'), $container->get('url_generator'));
  }

73 74 75 76
  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL) {
77 78 79 80 81 82 83 84
    $url = $this->urlGenerator
      ->generateFromPath($this->configuration['url'], array('absolute' => TRUE));
    $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);
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultConfiguration() {
    return array(
      'url' => '',
    );
  }

  /**
   * {@inheritdoc}
   */
99
  public function buildConfigurationForm(array $form, array &$form_state) {
100 101 102 103 104 105 106 107 108 109 110 111 112
    $form['url'] = array(
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#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.', array('@url' => 'http://drupal.org')),
      '#default_value' => $this->configuration['url'],
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
113
  public function submitConfigurationForm(array &$form, array &$form_state) {
114 115 116 117
    $this->configuration['url'] = $form_state['values']['url'];
  }

}