Calling A Custom Or Panel Page From Code (Panels 3.x)

First of all, I’d like to say hello fellow drupalers! This is my first post here, (and also my first ever blog post!) since Greg opened up drupaler.co.uk as a collaborative blog for Drupal developers.

I’ve been playing around a bit with Panels 3 since the early alpha releases. In the initial stages of my explorations I encountered some frustrations, as a lot of the code was still in heavy development, but I’m pleased to report that the latest beta (beta2) seems much more stable, and I havent had many issues with it so far (fingers crossed).

It came to a point in the development of our site where we were happy with the layout and content in our panels and we wanted to move them out of the database and into code using the export feature - A similar feature which is also available in the views module. This post is going to cover exporting a custom panel page and a panel page and calling them from code. The methods for the two are slightly different so I’ll explain both.

Initially I spent a fair bit of time digging around drupal.org , but couldnt find much in the way of examples on how to go about calling panels from code. I did however stumble across this great tutorial from Michelle which was an excellent starting point, and I’ll use part of that tutorial in this example.

As a first step, prepare a module ready to accept the panel export code. To do this, First of all follow Step 1 from Michelle’s tutorial - create a new module, and inside your module’s directory create a file called MODULE_NAME.delegator_default.inc.

Next, We’ll also need Step 2 from Michelles tutorial – Go ahead and paste the following code into your .inc file –

<?php
/**
* Implementation of hook_default_delegator_handlers()
*/
function MODULE_NAME_default_delegator_handlers() {
$handlers = array();
// BEGIN HANDLER EXPORT ******************************************************/
  // PASTE YOUR HANDLER EXPORT IN HERE
// END HANDLER EXPORT ******************************************************/
 
$handlers[$handler->name] = $handler;
return
$handlers;
}
?>

We’re going to skip Step 3 of Michelle’s tutorial for now and go straight to step 4. Next add the following code to your .module file

<?php
function MODULE_NAME_ctools_plugin_api($module, $api) {
  if (
$module == 'delegator' && $api == 'delegator_default') {
   
$module_path = drupal_get_path('module', 'MODULE_NAME');
    return array(
'version' => 1, 'path' => $module_path . '/path/to/include/file');
  }
}
?>

The next step is to implement hook_default_delegator_pages() in your .inc file. Paste the following code:

<?php
/**
* Implementation of hook_default_delegator_pages();
*/
function MODULE_NAME_default_delegator_pages(){
 
$pages = array();
 
//include pages
  // BEGIN HANDLER EXPORT ******************************************************/
  // PASTE YOUR HANDLER EXPORT IN HERE
// END HANDLER EXPORT ******************************************************/


 
return $pages;
}
?>

Our module now has the base ready to handle the exported panel code.

Panel Page Export
On the Panel Page you wish to export click Operations and then Export. Copy the code from the following screen and paste it into MODULE_NAME_default_delegator_pages(). That’s it for normal panel pages! If you clear caches and refresh the panels admin page (admin/build/pages), if everything worked you should see the ‘Storage’ change from ‘Normal’ to Overridden’.

Custom Page Export
Custom pages are slightly different in that they also require you to export each task handler for the page as well as the page code. This is because a custom page may have multiple task handlers, whereas a normal panel page only has one.

To export a custom page you should follow the same steps as above (Panel Page Export) to export the page code and paste it into MODULE_NAME_default_delegator_pages().

To export the handler code, on the custom page you wish to export, click Operations then Task Handlers. This should take you to a page which shows a list of handlers for the current panel.

Note* - if you only initially specified the panel to have a single handler, you might be displayed with a screen that gives you 2 options – allow multiple handlers, or create a singler handler. In order to export the handler code, you must choose allow multiple and select finish.

If you had to choose allow multiple, go back in to the task handlers for your panel. Now you should see the list of handlers.

Now export each handler. Copy the code and paste it into MODULE_NAME_default_delegator_handlers() , and that’s you done! Again, on admin/build/pages the ‘Storage’ should change from Normal to Overridden. In task handlers for the panel, each task handler ‘type’ should also change from Normal to Overridden.

As a final step, if you want to delete the copies from the database for each page / handler click on Operations then Revert, and delete the copy in the database. This will mean that your panel page is now being called from Code. If you did this the ‘Storage’ and ‘type’ fields on the pages should change to Default.

thankyou for this

Thanks for this write.

It's always useful to see a working example.

cheers

Keep writing

Keep writing

Great post!

I enjoyed this.... keep it up!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.