How URI patterns work? Is it possible to address query parameters?

Created on 18 August 2023, over 1 year ago
Updated 24 August 2023, over 1 year ago

I've tried to use an URL query parameter (so something like: "*?switch_theme") as URI pattern, but I was not able to figure out how and if this is possible.

Is this URL pattern matching notation a standard? I couldn't find a good documention to this topic.
Probably worth to link it and provide some more examples on the module page?

💬 Support request
Status

Fixed

Version

1.2

Component

Documentation

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @thomas.frobieter
  • 🇫🇷France racol

    the pattern requested is a regex (or regexs concatanated) which will be used by the theme negotiator to know if the current URI must apply or not the theme choosen in the tab "Appearance/Switch Theme".

    You must have a little knowledge about how to write regex.

    1. Example 1:
    2. #^/second-part-theme#

      # : regex delimiter
      ^ : must begin by "/second-part-theme"

      In this case, all URL wich match "https://www.your-website.com/second-part-theme", the choosen theme for this pattern will be applied

      https://www.your-website.com/second-part-theme
      https://www.your-website.com/second-part-theme/sub-part
      https://www.your-website.com/second-part-theme/sub-part/sub-part-2
      ...

    3. Example 2:
    4. #^/cms-page-presentation$#

      # : regex delimiter
      ^ : must begin by "/cms-page-presentation"
      $ : must end by "/cms-page-presentation"

      so in this case, the theme will be applied only for

      https://www.your-website.com/cms-page-presentation

      You can concatanate regexs with a "|"

    5. Example 3:
    6. #^/cms-page-presentation$|^/second-part-theme|^/test$#

      # : regex delimiter
      ^ : must begin by "/cms-page-presentation" OR "/second-part-theme" OR "/test"
      $ : must end by "/cms-page-presentation" OR "/test"
      | : regexs concatanator

      In this case, the theme will be applied for thoses URLS

      https://www.your-website.com/cms-page-presentation
      https://www.your-website.com/second-part-theme
      https://www.your-website.com/second-part-theme/sub-part
      https://www.your-website.com/second-part-theme/sub-part/sub-part-2
      ...
      https://www.your-website.com/test

    In case of query parameter like "/news?page=10" or "/news?page=10&order=ASC&param=doe&...", you don't need to specify query parameters in the URI pattern configuration, the "/news" condition is sufficient to do what you want and query parameters will be always available for the Drupal system or for your custom module.

    I'm going to update the documentation to say explicitly that a regex is asked, not an URI with query paramaters.

    Pleasure

  • Assigned to racol
  • You must have a little knowledge about how to write regex.

    Okay, I wasn't sure about this, because my Query-Parameter regex didn't worked. But the regex filter is simply limited to the path (+domain)?

    In case of query parameter like "/news?page=10" or "/news?page=10&order=ASC&param=doe&...", you don't need to specify query parameters in the URI pattern configuration, the "/news" condition is sufficient to do what you want and query parameters will be always available for the Drupal system or for your custom module.

    Sorry, I should have clarified what's my intention here. I use Switch Theme inside an iframe, so the idea is to simply add a "?is-embed" query parameter, to switch the theme in this case.

    But thats not possible if I understand you correctly?

    Thanks for all the examples, great help for all of us who 'fear' REGEX ;)

  • 🇫🇷France racol

    Thanks for all the examples, great help for all of us who 'fear' REGEX ;)

    héhé ;)

    Sorry, I should have clarified what's my intention here. I use Switch Theme inside an iframe, so the idea is to simply add a "?is-embed" query parameter, to switch the theme in this case.

    Don't worry, actually, you can't with this Switch Theme module version because of only the URI is concerned by the theme negociator as "/news", "/toto", etc but not query parameters as "/news?toto=joe" as regex even if you have escaped the "?".

    I have tested myself - not via an iframe but it should be the same behaviour - and modified the theme negociator to resolve your problem.

    /modules/custom/switch_theme/src/Theme/ThemeNegotiator.php

    Just replace

    l.89 $requestUri = strtok(\Drupal::request()->getRequestUri(), '?');

    by

    l.89 $requestUri = \Drupal::request()->getRequestUri();

    What does it mean ? For the Switch theme module, only URI is important, I haven't develop this module to take care of query parameters to adapt theme depending only of parameters in the URL (caching problem, take care about that ! :/ )
    but this solution works, not the best at all, but it works for your problem.

    Let me know if the problem is solved :).

    Pleasure

  • 🇫🇷France racol

    It's works but let me, before, check the security :) !

    Pleasure

  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024