0

On our Drupal 8 site, we are using the article content type for press releases. We have the pathauto module generating automatic aliases for these nodes, so the article node entitled "Cool Things are Happening" would get the url /press-releases/cool-things-are-happening.

Additionally, we have a view of press releases, at /press-releases. This is provided by a view of articles that has a page display.

Our marketing department wants some changes implemented to the page delivered from /press-releases. Since we have the paragraphs module installed on our side, I decided the best way to future-proof the page would be to disable the press releases page display, create a block display of that view, and add a basic page with the press releases block display in a paragraph, and set the alias of that node to /press-releases.

However, one of the requested changes is a year filter feature on the press releases page, so that the results of the view can be filtered by year.

I have that filter working on the view, as I've confirmed in the block display preview. I've added a custom block to the new /press-releases page that has links to /press-releases/2019, press-releases/2018, etc. The idea, obviously, is that the second part of the path would be the value for the contextual filter on the block display of the view.

However, I'm not sure if it's possible, and if it is, then how to specify a wildcard route in Drupal 8 that passes the second url parameter to the contextual filter. Currently, when I navigate to /press-releases/2018, I get a 404, which is not unexpected.

Since the view display is not a page, but a block, I don't see where I can indicate a path with a wildcard value, like /press-releases/%. (It's been a while since I've developed for Drupal 7, but I seem to recall that's how values were grabbed from paths.)

Editing the Contextual Filter on the block display of the view, I tried adding "Raw value from URL". I set the settings

When the filter value is NOT available - Provide default value - Type: Raw value from URL - Path component: 2 - Exception value: all

enter image description here

However, with these settings, /press-releases and /press-releases/all show the page with an empty view block display, and also paths with the year value (like /press-releases/2018) are also empty. However, node paths still work. I'm not sure if I'm using the wrong settings, or if this way is not going to work regardless.

This answer shows how to get a value from the URL using the InboundPathProcessorInterface in a custom module, but I'm not sure how to then pass that value to the view's block display in the paragraph. Also, if I'm using /press-releases/% as my pattern to match, I don't want to interfere and break existing node paths at e.g. /press-releases/cool-things-are-happening. I only want to match YYYY, where Y is a number. (I could do more advanced validation for the year, but I can pretty much guarantee we are never going to have just a year by itself as the actual title of a press release node.)

How can I pass the year value to the contextual filter? Have I gone down the wrong path for developing this feature?

We are on Drupal 8.7.3.

1 Answer 1

1

So I was able to get my functionality by changing the Contextual Filter to a query parameter:

enter image description here

So in my widget block, I changed the link values from /press-releases/2015 to /press-releases?year=2015. Now the functionality works as wanted. Both urls /press-releases/ and /press-releases?year=all show all the press releases, and the node urls still work.

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.