Created
July 30, 2014 07:55
-
-
Save msankhala/0e815f2b52664e9fdd81 to your computer and use it in GitHub Desktop.
Continue with Drupal menu system.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example: Exportables | |
//Here’s a simple example illustrating collecting exportables using aggregate hooks and static variables - | |
<?php | |
function my_module_data_get_all() { | |
$data = &drupal_static(__FUNCTION__); | |
// check for existing $data from previous function call | |
if (!isset($data)) { | |
// pull all $data from database | |
$data = db_select('my_table', 'mt')->fields('mt')->fetchAll(); | |
// cycle through all code-based hook_default_my_data implementations | |
// and add to database entries | |
foreach (module_implements('default_my_data') as $module) { | |
// combine $data with returned array (db takes precedence) | |
$data = array_merge(module_invoke($module, 'default_my_data'), $data); | |
} | |
} | |
return $data; | |
} | |
?> | |
//This method can then be used when listing all data from a menu callback - | |
<?php | |
$items['my-module'] = array( | |
'title' => 'List all data', | |
'page callback' => 'my_module_data_view', | |
); | |
$items['my-module/%my_module_data'] = array( | |
'title callback' => 'my_module_data_title', | |
'title arguments' => array(1), | |
'page callback' => 'my_module_data_view', | |
'page arguments' => array(1), | |
); | |
$items['my-module/%my_module_data/view'] = array( | |
'title' => 'View', | |
'type' => MENU_DEFAULT_LOCAL_TASK, | |
'weight' => -10, | |
); | |
$items['my-module/%my_module_data/export'] = array( | |
'title' => 'Export', | |
'page callback' => 'my_module_data_export', | |
'page arguments' => array(1), | |
'type' => MENU_LOCAL_TASK, | |
); | |
?> | |
<?php | |
// menu load function | |
function my_module_data_load($my_data) { | |
$data = my_module_data_get_all(); | |
return isset($data[$my_data]) ? $data[$my_data] : NULL; | |
} | |
// menu title callback | |
function my_module_data_title($my_data, $op = 'view') { | |
return is_object($my_data) && isset($my_data->value) ? ucwords($op) . $my_data->value : ''; | |
} | |
?> | |
//Everything is now loaded and passed with full objects to our page callbacks. Our page callback can now be as simple as a theme callback - | |
<?php | |
// view page callback | |
function my_module_data_view($my_data = NULL) { | |
return theme('my_data', array('data' => $my_data)); | |
} | |
// simple export page callback | |
function my_module_data_export($my_data) { | |
return var_export($my_data, TRUE); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment