You are here

class GotoAction in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/action/src/Plugin/Action/GotoAction.php \Drupal\action\Plugin\Action\GotoAction

Redirects to a different URL.

Plugin annotation


@Action(
  id = "action_goto_action",
  label = @Translation("Redirect to URL"),
  type = "system"
)

Hierarchy

Expanded class hierarchy of GotoAction

File

core/modules/action/src/Plugin/Action/GotoAction.php, line 31
Contains \Drupal\action\Plugin\Action\GotoAction.

Namespace

Drupal\action\Plugin\Action
View source
class GotoAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
   * The unrouted URL assembler service.
   *
   * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
   */
  protected $unroutedUrlAssembler;

  /**
   * 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 mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   The tempstore factory.
   * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
   *   The unrouted URL assembler service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher, UnroutedUrlAssemblerInterface $url_assembler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->dispatcher = $dispatcher;
    $this->unroutedUrlAssembler = $url_assembler;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $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://example.com',
      )),
      '#default_value' => $this->configuration['url'],
      '#required' => TRUE,
    );
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access
      ->isAllowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionBase::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionInterface::executeMultiple 2
ConfigurableActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
ConfigurableActionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
ConfigurableActionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
ConfigurableActionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GotoAction::$dispatcher protected property The event dispatcher service.
GotoAction::$unroutedUrlAssembler protected property The unrouted URL assembler service.
GotoAction::access public function Checks object access. Overrides ActionInterface::access
GotoAction::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
GotoAction::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GotoAction::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableActionBase::defaultConfiguration
GotoAction::execute public function Executes the plugin. Overrides ExecutableInterface::execute
GotoAction::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
GotoAction::__construct public function Constructs a new DeleteNode object. Overrides ConfigurableActionBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 2
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.