Hướng dẫn create a link/URL with querystrings set/get from parameters
28th Aug 2022In Drupal 9 I customize the filter in the block in the sidebar menu. When the user clicks on the option in the block, I need to pass his choice to the URL.
SET parameters
$options = ['absolute' => TRUE]; $url = Url::fromRoute('entity.node.canonical', ['node' => 526,'destination'=>'router_back'], $options); $link = Link::fromTextAndUrl('Here is the link', $url)->toString();
Result link:node/526?destination=router_back
GET parameters
This is a snippet on how to build a custom drupal entity database query that fetches the nodes you want from the database based on custom selection criteria.
$request_params = \Drupal::request()->query->all(); // for example my_custom_path?arg1=1&arg2=3&arg3=15 will return an array with arg1 => 1, arg2 => 3 etc if (!empty($request_params)) { $entity = \Drupal::entityTypeManager()->getStorage('node'); $query = $entity->getQuery(); $query->condition('type', 'bundle'); $query->condition('status', 1); foreach ($request_params as $key => $arr) { if ($key === 'node_id') { $query->condition('field_node_ref',array_keys($arr),'IN'); } if ($key === 'entity_id') { $query->condition('field_entity_ref',array_keys($arr),'IN'); } if ($key === 'multiple_value_field') { $query->condition('field_multiple_value',array_keys($arr),'IN'); } if ($key === 'field_with_extra_values') { $query->condition('field_with_extra_values.starthours', $arr, '<='); } if ($key === 'field_with_extra_values') { $query->condition('field_with_extra_values.endhours', $arr, '>='); } } //Get only 4 of them $query->range(0,4); //sort by DESC custom field value $query->sort('field_custom_integer','DESC'); //most recent $query->sort('created','DESC'); $ids = $query->execute(); }
Get parameter from url in drupal 9
$param = \Drupal::request()->query->all(); To get “uid” from the url, use: $uid = \Drupal::request()->query->get(‘uid’); To get “num” from the url, use: $num = \Drupal::request()->query->get(‘num’);
How do I get URL parameters in Drupal 7?
In an ideal scenario, you should use a callback function in a menu and named arguments. But in case you’re in a really particular scenario and need to get arguments like this, you can do it using the arg() function. $category = arg(0);
How do we define routes with URL parameter?
Route params are parameters whose values are set dynamically in a page’s URL. This allows a route to render the same component while passing that component the dynamic portion of the URL, so that it can change its data based on the parameter.
To get $_POST variables in Drupal 8 we need to use \Drupal::request()->request->get method.
Find the route name of the current page in Drupal 8
$current_path = \Drupal::service(‘path. current’)->getPath(); $url_object = \Drupal::service(‘path. validator’)->getUrlIfValid($current_path); $route_name = $url_object->getRouteName(); $route_parameters = $url_object->getrouteParameters();
To get all params, use:
$param = \Drupal::request()->query->all();
To get "uid" from the url, use:
$uid = \Drupal::request()->query->get('uid');
To get "num" from the url, use:
$num = \Drupal::request()->query->get('num');
- 19 views
Add new comment