diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/menu/menu.api.php | 72 | ||||
-rw-r--r-- | modules/simpletest/tests/menu.test | 15 | ||||
-rw-r--r-- | modules/simpletest/tests/menu_test.module | 46 |
3 files changed, 133 insertions, 0 deletions
diff --git a/modules/menu/menu.api.php b/modules/menu/menu.api.php index bf37caae3..99fcb8e20 100644 --- a/modules/menu/menu.api.php +++ b/modules/menu/menu.api.php @@ -147,6 +147,78 @@ function hook_translated_menu_link_alter(&$item, $map) { } } + /** + * Inform modules that a menu link has been created. + * + * This hook is used to notify module that menu items have been + * created. Contributed modules may use the information to perform + * actions based on the information entered into the menu system. + * + * @param $link + * The $link record saved into the {menu_links} table. + * @return + * None. + * + * @see hook_menu_link_update() + * @see hook_menu_link_delete() + */ +function hook_menu_link_insert($link) { + // In our sample case, we track menu items as editing sections + // of the site. These are stored in our table as 'disabled' items. + $record['mlid'] = $link['mlid']; + $record['menu_name'] = $link['menu_name']; + $record['status'] = 0; + drupal_write_record('menu_example', $record); +} + +/** + * Inform modules that a menu link has been updated. + * + * This hook is used to notify module that menu items have been + * updated. Contributed modules may use the information to perform + * actions based on the information entered into the menu system. + * + * @param $link + * The $link record saved into the {menu_links} table. + * @return + * None. + * + * @see hook_menu_link_insert() + * @see hook_menu_link_delete() + */ +function hook_menu_link_update($link) { + // If the parent menu has changed, update our record. + $menu_name = db_result(db_query("SELECT mlid, menu_name, status FROM {menu_example} WHERE mlid = :mlid", array(':mlid' => $link['mlid']))); + if ($menu_name != $link['menu_name']) { + db_update('menu_example') + ->fields(array('menu_name' => $link['menu_name'])) + ->condition('mlid', $link['mlid']) + ->execute(); + } +} + +/** + * Inform modules that a menu link has been deleted. + * + * This hook is used to notify module that menu items have been + * deleted. Contributed modules may use the information to perform + * actions based on the information entered into the menu system. + * + * @param $link + * The $link record saved into the {menu_links} table. + * @return + * None. + * + * @see hook_menu_link_insert() + * @see hook_menu_link_update() + */ +function hook_menu_link_delete($link) { + // Delete the record from our table. + db_delete('menu_example') + ->condition('mlid', $link['mlid']) + ->execute(); +} + /** * @} End of "addtogroup hooks". */ diff --git a/modules/simpletest/tests/menu.test b/modules/simpletest/tests/menu.test index 889d877d3..26443f74e 100644 --- a/modules/simpletest/tests/menu.test +++ b/modules/simpletest/tests/menu.test @@ -132,6 +132,21 @@ class MenuIncTestCase extends DrupalWebTestCase { $this->assertEqual($compare_item, $item, t('Modified menu item is equal to newly retrieved menu item.'), 'menu'); } + /** + * Test menu maintainance hooks. + */ + function testMenuItemHooks() { + // Create an item. + menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/4', 'Menu link #4'); + $this->assertEqual(menu_test_static_variable(), 'insert', t('hook_menu_link_insert() fired correctly')); + // Update the item. + menu_link_maintain('menu_test', 'update', 'menu_test_maintain/4', 'Menu link updated'); + $this->assertEqual(menu_test_static_variable(), 'update', t('hook_menu_link_update() fired correctly')); + // Delete the item. + menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/4', ''); + $this->assertEqual(menu_test_static_variable(), 'delete', t('hook_menu_link_delete() fired correctly')); + } + } /** diff --git a/modules/simpletest/tests/menu_test.module b/modules/simpletest/tests/menu_test.module index 0428fd763..15ecea71a 100644 --- a/modules/simpletest/tests/menu_test.module +++ b/modules/simpletest/tests/menu_test.module @@ -73,3 +73,49 @@ function menu_test_menu_name($new_name = '') { } return $name; } + +/** + * Implement hook_menu_link_insert(). + * + * @return + * A random string. + */ +function menu_test_menu_link_insert($item) { + menu_test_static_variable('insert'); +} + +/** + * Implement hook_menu_link_update(). + * + * @return + * A random string. + */ +function menu_test_menu_link_update($item) { + menu_test_static_variable('update'); +} + +/** + * Implement hook_menu_link_delete(). + * + * @return + * A random string. + */ +function menu_test_menu_link_delete($item) { + menu_test_static_variable('delete'); +} + +/** + * Static function for testing hook results. + * + * @param $value + * The value to set or NULL to return the current value. + * @return + * A text string for comparison to test assertions. + */ +function menu_test_static_variable($value = NULL) { + static $variable; + if (!empty($value)) { + $variable = $value; + } + return $variable; +} |