/[drupal]/drupal/modules/menu/menu.api.php
ViewVC logotype

Contents of /drupal/modules/menu/menu.api.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.19 - (show annotations) (download) (as text)
Sat Oct 17 11:39:15 2009 UTC (6 weeks ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.18: +8 -7 lines
File MIME type: text/x-php
- Patch #473268 by sun: documentation improvements for edit in place.
1 <?php
2 // $Id: menu.api.php,v 1.18 2009/10/17 05:50:28 webchick Exp $
3
4 /**
5 * @file
6 * Hooks provided by the Menu module.
7 */
8
9 /**
10 * @addtogroup hooks
11 * @{
12 */
13
14 /**
15 * Define menu items and page callbacks.
16 *
17 * This hook enables modules to register paths in order to define how URL
18 * requests are handled. Paths may be registered for URL handling only, but can
19 * also register a link to be placed in a menu (usually the Navigation menu). A
20 * path and its associated information is commonly called a "menu router item".
21 *
22 * hook_menu() implementations return an associative array whose keys define
23 * paths and whose values are an associative array defining properties for each
24 * path. The definition for each path may refer to a callback function that is
25 * invoked when the registered path is requested. In case there is no other
26 * registered path that fits the requested path better, any further path
27 * components are optionally passed to the callback function by default.
28 * For example, when registering the path 'abc/def':
29 * @code
30 * function mymodule_menu() {
31 * $items['abc/def'] = array(
32 * 'page callback' => 'mymodule_abc_view',
33 * );
34 * }
35 *
36 * function mymodule_abc_view($ghi = 0, $jkl = '') {
37 * // ...
38 * }
39 * @endcode
40 * When the path 'abc/def' was requested, then no further arguments would be
41 * passed to the callback function, so $ghi and $jkl would take the default
42 * values as defined in the function signature.
43 * In case 'abc/def/123/foo' was requested, then $ghi would be '123' and $jkl
44 * would be 'foo'.
45 *
46 * In addition to optional path arguments, the definition for each path may
47 * specify a list of arguments for each callback function as an array. These
48 * argument lists may contain fixed/hard-coded argument values, but may also
49 * contain integers that correspond to path components. When integers are used
50 * and the callback function is called, the corresponding path components will
51 * be substituted. For example:
52 * @code
53 * function mymodule_menu() {
54 * $items['abc/def'] = array(
55 * 'page callback' => 'mymodule_abc_view',
56 * 'page arguments' => array(1, 'foo'),
57 * );
58 * }
59 * @endcode
60 * When the path 'abc/def' was requested, the callback function would get 'def'
61 * as first argument and (always) 'foo' as second argument.
62 * The integer 1 in an argument list would be replaced with 'def' and integer 0
63 * would be replaced with 'abc', i.e. path components are counted from zero.
64 * This allows to re-use a callback function for several different paths.
65 *
66 * Arguments may also be used to replace wildcards within paths. For example,
67 * when registering the path 'my-module/%/edit':
68 * @code
69 * $items['my-module/%/edit'] = array(
70 * 'page callback' => 'mymodule_abc_edit',
71 * 'page arguments' => array(1),
72 * );
73 * @endcode
74 * When the path 'my-module/foo/edit' is requested, then integer 1 will be
75 * replaced with 'foo' and passed to the callback function.
76 *
77 * Registered paths may also contain special "auto-loader" wildcard components
78 * in the form of '%mymodule_abc', where the '%' part means that this path
79 * component is a wildcard, and the 'mymodule_abc' part defines the prefix for a
80 * menu argument loader function, which here would be mymodule_abc_load().
81 * For example, when registering the path 'my-module/%mymodule_abc/edit':
82 * @code
83 * $items['my-module/%mymodule_abc/edit'] = array(
84 * 'page callback' => 'mymodule_abc_edit',
85 * 'page arguments' => array(1),
86 * );
87 * @endcode
88 * When the path 'my-module/123/edit' is requested, then the argument loader
89 * function mymodule_abc_load() will be invoked with the argument '123', and it
90 * is supposed to take that value to load and return data for "abc" having the
91 * internal id 123:
92 * @code
93 * function mymodule_abc_load($abc_id) {
94 * return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();
95 * }
96 * @endcode
97 * The returned data of the argument loader will be passed in place of the
98 * original path component to all callback functions referring to that (integer)
99 * component in their argument list.
100 * Menu argument loader functions may also be passed additional arguments; see
101 * "load arguments" below.
102 *
103 * If a registered path defines an argument list, then those defined arguments
104 * will always be passed first to the callback function. In case there are any
105 * further components contained in the requested path, then those will always
106 * come last.
107 *
108 * Special care should be taken for the page callback drupal_get_form(), because
109 * the callback function will always receive $form and &$form_state as the very
110 * first function arguments:
111 * @code
112 * function mymodule_abc_form($form, &$form_state) {
113 * // ...
114 * return $form;
115 * }
116 * @endcode
117 * See @link form_api Form API documentation @endlink for details.
118 *
119 *
120 * You can also make groups of menu items to be rendered (by default) as tabs
121 * on a page. To do that, first create one menu item of type MENU_NORMAL_ITEM,
122 * with your chosen path, such as 'foo'. Then duplicate that menu item, using a
123 * subdirectory path, such as 'foo/tab1', and changing the type to
124 * MENU_DEFAULT_LOCAL_TASK to make it the default tab for the group. Then add
125 * the additional tab items, with paths such as "foo/tab2" etc., with type
126 * MENU_LOCAL_TASK. Example:
127 * @code
128 * // This will make "Foo settings" appear on the admin Config page
129 * $items['admin/config/foo'] = array(
130 * 'title' => 'Foo settings',
131 * 'type' => MENU_NORMAL_ITEM,
132 * // page callback, etc. need to be added here
133 * );
134 * // When you go to "Foo settings", "Global settings" will be the main tab
135 * $items['admin/config/foo/global'] = array(
136 * 'title' => 'Global settings',
137 * 'type' => MENU_DEFAULT_LOCAL_TASK,
138 * // page callback, etc. need to be added here
139 * );
140 * // Make an additional tab called "Node settings"
141 * $items['admin/config/foo/node'] = array(
142 * 'title' => 'Node settings',
143 * 'type' => MENU_LOCAL_TASK,
144 * // page callback, etc. need to be added here
145 * );
146 * @endcode
147 *
148 * This hook is rarely called (for example, when modules are enabled), and
149 * its results are cached in the database.
150 *
151 * @return
152 * An array of menu items. Each menu item has a key corresponding to the
153 * Drupal path being registered. The corresponding array value is an
154 * associative array that may contain the following key-value pairs:
155 * - "title": Required. The untranslated title of the menu item.
156 * - "title callback": Function to generate the title; defaults to t().
157 * If you require only the raw string to be output, set this to FALSE.
158 * - "title arguments": Arguments to send to t() or your custom callback,
159 * with path component substitution as described above.
160 * - "description": The untranslated description of the menu item.
161 * - "page callback": The function to call to display a web page when the user
162 * visits the path. If omitted, the parent menu item's callback will be used
163 * instead.
164 * - "page arguments": An array of arguments to pass to the page callback
165 * function, with path component substitution as described above.
166 * - "delivery callback": The function to call to package the result of the
167 * page callback function and send it to the browser. Defaults to
168 * drupal_deliver_html_page() unless a value is inherited from a parent menu
169 * item.
170 * - "access callback": A function returning a boolean value that determines
171 * whether the user has access rights to this menu item. Defaults to
172 * user_access() unless a value is inherited from a parent menu item.
173 * - "access arguments": An array of arguments to pass to the access callback
174 * function, with path component substitution as described above.
175 * - "theme callback": Optional. A function returning the machine-readable
176 * name of the theme that will be used to render the page. If the function
177 * returns nothing, the main site theme will be used. If no function is
178 * provided, the main site theme will also be used, unless a value is
179 * inherited from a parent menu item.
180 * - "theme arguments": An array of arguments to pass to the theme callback
181 * function, with path component substitution as described above.
182 * - "file": A file that will be included before the callbacks are accessed;
183 * this allows callback functions to be in separate files. The file should
184 * be relative to the implementing module's directory unless otherwise
185 * specified by the "file path" option. Note: This does not apply to the
186 * 'access callback'.
187 * - "file path": The path to the folder containing the file specified in
188 * "file". This defaults to the path to the module implementing the hook.
189 * - "load arguments": An array of arguments to be passed to each of the
190 * wildcard object loaders in the path. For example, for the path
191 * node/%node/revisions/%/view, a "load arguments" value of array(1, 3) will
192 * call node_load() with the second and fourth path components passed in (as
193 * described above, integers are automatically replaced with path
194 * components). There are also two "magic" values: "%index" will correspond
195 * to the index of the wildcard path component, and "%map" will correspond
196 * to the full menu map, passed in by reference.
197 * - "weight": An integer that determines the relative position of items in
198 * the menu; higher-weighted items sink. Defaults to 0. When in doubt, leave
199 * this alone; the default alphabetical order is usually best.
200 * - "menu_name": Optional. Set this to a custom menu if you don't want your
201 * item to be placed in Navigation.
202 * - "context": (optional) Defines the context a tab may appear in. By
203 * default, all tabs are only displayed as local tasks when being rendered
204 * in a page context. All tabs that should be accessible as contextual links
205 * in page region containers outside of the parent menu item's primary page
206 * context should be registered using one of the following contexts:
207 * - MENU_CONTEXT_PAGE: (default) The tab is displayed as local task for the
208 * page context only.
209 * - MENU_CONTEXT_INLINE: The tab is displayed as contextual link outside of
210 * the primary page context only.
211 * Contexts can be combined. For example, to display a tab both on a page
212 * and inline, a menu router item may specify:
213 * @code
214 * 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
215 * @endcode
216 * - "tab_parent": For local task menu items, the path of the task's parent
217 * item; defaults to the same path without the last component (e.g., the
218 * default parent for 'admin/people/create' is 'admin/people').
219 * - "tab_root": For local task menu items, the path of the closest non-tab
220 * item; same default as "tab_parent".
221 * - "block callback": Name of a function used to render the block on the
222 * system administration page for this item (called with no arguments).
223 * If not provided, system_admin_menu_block() is used to generate it.
224 * - "position": Position of the block ('left' or 'right') on the system
225 * administration page for this item.
226 * - "type": A bitmask of flags describing properties of the menu item.
227 * Many shortcut bitmasks are provided as constants in menu.inc:
228 * - MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be
229 * moved/hidden by the administrator.
230 * - MENU_CALLBACK: Callbacks simply register a path so that the correct
231 * function is fired when the path is accessed.
232 * - MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the
233 * administrator may enable.
234 * - MENU_LOCAL_TASK: Local tasks are rendered as tabs by default.
235 * - MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one
236 * "default" task, that links to the same path as its parent when clicked.
237 * If the "type" element is omitted, MENU_NORMAL_ITEM is assumed.
238 *
239 * For a detailed usage example, see page_example.module.
240 * For comprehensive documentation on the menu system, see
241 * http://drupal.org/node/102338.
242 */
243 function hook_menu() {
244 $items['blog'] = array(
245 'title' => 'blogs',
246 'page callback' => 'blog_page',
247 'access arguments' => array('access content'),
248 'type' => MENU_SUGGESTED_ITEM,
249 );
250 $items['blog/feed'] = array(
251 'title' => 'RSS feed',
252 'page callback' => 'blog_feed',
253 'access arguments' => array('access content'),
254 'type' => MENU_CALLBACK,
255 );
256
257 return $items;
258 }
259
260 /**
261 * Alter the data being saved to the {menu_router} table after hook_menu is invoked.
262 *
263 * This hook is invoked by menu_router_build(). The menu definitions are passed
264 * in by reference. Each element of the $items array is one item returned
265 * by a module from hook_menu. Additional items may be added, or existing items
266 * altered.
267 *
268 * @param $items
269 * Associative array of menu router definitions returned from hook_menu().
270 */
271 function hook_menu_alter(&$items) {
272 // Example - disable the page at node/add
273 $items['node/add']['access callback'] = FALSE;
274 }
275
276 /**
277 * Alter the data being saved to the {menu_links} table by menu_link_save().
278 *
279 * @param $item
280 * Associative array defining a menu link as passed into menu_link_save().
281 */
282 function hook_menu_link_alter(&$item) {
283 // Example 1 - make all new admin links hidden (a.k.a disabled).
284 if (strpos($item['link_path'], 'admin') === 0 && empty($item['mlid'])) {
285 $item['hidden'] = 1;
286 }
287 // Example 2 - flag a link to be altered by hook_translated_menu_link_alter()
288 if ($item['link_path'] == 'devel/cache/clear') {
289 $item['options']['alter'] = TRUE;
290 }
291 }
292
293 /**
294 * Alter a menu link after it's translated, but before it's rendered.
295 *
296 * This hook may be used, for example, to add a page-specific query string.
297 * For performance reasons, only links that have $item['options']['alter'] == TRUE
298 * will be passed into this hook. The $item['options']['alter'] flag should
299 * generally be set using hook_menu_link_alter().
300 *
301 * @param $item
302 * Associative array defining a menu link after _menu_link_translate()
303 * @param $map
304 * Associative array containing the menu $map (path parts and/or objects).
305 */
306 function hook_translated_menu_link_alter(&$item, $map) {
307 if ($item['href'] == 'devel/cache/clear') {
308 $item['localized_options']['query'] = drupal_get_destination();
309 }
310 }
311
312 /**
313 * Inform modules that a menu link has been created.
314 *
315 * This hook is used to notify modules that menu items have been
316 * created. Contributed modules may use the information to perform
317 * actions based on the information entered into the menu system.
318 *
319 * @param $link
320 * Associative array defining a menu link as passed into menu_link_save().
321 *
322 * @see hook_menu_link_update()
323 * @see hook_menu_link_delete()
324 */
325 function hook_menu_link_insert($link) {
326 // In our sample case, we track menu items as editing sections
327 // of the site. These are stored in our table as 'disabled' items.
328 $record['mlid'] = $link['mlid'];
329 $record['menu_name'] = $link['menu_name'];
330 $record['status'] = 0;
331 drupal_write_record('menu_example', $record);
332 }
333
334 /**
335 * Inform modules that a menu link has been updated.
336 *
337 * This hook is used to notify modules that menu items have been
338 * updated. Contributed modules may use the information to perform
339 * actions based on the information entered into the menu system.
340 *
341 * @param $link
342 * Associative array defining a menu link as passed into menu_link_save().
343 *
344 * @see hook_menu_link_insert()
345 * @see hook_menu_link_delete()
346 */
347 function hook_menu_link_update($link) {
348 // If the parent menu has changed, update our record.
349 $menu_name = db_result(db_query("SELECT mlid, menu_name, status FROM {menu_example} WHERE mlid = :mlid", array(':mlid' => $link['mlid'])));
350 if ($menu_name != $link['menu_name']) {
351 db_update('menu_example')
352 ->fields(array('menu_name' => $link['menu_name']))
353 ->condition('mlid', $link['mlid'])
354 ->execute();
355 }
356 }
357
358 /**
359 * Inform modules that a menu link has been deleted.
360 *
361 * This hook is used to notify modules that menu items have been
362 * deleted. Contributed modules may use the information to perform
363 * actions based on the information entered into the menu system.
364 *
365 * @param $link
366 * Associative array defining a menu link as passed into menu_link_save().
367 *
368 * @see hook_menu_link_insert()
369 * @see hook_menu_link_update()
370 */
371 function hook_menu_link_delete($link) {
372 // Delete the record from our table.
373 db_delete('menu_example')
374 ->condition('mlid', $link['mlid'])
375 ->execute();
376 }
377
378 /**
379 * Informs modules that a custom menu was created.
380 *
381 * This hook is used to notify modules that a custom menu has been created.
382 * Contributed modules may use the information to perform actions based on the
383 * information entered into the menu system.
384 *
385 * @param $menu
386 * An array representing a custom menu:
387 * - menu_name: The unique name of the custom menu.
388 * - title: The human readable menu title.
389 * - description: The custom menu description.
390 *
391 * @see hook_menu_update()
392 * @see hook_menu_delete()
393 */
394 function hook_menu_insert($menu) {
395 // For example, we track available menus in a variable.
396 $my_menus = variable_get('my_module_menus', array());
397 $my_menus[$menu['menu_name']] = $menu['menu_name'];
398 variable_set('my_module_menus', $my_menus);
399 }
400
401 /**
402 * Informs modules that a custom menu was updated.
403 *
404 * This hook is used to notify modules that a custom menu has been updated.
405 * Contributed modules may use the information to perform actions based on the
406 * information entered into the menu system.
407 *
408 * @param $menu
409 * An array representing a custom menu:
410 * - menu_name: The unique name of the custom menu.
411 * - title: The human readable menu title.
412 * - description: The custom menu description.
413 * - old_name: The current 'menu_name'. Note that internal menu names cannot
414 * be changed after initial creation.
415 *
416 * @see hook_menu_insert()
417 * @see hook_menu_delete()
418 */
419 function hook_menu_update($menu) {
420 // For example, we track available menus in a variable.
421 $my_menus = variable_get('my_module_menus', array());
422 $my_menus[$menu['menu_name']] = $menu['menu_name'];
423 variable_set('my_module_menus', $my_menus);
424 }
425
426 /**
427 * Informs modules that a custom menu was deleted.
428 *
429 * This hook is used to notify modules that a custom menu along with all links
430 * contained in it (if any) has been deleted. Contributed modules may use the
431 * information to perform actions based on the information entered into the menu
432 * system.
433 *
434 * @param $link
435 * An array representing a custom menu:
436 * - menu_name: The unique name of the custom menu.
437 * - title: The human readable menu title.
438 * - description: The custom menu description.
439 *
440 * @see hook_menu_insert()
441 * @see hook_menu_update()
442 */
443 function hook_menu_delete($menu) {
444 // Delete the record from our variable.
445 $my_menus = variable_get('my_module_menus', array());
446 unset($my_menus[$menu['menu_name']]);
447 variable_set('my_module_menus', $my_menus);
448 }
449
450 /**
451 * Alter tabs and actions displayed on the page before they are rendered.
452 *
453 * This hook is invoked by menu_local_tasks(). The system-determined tabs and
454 * actions are passed in by reference. Additional tabs or actions may be added,
455 * or existing items altered.
456 *
457 * Each tab or action is an associative array containing:
458 * - #theme: The theme function to use to render.
459 * - #link: An associative array containing:
460 * - title: The localized title of the link.
461 * - href: The system path to link to.
462 * - localized_options: An array of options to pass to url().
463 * - #active: Whether the link should be marked as 'active'.
464 *
465 * @param $data
466 * An associative array containing:
467 * - actions: An associative array containing:
468 * - count: The amount of actions determined by the menu system, which can
469 * be ignored.
470 * - output: A list of of actions, each one being an associative array
471 * as described above.
472 * - tabs: An indexed array (list) of tab levels (up to 2 levels), each
473 * containing an associative array:
474 * - count: The amount of tabs determined by the menu system. This value
475 * does not need to be altered if there is more than one tab.
476 * - output: A list of of tabs, each one being an associative array as
477 * described above.
478 */
479 function hook_menu_local_tasks_alter(&$data, $router_item, $root_path) {
480 // Add an action linking to node/add to all pages.
481 $data['actions']['output'][] = array(
482 '#theme' => 'menu_local_task',
483 '#link' => array(
484 'title' => t('Add new content'),
485 'href' => 'node/add',
486 'localized_options' => array(
487 'attributes' => array(
488 'title' => t('Add new content'),
489 ),
490 ),
491 ),
492 );
493
494 // Add a tab linking to node/add to all pages.
495 $data['tabs'][0]['output'][] = array(
496 '#theme' => 'menu_local_task',
497 '#link' => array(
498 'title' => t('Example tab'),
499 'href' => 'node/add',
500 'localized_options' => array(
501 'attributes' => array(
502 'title' => t('Add new content'),
503 ),
504 ),
505 ),
506 // Define whether this link is active. This can be omitted for
507 // implementations that add links to pages outside of the current page
508 // context.
509 '#active' => ($router_item['path'] == $root_path),
510 );
511 }
512
513 /**
514 * Alter contextual links before they are rendered.
515 *
516 * This hook is invoked by menu_contextual_links(). The system-determined
517 * contextual links are passed in by reference. Additional links may be added
518 * or existing links can be altered.
519 *
520 * Each contextual link must at least contain:
521 * - title: The localized title of the link.
522 * - href: The system path to link to.
523 * - localized_options: An array of options to pass to url().
524 *
525 * @param $links
526 * An associative array containing contextual links for the given $root_path,
527 * as described above. The array keys are used to build CSS class names for
528 * contextual links and must therefore be unique for each set of contextual
529 * links.
530 * @param $router_item
531 * The menu router item belonging to the $root_path being requested.
532 * @param $root_path
533 * The (parent) path that has been requested to build contextual links for.
534 * This is a normalized path, which means that an originally passed path of
535 * 'node/123' became 'node/%'.
536 *
537 * @see menu_contextual_links()
538 * @see hook_menu()
539 * @see system_preprocess()
540 */
541 function hook_menu_contextual_links_alter(&$links, $router_item, $root_path) {
542 // Add a link to all contextual links for nodes.
543 if ($root_path == 'node/%') {
544 $links['foo'] = array(
545 'title' => t('Do fu'),
546 'href' => 'foo/do',
547 'localized_options' => array(
548 'query' => array(
549 'foo' => 'bar',
550 ),
551 ),
552 );
553 }
554 }
555
556 /**
557 * @} End of "addtogroup hooks".
558 */

  ViewVC Help
Powered by ViewVC 1.1.2