Skip to Content
RSSGrab the RSS feed! ->

How to load panel pages from code

They will load faster, it's easier for deployment, and you'll be able have them under version control.

We all know that you can load views from code, and it is even recommended. But what about Panels pages? It is also possible. And actually, it's quite easy.

Let's say that you have reached a beta stage for these pages, and you are ready to start having these under version control so you can sleep better at night.

1. Create the module that will load these pages

panels_default_pages_file_structure.png
We'll call our module ctools_defaults. On the right we see the structure and files that we will need in our module.
Inside of the pages directory, we'll be inserting each of our exported pages.

2. Implement hook_ctools_plugin_api()

This is done in order to let ctools know that our module has something to say. We'll do this inside ctools_defaults.module.

<?php
/**
  * Implementation of hook_ctools_plugin_api().
  */
function ctools_defaults_ctools_plugin_api($module, $api) {
  if (
$module == 'page_manager' && $api == 'pages_default') {
    return array(
'version' => 1);
  }
}
?>

3. Implement hook_default_page_manager_pages()

The name of the file, very important, will be MODULENAME.pages_default.inc - and in our case ctools_defaults.pages_default.inc. Because we are clever, in it we'll do a little trick, to facilitate our lives, and have each page in its separate file. This will allow us to have an independent version control per page, and also make the re-exporting/editing of existing pages easier.

<?php
/**
* Implementation of hook_default_page_manager_pages().
*/
function ctools_defaults_default_page_manager_pages() {
 
$pages = array();
 
$path = drupal_get_path('module', 'ctools_defaults') . '/pages';
 
$files = drupal_system_listing('.inc$', $path, 'name', 0);
  foreach(
$files as $file) {
    include_once
$file->filename;
   
$pages[$page->name] = $page;
   }
  return
$pages;
}
?>

So what's special? not much, this code looks for .inc files in the directory called pages within our module, and for each of the files found, it tells panels that we have a default page. Each new file that we place in there will be loaded automatically.

4. Export our panel pages

The process is pretty simple, but in case someone gets lost, the button is right here when you are editing a panel page:
panel_page_export_button.png

Which will get you to a page with all the code ready to be copied, like:
panel_page_export_code.png

5. Create our PAGENAME.inc

We create a file called PAGENAME.inc inside our pages directory, within our module's directory.

<?php
// paste below the code exported from the panels UI
?>

Inside this file, we'll open php, and paste the code copied in the previous screen. Just like that, don't be shy and save the file.

6. Empty the cache

Panels caches (thankfully) the default pages that third party modules provide, so we must clear the cache when we create a new default page, and when we modify the code of an existing one.

Conclusion

This technique let's us sleep better at night. If someone ever touches the panel page, and breaks things, we can always revert to the default code. We'll be able to create pages based on existing ones if they are very similar, just by copying and modifying the original code, reducing our development time, and improving our personal relationships as an unexpected bonus.

If you have any comments, you know what to do. If you see a mistake in my technique, please do let me know and I'll fix it right away.

AttachmentSize
ctools_defaults.tar.gz926 bytes

8 responses to this article.

#1

Great post, I've seen others

Great post, I've seen others trying to explain the same thing but you've done really well to keep it simple and informative. btw, where's the demo for your views accordion module, I'm thinking of using but I don't think it makes views attachments into an accordion, does it?

#2

Forgot about the demo, I'm

Forgot about the demo, I'm being an idiot, I can see the right hand side of the page uses it :-)

#3

Thank you for the kind words

Thank you for the kind words Nikos.

#4

I spent a week trying to

I spent a week trying to figure out how to do this. I wish I would have stumbled across this then. Great site and thanks for the views accordian!

#5

Thanks for the article but I

Thanks for the article but I have a couple questions.

1. Could you give some example code of how I would create one module to add several panels pages to code. It would be nice not to have to create a module for EACH page.

2. How is the process different for putting Views into code?

#6

This example explains exactly

This example explains exactly that Stieglitz, just add every exported page into its own file in the pages directory, like the example I give.

#7

I'm a bit confused on step

I'm a bit confused on step number 3.
Where is the variable $page ever loaded?

#8

Nevermind... I didn't look

Nevermind... I didn't look close enough. The include grabs the $page from the .inc file.

Languages

Contact