Score:0

How do I get my custom module controller logic to trigger on only certain url paths?

rs flag

Hi all I am very new to Drupal and am trying to write a custom module. So my site has a bunch of different urls for example example.com/users/3 or example.com/articles/47. I want my custom module code to run whenever the user is on a path that has exmaple.com/users/.. in it.

The custom code is in the module controller and has some api calls in it using Drupals global user entity. Right now I got it working where in the routing.yml file it takes the path I give it and just creates a new page where the custom code is working path: '/custom/page'

How do I get my custom code to trigger on certain url paths?

Score:1
cg flag

I would:

  1. create a custom module
  2. Add a hook_page_preprocess() function.
  3. Check the URL with $current_path = \Drupal::service('path.current')->getPath(); (See this answer for more info)
  4. Do your logic in the path match example.com/users/

You could also checkout Event Subscribers below, but I'm unfamiliar with them.

For more info, see https://www.drupal.org/docs/creating-modules/subscribe-to-and-dispatch-events

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.