Latest Tweets

Adding views, panels and handlers in a custom module

In the drupal development process we always we need to create a custom module, a lot of views and pages. Now i want to explain how manage the includes of this views, pages and handlers without create giant files with thousands code lines.

In this example my module name is my_module

This is the content of our module folder:


Selection_005.png


1. First we need our my_module.info file

; $Id: my_module.info,v 1.0.0.0 2010/12/06 10:11:15 myWebsite.net Exp $
name = "My module"
description = "My example module"
core = "7.x"
package = "Custom modules"

2. Now we create my_module.module, here we need communicate to Views module where the default views are. And we do the same with the panels. Finally we add a function which process a given folder.

function my_module_views_api() {
  return array(
      'api' => 2,
      'path' => drupal_get_path('module', 'my_module') . '/includes'
  );
}

function my_module_panels_ctools_plugin_api($module, $api) {
  if ($module == 'page_manager' && $api == 'pages_default') {
    return array(
      'version' => 1,
      'path' => drupal_get_path('module', 'my_module') . '/includes',
    );
  }
}

function process_includes($path, $name) {
  $items = array();
  $files = glob($path."*.inc");
  foreach($files as $file) {
    include_once $file;
    $items[$$name->name] = $$name;
   }
  return $items;
}

3. In my_module.views_default.inc we implement hook_views_default_views()

function ignite_views_default_views() {
  $path = drupal_get_path('module', 'summa_panels') . '/includes/views/';
  return process_includes($path, "view");
}

4. In my_module.pages_default.inc we implement hook_default_page_manager_pages() and hook_default_page_manager_handlers()

 

function summa_panels_default_page_manager_pages() {
  $path = drupal_get_path('module', 'summa_panels') . '/includes/pages/';
  return process_includes($path, "page");
}

function summa_panels_default_page_manager_handlers() {
  $path = drupal_get_path('module', 'summa_panels') . '/includes/handlers/';
  return process_includes($path, "handler");
}

5. When you export a view, page or a handler you put the code in their respective folder with *.inc extension

And that is all, now we have all our includes in their place

Author

PHP Developer
I'm a young developer from Argentina, since I was a child I have a computer under my control. I am allways searching for new challenges, and learning new technologies. My specialty is drupal and...

Comments

Wow! very useful articles. I write the things I'd like to read about. If anyone else likes it, that's a nice bonus. <a href="http://www.tulleeho.org">alchemist</a> www.tulleeho.org

Add comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.