I have this in my routing page

  path: '/collecte/apports/{date}'
    _controller: '\Drupal\function_adh_apport\Controller\RapportsController::filtrer'
    date: ''
    _permission: 'access content'
    date: \d+

I got this url : ../web/collecte/apports/2016

but now, i'm trying to show in twig the value of {date}

So i do this in twig :

{{ path('function_adh_apport.adh_apport', { 'date': apports.date}) }}

but it return "../web/collecte/apports" and not just "2016"

  • apports.date is probably empty or null it should returns, make a {{ dump(apports.date) }} in your template to be sure. – Oulalahakabu Apr 24 '18 at 8:09
  • Yeah, {{ dump(apports.date) }} return NULL – psilonnix Apr 24 '18 at 8:11

According to your preprocess :

$vars['get']['date'] = \Drupal\Component\Utility\XSS::filter($_GET['date']);

Your template should be :

{{ path('function_adh_apport.adh_apport', { 'date': get.date }) }}

PS : better to use \Drupal::request()->query->get('date'); than $_GET['date']

  • Thanks for helping, but it doesn't work when i {{ dump(apports.date) }} i got 'NULL'. and my url is always "./web/collecte/apports". I have change the preprocess and my template :/ – psilonnix Apr 24 '18 at 8:21
  • your preprocess is adding $vars['get']['date'], if you need something in apports.date, you should do $vars['apports']['date'] = \Drupal\Component\Utility\XSS::filter($_GET['date']); Note that this apports.date will be present in page.html.twig. – Oulalahakabu Apr 24 '18 at 8:23
  • Sry, i have same result :/ – psilonnix Apr 24 '18 at 8:28
  • I think you are mistaking elsewhere, how is called your template file ? how is called your module ? is your function correctly prefixed with your module's name. – Oulalahakabu Apr 24 '18 at 8:31
  • So in my controller i call my template $build ['theme'] = array( '#theme' => 'liste-adh2'); in my routing i called my function "filtrer" from my controller and in my file ".module" with my template i have add the preprocess function – psilonnix Apr 24 '18 at 8:40

I have do a preprocess function in my file ".module" like this

 * Implements hook_preprocess_page()
function MYTHEME_preprocess_page(&$vars) {
  $vars['get']['date'] = \Drupal\Component\Utility\XSS::filter($_GET['date']);

but not sure that correct

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.