Different Views inside a Custom Block in Drupal 7

296 Views Asked by At

I have defined a Custom Block in my module and I want display different views in that block when user selects different sections using navigation menu. With the use of hook_block_view() i can set the content that needs to be rendered for that particular block. But how can I render different views based on the user's selection?

For a example;

I have a Block called Customers;

  • When user selects the Customer List form navigation menu, I want to show a customer list inside Customers Block.

  • When user selects a Specific Customer, I want to show the detail of that customer inside Customers Block.

What would be the best approach to achieve this in Drupal 7?

1

There are 1 best solutions below

0
On

You can define your block from a code and use any logic you want to decide which view to use (simple if or switch php statement). Then you can embed different view based on your logic:

https://api.drupal.org/api/views/views.module/function/views_embed_view/7

Or you can get view results and prent data on your own:

https://api.drupal.org/api/views/views.module/function/views_get_view_result/7

Or, you can make 2 different block views and set for each one on what pages to be visible. You can do that in block settings or for more complex logic you can use context module to place appropriate block to a region depending on situation (page, what ever).