0

I know I can pass variables to templates using render arrays.

$theme = 'block-AcademicDisplay';   

return array(
  '#theme' => $theme,
  '#content' => $content,
  '#url' => urlencode($url),
);

How can I pass variables and call controller methods from that template? On that template, when clicking on some buttons or links, I would like to call a controller method and pass it 2 parameters.

  • 1
    Hi @Matoeil Can you please share your solution if resolved? – Prerit Mohan Mar 13 at 15:41
  • i have used parameters upcasting in routes ( see below) – Matoeil Mar 15 at 8:38
1

Passing arguments in route parameters

I think the missing part is the route that you have defined for the controller. You have to use this to generate the url:

$url = Url::fromRoute('entity.node.canonical', ['node' => 1]);

This example is for the node controller and passes the argument nid=1. The parameter names are defined in the route definition.

Passing arguments in query parameters

I've seen in one of your other posts that you want to use query parameters. This is an example for the user login:

$url = Url::fromRoute('user.login', [], ['query' => ['destination' => 'http://example.com']]);

For this to work you don't need to define a query parameter in the route. Simply inject the request as function argument (the request is always available to a controller, it's not necessary to define it in the route) and get the query parameter:

use Symfony\Component\HttpFoundation\Request;

  public function myController(Request $request) {

    $destination = $request->query->get('destination');

  }
0

i have used parameter upcasting in routes to convert dynamic values in url to object instances

https://www.drupal.org/docs/8/api/routing-system/parameter-upcasting-in-routes

in a bloc template listing all publications

 <a href="/publication/{{ row.halId_s }}">call to publication detail</a>

my_module.routing.yml , linking the route to the controller, with upcasting parameters

my_module.publication_detail:
#parameter upcasting
  path: '/publication/{halId_s}'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::publicationDetail'
    _title: 'publication detail'
  options:
    parameters:
      halId_s:
        type: int
  requirements:
    _permission: 'access content'

MyModuleController.php

<?php


namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;

/**   
 *
 * for created routes publication/{}
 */
class MyModuleController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function publicationDetail($halId_s) {

    $module_global_config = \Drupal::configFactory()
      ->getEditable('amu_hal.settings');
    $amu_hal_url_ws = $module_global_config->get('amu_hal_url_ws');
    $docType_s = $module_global_config->get('docType_s');

    $url = $amu_hal_url_ws . 'search/?fl=title_s,en_title_s,docid,label_s,en_label_s,docType_s,authIdHal_s,halId_s,structId_i,uri_s,keyword_s,en_keyword_s,authLastNameFirstName_s,journalTitle_s,abstract_s,en_abstract_s,producedDate_tdate,producedDateY_i,language_s,fileMain_s&fq=halId_s:' . $halId_s;

    try {
      $client = new Client();
      $response = $client->request('GET', $url);
      $content = json_decode($response->getBody(), TRUE);       
    } catch (ConnectException $e) {
      \Drupal::logger('amu_hal')->error('cette url n est pas bonne' . $url);
    }

    $theme = 'page-PublicationDetails';

    $module_path = file_create_url(drupal_get_path('module', 'my_module'));

    $build = [
      '#theme' => $theme,
      "#content" => $content,
      '#url' => $url,
      '#docTypes_array' => $docType_s,
      '#module_path' => $module_path,
    ];
    return $build;
  }

}

page-PublicationDetails.html.twig

{% for doc in content.response.docs %}    
      {{ doc.title_s|first }}
    // some more //    
 {% endfor %}

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.