Example scaffolding

Before we dive into the details of the Forms API, we need a little scaffolding code to enable you to test the examples on a site. Here we map the URL example/page to the function call example_page(). We'll later fill this function with relevant code.

  1. function example_menu($may_cache) {
  2.   if ($may_cache) {
  3.     $items = array();
  4.     $items[] = array(
  5.       'path' => 'example/page',
  6.       'title' => 'Example',
  7.       'callback' => 'example_page',
  8.       'access' => user_access('access example'),
  9.       'type' => MENU_CALLBACK,
  10.     );
  11.     return $items;
  12.   }
  13. }
  14.  
  15. function example_perm() {
  16.   return array('access example');
  17. }
  18.  
  19. /**
  20.   * This function will be called upon a visit to example/page.
  21.   */
  22. function example_page() {
  23.  
  24. }

The example implements the following hooks:

hook_menu
Maps URLs to callback functions in addition to providing a way to define actual entries in the navigation menu.
hook_perm
Supplies permissions that can be assigned on the admin/users/access page and checked with user_access().

To use the example code on your site, you can save it in sites/all/modules/example/example.module together with example.info:

  1. name = Example
  2. description = Toy with FAPI examples.

Average: 5 (2 votes)