How to get a parameter value from the current URL in views-view-fields.html.twig?

Solutions

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

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 was also trying to get the url parameters, but the current path was only returning the path without the parameters.

The following solution worked for me in a page preprocess function, I haven't tried with views fields but it should work also.

function yourtheme_preprocess_views_view_fields(&$variables) {

  // Retrieve an array which contains the path parameters.
  $variables['params']['parameter_name'] = $_GET['parameter_name'];
}
Tags: Views / Drupal 8 / Theming

Similar questions

How to print fields value in views-view.html.twig in drupal 8
How to print fields value in views-view.html.twig I have created a views-view.html.twig file for my view slider_3. The template name is 'views-view--slider_3.html.twig' The view formatting is done by this template, but I need a field is come at begging position of the view: <div class="slider-3-main">{{ rows.required_field }} <div{{ attrib...
Get the raw URL of an image in views-view-table.html.twig
In the views-view-table.html.twig template, I can get the raw values of fields by repeating on rows and accessing the view object. For example: With Formatter for an image field set to URL to image in the view, I can test to see if an image exists in the field by doing: QUESTION: How can I get the raw URL for the image? If I use: I get a number as ...
How to pass Get parameter to url when url has "?" mark
In my site clean urls is not working. As a result ?q is getting appended to the url. My site url is http://abc.com/?q=user I wrote a form in a page, and redirect to another view which is http://abc.com/?q=listings. When I click on submit button in form page GET parameter are not getting appended correctly: They are getting appended as http://abc.co...
fields.title.content in views-view-fields--n--block.html.twig won't display
This is what I wrote into views-view-fields--n--block.html.twig. The second and third fields are displayed properly (here I used the machine name). But how do I get the title which is automatically there with every content type? please help!
PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined:
I get "PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" using the below db_query() call. I also tried like this. Wny idea what's wrong in my query?
db_insert throwing up "Invalid parameter number: parameter was not defined"
I've got a form in a Drupal module I'm trying to submit to create an "event". The events table structure is: When I try to create a new event and insert it into the database using: it throws up an error saying: Any ideas as to what I'm doing wrong? Thanks

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.