5

I've made a form and I'm trying to get the submit result into a redirect page with URL api:

 public function submitForm(array &$form, FormStateInterface $form_state)
    {

    $metier=$form_state->getValue('metier');
    $position=$form_state->getValue('position');

    //$form_state->setRedirect('recherche.resultat',array('param'=>'test');

    $url = new Url('recherche.resultat');
    $url->setOptions(array('param'=>'test2'));
    $url->setRouteParameters(array('param'=>'test2'));

    $form_state->setRedirectUrl($url);

    drupal_set_message("envoyer");

}

I set two options :

$url->setOptions(array('param'=>'test2'));

and

$url->setRouteParameters(array('param'=>'test2'));

The form redirect me and this is my controller :

public function resultat()
{
    $request = \Drupal::request();
    $url = Url::createFromRequest($request);
    kint($url);
    kint($url->getOptions());
    kint($url->getRouteParameters());
    return array('#markup'=>t('test'));

}

The two methods :

    kint($url->getOptions());
    kint($url->getRouteParameters());

are empty

This is my routing :

 recherche.resultat:
  path: '/resultat'
  defaults:
    _title: 'resultat'
    _controller: '\Drupal\recherche\Controller\ResultatRechercheController::resultat'
  requirements:
     _permission: 'access content'

The parameters are in the URL and I can get them with $_GET but how can I get with the URL API?

6

Ok i got it by this way :

So i have 2 parameters from my form to pass to my controller :

public function submitForm(array &$form, FormStateInterface $form_state)
{

    $metier=$form_state->getValue('metier');
    $position=$form_state->getValue('position');

    $url = \Drupal\Core\Url::fromRoute('recherche.resultat')
          ->setRouteParameters(array('metier'=>$metier,'position'=>$position));

    $form_state->setRedirectUrl($url);


}

The key is in the YAML routing, you need to build your path like that for get parameters ( name of parameters need to be same as your route, see previous function (submitForm)) :

recherche.resultat:
  path: '/resultat/{metier}/{position}'
  defaults:
    _title: 'resultat'
    _controller: '\Drupal\recherche\Controller\ResultatRechercheController::resultat'
    metier: ''
    position: ''
  requirements:
     _permission: 'access content'

And in my controller for get my parameters :

Class: ResultatRechercheController

 public function resultat($metier, $position)
    {

        kint($metier);
        kint($position);

        return array('#markup'=>t('la premiere spécialite est @specialite',array('@specialite'=>'test')));

    }

And the URL is clean :

localhost:8888//monsite.com/resultat/{metier}/{position}

3

You can get the get attributes directly from the request object \Drupal::request()->attributes->get('param'); or \Drupal::request()->query

Source: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3Arequest/8

  • Thank, so how work $url->getOptions or $url->getRouteParameters() ? – Kevin Aug 4 '15 at 13:25
  • 1
    This link says that the usage is discouraged. – Will Apr 12 '16 at 21:17
0

In a block form, you can simply add your parameters in your form redirect url, without routing :

public function submitForm(array &$form, FormStateInterface $form_state) {
  # get form data
    $form_var = $form_state->getValue('input_name')
    ...

  # set form data in url redirect
  $option = [
    'query' => ['testvar' => $form_var],
  ];
  $url = Url::fromUri('internal:/my_url/redirect', $option);

  $form_state->setRedirectUrl($url);
}

Then retrieve it and clean it :

# retrieve query param
$my_var = \Drupal::request()->query->get('testvar');

# clean for a textfield
$my_var_escaped = Html::escape($my_var);
  • Interesting, i ll try that – Kevin May 17 '17 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.