Drupal Study Notes-1. Getting started with modules

Source: Internet
Author: User
Tags drupal
Document directory
  • Module description file-x.info
  • Module implementation file-×. Module
  • Module Installation File-X. Install
Drupal module installation path

The module in Drupal can be installed in three paths

  1. The modules under the root directory are all Drupal system modules. It is better to avoid the trouble of upgrading.
  2. /Sites/All/modules. The modules under this directory can be shared by all sites, and public service modules can be placed here.
  3. /Sites/www.somesite.com/modules, the modules under this directory can only be used by the site, private service modules can be placed here.
Description of components of the Drupal module-x.info

Info is a plain text file using a simple key = value data structure, similar to commonly used INI files in windows. the file contains the module name, function introduction, Drupal version, module dependency, and other running information. the detailed structure is as follows:

  • Name = Module name
  • Description = function Overview
  • Core = required Drupal version (commonly used 5.x/6.x)
  • Package = group of modules
  • PHP = required PHP version
  • Dependencies [] = dependent Module 1
  • Dependencies [] = dependent module 2
  • ..
  • Dependencies [] = dependent module n

Code example annotate.info

Name = annotate <br/> description = allow user to annotate nodes <br/> core = 6.x< br/> package = druapl Study 

Module implementation file-×. Module

Module is a standard PHP file, and its content is basically to implement various hooks of druapl. Hook is the core structure of Drupal and also the foundation of its scalability. In Drupal, Hook is omnipotent everywhere, the functions of most modules are implemented through various hooks. common hooks are:

  • Menu_hook, which is usually used to display the Module User Interface or configuration interface.
  • Nodeapi_hook: All content types in Drupal are node, so it is essential to process and process the content.

 

Code example annotate. Module

/** <Br/> * Implementation of hook_menu (). <br/> */<br/> function annotate_menu () {<br/> $ items ['admin/annotate'] = array (<br/> 'title' => 'node annotation ', <br/> 'description' => 'adjust node annotation options. ', <br/> 'position' => 'right', <br/> 'weight' =>-5, <br/> 'page callback' => 'System _ admin_menu_block_page ', <br/> 'Access arguments' => array ('administrator site configuration'), <br /> 'File' => 'System. admin. INC ', <br/> 'file path' => drupal_get_path ('module', 'system'), <br/> ); </P> <p> $ items ['admin/annotate/settings'] = array (<br/> 'title' => 'annotation settings ', <br/> 'description' => 'change how annotations behave ', <br/> 'page callback' => 'drupal _ get_form ', <br/> 'page arguments '=> array ('annotate _ admin_settings'), <br/> 'Access arguments' => array ('administer sit E configuration '), <br/> 'type' => menu_normal_item, <br/> 'file' => 'annotate. admin. INC ', <br/>); <br/> return $ items; <br/>}</P> <p>/** <br/> * Implementation of hook_nodeapi (). <br/> */<br/> function annotate_nodeapi (& $ node, $ op, $ teaser, $ page) {<br/> global $ user; <br/> switch ($ OP) {<br/> // The 'view' operation means the node is about to be displayed. <br/> case 'view': <br/> // abort if The user is an anonymous (not logged in) or <br/> // If the node is not being displayed on a page by itself <br/> // (for example, it cocould be in a node listing or search result ). <br/> if ($ user-> uid = 0 |! $ Page) {<br/> break; <br/>}</P> <p> // find out which node types we shocould annotate. <br/> $ types_to_annotate = variable_get ('annotate _ node_types ', array ('page ')); </P> <p> // abort if the node is not on of the types we shoshould annotate. <br/> If (! In_array ($ node-> type, $ types_to_annotate) {<br/> break; <br/>}</P> <p> // get the current annotation for this node from the database <br/> // and store it in the Node object <br/> $ result = db_query ('select note from {annotations} Where nid = % d and uid = % d ', <br/> $ node-> NID, $ user-> UID); <br/> $ node-> annotation = db_result ($ result ); </P> <p> // Add our form as node item. <br/> $ node-> content ['annotate _ form'] = array (<br/> '# value' => drupal_get_form ('annotate _ entry_form ', $ node), <br/> '# weight' => 10, <br/>); </P> <p> break; </P> <p> case 'delete': <br/> db_query ('delete from {annotations} Where nid = % d', $ node-> NID ); <br/> break; <br/>}< br/>}

 

Module Installation File-X. Install

The Install file must implement the two hooks required for installing and uninstalling the module, hook_install and hook_uninstall. If there is a database change, you must implement hook_schema, it is worth noting that druapl in hook_schema does not support using DDL directly to operate databases, but uses a custom table schema model to support multiple databases.

 

Code example annotate. Install

/** <Br/> * Implementation for hook_install (). <br/> */<br/> function annotate_install () {<br/> // use schema API to create database table. <br/> drupal_install_schema ('annotate'); <br/>}</P> <p>/** <br/> * Implementation for hook_uninstall (). <br/> */<br/> function annotate_uninstall () {<br/> // use schema API to delete database table. <br/> drupal_uninstall_schema ('annotate'); <br/> // delete our module's variable from the variables table. <br/> variable_del ('annotate _ node_types '); <br/> variable_del ('annotate _ limit_per_node'); <br/> variable_del ('annotate _ deletion '); <br/>}</P> <p>/** <br/> * Implementation for hook_schema (). <br/> */<br/> function annotate_schema () {<br/> $ schema ['annotations '] = array (<br/> 'description' => T ('stores node annotations that users write. '), <br/> 'fields' => array (<br/> 'nid' => array (<br/> 'type' => 'int ', <br/> 'unsigne' => true, <br/> 'not null' => true, <br/> 'default' => 0, <br/> 'description' => T ('the {node }. NID to which the annotation applies. '), <br/>), <br/> 'uid' => array (<br/> 'type' => 'int ', <br/> 'unsigne' => true, <br/> 'not null' => true, <br/> 'default' => 0, <br/> 'description' => T ('the {user }. UID of the user who created the annotation. ') <br/>), <br/> 'note' => array (<br/> 'description' => T ('The text of the annotation. '), <br/> 'type' => 'text', <br/> 'not null' => true, <br/> 'SIZE' => 'Big '<br/> ), <br/> 'created '=> array (<br/> 'description' => T ('a UNIX timestamp indicating when the annotation was created. '), <br/> 'type' => 'int', <br/> 'not null' => true, <br/> 'default' => 0 <br/>), <br/> ), <br/> 'Primary key' => array (<br/> 'nid', 'uid' <br/>), <br/> ); <br/> return $ Schema; <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.