Drupal: Minimum Hooks to Develop Your Own Module (English)

Obviously there is a learning curve to overcome in order to understand a concept of a world of a Drupal's hook system, that is, a.k.a callbacks. The following is a summary of hooks both for a management system and a form system, which I organized.


Hooks for a Management

  • hook_help($section)

    The implementation of hook_help is also recommended even though those are described in yourmodule.info since Drupal 5.x

  • hook_perm()

    A set of three hooks such as hook_perm, hook_access and hook_menu should be always implemented together. It must be implemented in order to set access controls in every single user group at Home > Administer > Access Control

  • hook_access($op, $node)

    Describes which access privilege is bound to the corresponding operation, which is an argument of this function, such as $op == 'create'、$op == 'update'、$op == 'delete', and etc.

  • hook_menu($may_cache)

    Maps a set of the path, module, privilege and page tile in this hook.

    'type' => MENU_NORMAL_ITEM enables the item to show in the menu system

    'type' => MENU_CALLBACK disables the item to show in the menu system and only to call the mapped function with the path.

  • hook_node_info()

    The following description enables two forms in one module. The significant lines are 'module' => 'form1' and 'module' => 'form1'; eventually they turn out the function name such as form1_view, form2_view which are used as the prefix such as form1_ or form2_ with hooks for forms on the later explanation.

    <?php

    function hook_node_info() {
      return array(

       
    'form1' => array(
                
    'name'        => t('form1'),
                
    'module'      => 'form1',
                
    'description' => t("Describe the explanation of form1"),
        ),

       
    'form2' => array(
                
    'name'        => t('form2'),
                
    'module'      => 'form2',
                
    'description' => t("Describe the explanation of form2"),
        )
      );
    }

    ?>

  • hook_admin()

    Enables the configurable values in this module to setup under the menu such as Home > Administer > Site Configuration > Your Module Settings

Hooks for a Form
Renders a page or shows a form

  • hook_form(&$node)

    Display a form to input data

  • hook_validate($form_id, $form_values)

    Allows the inputted values to check when a submit button is pressed; you can make an error page show when the valus are invalid strings or out of the range by calling form_set_error('Write the field name of the item here', t('Write an error description here'));

  • hook_submit($node)

    Called before hook_insert is called when a submit button is pressed. And also allows $node that should be passed to hook_insert to alter

  • hook_insert($node)

    Receives values from an inputting form (that is, just a 'form') and stores a record (that is, a node) into the database. Virtually, An SQL statement 'INSERT' will be issued in this hook. As a result, a new node will be added into the database.

  • hook_update($node)

    Receives values from an edit form (that is, just a 'form') and updates a record (that is, a node) in the database. Virtually, An SQL statement 'UPDATE' will be issued in this hook.

  • hook_delete(&$node)

    Called when a Delete button is pressed as the same as an edit form (that is, just a 'form'). A node stored in a database is deleted. Virtually, An SQL statement 'DELETE' will be issued in this hook.

  • hook_load($node)

    In Drupal, each document created by a user is stored into 'node' table with a unit 'node'. If you add a custom field into 'node', you must explicitly specify your custom field and load the node when your module is about to show the node. The code is the following.

    <?php

    function hook_load($node) {

     
    $r = db_fetch_object(
              
    db_query('SELECT `your_id`, `your_field1`, `your_field2`, ... FROM {your_table_name} WHERE nid = %d', $node->nid)
            );

      return
    $r;
    }

    ?>

  • hook_prepare(&$node)

    Allows node vales to edit before calling hook_view. For example, if you show a date formatted to mm/dd/yyyy before calling hook_view, you can make the date format in this funciton.

  • hook_view(&$node, $teaser = FALSE, $page = FALSE)

    Display a node. If you implement 'yourmodule_prepare', it should be called at the beginning of this hook implementation.

    <?php

    function hook_view(&$node, $teaser = FALSE, $page = FALSE) {

     
    $node = node_prepare($node, $teaser);
     
    yourmodule_prepare($node);

      ...

    }

    ?>

トラックバック URL: https://perltips.twinkle.cc/trackback/199
Posted on 2007-05-03 by yas |
Drupal: Develop a Minimum Function Set of Module (English)
Trackback from Perl Tips: Understanding the development of Drupal......
Posted by Perl Tips (未認証ユーザ) on 2007/04/21(土) 20:38
Drupal で、必要最低限の機能のモジュールを作る。
Trackback from Perl Tips: Drupal でのモジュール開発を理解するということは node のコンセプト......
Posted by Perl Tips (未認証ユーザ) on 2007/04/21(土) 20:41
Drupal で、モジュールを開発するために必要な最低限の hook
Trackback from Perl Tips: Drupal の hook (つまりコールバック/Callback)は、その世......
Posted by Perl Tips (未認証ユーザ) on 2007/04/21(土) 20:43