3

I have set up a custom page with Panels.

In the Panel configuration I have set the page path to /products/%term.

I made a custom module which currently displays "Hello world" on this page, and all is well.

function product_tree_block_view($delta='') {
  $block = array();
  switch($delta) {
    case 'product_tree' :
      $block['content'] = theme('product_tree', array("greet" => "Hello world",));
      break;
  }
  return $block;
}

However; I would like to process the %term to fetch some additional information, relevant only to the current %term.

My question is: How can I access the %term? Is this variable accessible from my module at all?

9

The function arg() will do that for you. For example, in path http://siteurl/products/%items,

  • arg(0) will have the value products (first value after your $base_url)
  • arg(1) will have the value of %items (second value).

You might then just apply your logic, in this this case you could do the check arg(1).

  • This is using showing the original url for me: /node/1222 – martinedwards Jan 12 '17 at 11:36
0

For Drupal 7 use arg() function and you are good to go.

For Drupal 8 follow below procedure,

Here's the example of accessing URL parameters and passing them to a TWIG template, I am considering you have already created your module and required files and suppose "/test?fn=admin" is your URL

  1. In Your .module file implement hook_theme and define variables and template name (Make sure you replace "_" with "-" when creating the template file)

     function my_module_theme () {   
             return [
              'your_template_name' => [               
                 'variables' => [
                     'first_name'    => NULL,
                  ],   
             ]; 
           }
    

Now create your controller and put below code in it.

 namespace Drupal\my_module\Controller;

 use Drupal\Core\Controller\ControllerBase;
 use Symfony\Component\HttpFoundation\Request;


 class MyModule extends ControllerBase {

   public function content(Request $request) {

     return [
       '#theme' => 'my_template',
       '#first_name' => $request->query->get('fn'), //This is because the parameters are in $_GET, if you are accessing from $_POST then use "request" instead "query"
     ];
   }

 }

Now in your TWIG file which should be "my-template.html.twig" you can access this parameter as,

 <h3>First Name: {{ first_name }}</h3>

And its done. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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