1

I need the modified URL to call a view with contextual filter, in Drupal 8. The URL must include the value of a field, that the user's clicking on.

This works.

<a href="{{ url('view.testview.page_1', {'arg_0': field_example__value}) }}">Link to Testview</a>

How can I overwrite Link to Testview with the field value?

{{field_example__value}} works as well.

1
  • Sorry, if I express myself unclear I'm beginner... 4*4k helped me a bit further. Commented Apr 22, 2017 at 21:41

1 Answer 1

1

I've seen it often that the path is simply built by concatenating strings. But if you want to do it in a D8 way, then use the route from the view.

You can investigate the routes with Drupal Console on the command line or the Devel module in UI, where you'll find the routes in /devel/routes.

For example this view:

ROUTE NAME: view.testview.page_1    PATH: /testview/{arg_0}/{arg_1}

When you know the route name and the arguments for the contextual filters you can build the URL:

use Drupal\Core\Url;

  $url = Url::fromRoute('view.testview.page_1', [
    'arg_0' => $arg1,
    'arg_1' => $arg2,
  ]);

The URL is an object, which you put in a render array, for example in a link element. If needed you can get the path as string with the method ->toString(), but this should be avoided.

If you want to use the route in a field rewrite you can use the twig function url() in a field replacement pattern:

<a href="{{ url('view.testview.page_1', {'arg_0': field_example__value}) }}">Link to Testview</a>
4
  • No, I meant something different. The view returns a field, token says: [node: field_betreuer]. How do I get the content of this field appending to the URL? "%" or "$" does not seem to work. It must be easy to transfer the contents of this field to the URL, isn't it? How to write this? Commented Apr 22, 2017 at 17:32
  • @FrankGiesecke, added an example for a field rewrite. Please provide more details what you want to achieve.
    – 4uk4
    Commented Apr 22, 2017 at 18:07
  • Thanks, that works. How can I overwrite "Link to Testview" with field value? - field_example__value itself doesn't work. Commented Apr 22, 2017 at 21:42
  • {{field_example__value}} works as well. Thx Commented Apr 22, 2017 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.