You are here

public function SubmitButtonRedirect::formSubmitRedirect in Flexiform 8

Redirection submit handler.

File

src/Plugin/FormEnhancer/SubmitButtonRedirect.php, line 108

Class

SubmitButtonRedirect
FormEnhancer for altering the redirects of submit buttons.

Namespace

Drupal\flexiform\Plugin\FormEnhancer

Code

public function formSubmitRedirect($form, FormStateInterface $form_state) {
  $element = $form_state
    ->getTriggeringElement();
  $token_data = $token_options = [];
  $token_info = $this->token
    ->getInfo();
  foreach ($this->formDisplay
    ->getFormEntityManager()
    ->getFormEntities() as $namespace => $form_entity) {
    $entity = $form_entity
      ->getFormEntityContext()
      ->getContextValue();
    if ($namespace == '') {
      $namespace = 'base_entity';
    }
    $token_type = $entity
      ->getEntityType()
      ->get('token_type') ?: (!empty($token_info['types'][$entity
      ->getEntityTypeId()]) ? $entity
      ->getEntityTypeId() : FALSE);
    if ($token_type) {
      $token_data[$namespace] = $form_entity
        ->getFormEntityContext()
        ->getContextValue();
      $token_options['alias'][$namespace] = $token_type;
    }
  }
  if (!empty($element['#submit_redirect'])) {

    // @todo: Support tokens.
    // @todo: Support all the different schemes.
    $path = $element['#submit_redirect'];
    if (!in_array($path[0], [
      '/',
      '?',
      '#',
    ])) {
      $path = '/' . $path;
    }
    $path = $this->token
      ->replace($path, $token_data, $token_options);
    $form_state
      ->setRedirectUrl(Url::fromUserInput($path));
  }
}