Registering Hook Handlers for a Subset of Pages

I've found on recent projects that I want to use the Customer Portal hook system to register hook handlers, but want to restrict the execution to only certain pages (or triggered by other conditions). Turns out this is very easy to do as hook handlers are registered in the config/hooks.php file using very standard PHP code. Because there is no DSL (Domain Specific Language) you can use any other PHP statement you desire in the config/hooks.php file and it will be evaluated and executed whenever a request is made to the Customer Portal. This includes AJAX requests, direct controller execution and normal page views.

To conditionally control when hook handlers should be registered, you can simply use a PHP 'if' conditional statement. But why stop there? You also have other PHP conditionals at your disposal such as 'switch', 'else', 'elseif', etc. Wrap your hook handler registration inside of the conditional and you are ready to go.

Example Code:

$CI =& get_instance(); /* Restrict hook handler to only pages in the 'chat' directory */ if(\RightNow\Utils\Text::beginsWith($_SERVER['REQUEST_URI'], '/app/chat')) { //Preload library but only for Chat Pages $CI->load->library('ChatSupport'); /* Hook Handler registration */ $rnHooks['pre_page_render'][] = array( 'class' => 'pre_page_render', 'function' => 'doSomething', 'filepath' => 'hooks' ); } /* These hooks apply globally */ $rnHooks['pre_page_render'][] = array( 'class' => 'pre_page_render', 'function' => 'doSomethingElse', 'filepath' => 'hooks' ); //Preload library globablly $CI->load->library('MyAppLibrary');

In my example I execute a pre_page_render hook handler only if I'm loading a chat page. I determine this by looking at the Request URI and check to see if it begins with '/app/chat'. I also have another pre_page_render hook handler registered that will execute globally. Since this is just standard PHP you can use your imagination to create your own conditions that meet your use case.

Another common thing I like to do in my hooks.php file is to preload CP Libraries that I know I will need throughout the application. Since the config/hooks.php file is simply executing PHP and doing so very, very early in the application's execution it is completely possible to grab a reference to the Code Igniter singleton ($CI =& get_instance()) and use the available methods to preload resources. My example shows how to load a Library both globally and contextually. I could additionally preload other resources such as models or helpers.

I've found many non-conventional uses for my config/hooks.php file including:

  • Conditionally registering hook handlers
  • Pre loading Libraries/Helpers
  • Defining Constants
  • Master application offline feature controlled by a custom configuration. The offline mode only applies to pages viewed in "Production Mode". If the pages are viewed in "Dev" or "Staging" mode they are still visible. This is nice for releasing new features as it allows you to shut off customer access while allowing your developers and testers to access the site.

If you have any additional ideas of how to leverage the power of the config/hooks.php file leave them in the comments section of this post.

Categories : 

Zircon - This is a contributing Drupal Theme
Design by WeebPal.