Drupal - How to get a parameter value from the current URL in views-view-fields.html.twig?
You could use Twig's split
filter, though I don't recommend scripting too much inside templates.
Instead I recommend to pass the value over to Twig from a preprocess function from inside your theme or a custom module via template_preprocess_views_view_fields
.
/**
* Implements template_preprocess_views_view_fields().
*/
function MYTHEME/MYMODULE_preprocess_views_view_fields(&$variables) {
// Retrieve an array which contains the path pieces.
$current_path = \Drupal::service('path.current')->getPath();
if ($current_path[0] == 'questions') {
$variables['question_id'] = $current_path[1];
}
}
And then inside your template print it for example like so:
{% if question_id %}
<div id="{{ question_id }}">Question ID: {{ question_id }}</div>
{% endif %}
I don't know it is best way or not but can help you You are using the {% set url = url('current>') %} After dump, it turned out that's what
array(3) {
["#markup"]=>
string(23) "http://example.com/category" ["#cache"]=> array(3) {
["contexts"]=> array(2) { [0]=> string(5) "route" [1]=>string(8)
"url.site" } ["tags"]=> array(0) { } ["max-age"]=> int(-1) }
["#attached"]=> array(0) { } }
Convert array to sting
{% set url = url('current>')['#markup'] %}
{{ url|split('/')|last }}
you will get the last value from theurl