官术网_书友最值得收藏!

Creating the mysite module to hold our tweaks

In the course of developing our site, we will frequently come across situations where various elements of the site need to be tweaked in PHP using Drupal's APIs. While a lot of theme-specific cases can be stored in template files, certain tweaks which are theme-agnostic require that we store them in a module to ensure that they are available to all themes.

This recipe covers the creation of a module to hold all these bits and pieces.

Getting ready

Create a folder inside sites/all named modules. This is where custom and contributed modules are usually housed.

How to do it...

The following list details the procedure involved in creating a module named mysite to hold our theme-agnostic customizations and other odds and ends:

  1. Create a folder inside sites/all/modules named mysite where mysite refers to the name of our site.
  2. Create a file named mysite.info within the mysite folder.
  3. Edit this file and add the following code inside:
    name = Mysite
    description = A module to hold odds and ends for mysite.
    core = 6.x
    
  4. Save the file.
  5. Create another file named mysite.module which will hold our odds and ends.
  6. Save and exit the editor.
  7. Finally, enable the module via the module administration page at admin/build/modules (Home | Administer | Site building | Modules).

How it works...

Just as with themes, modules require a .info file which provides information to Drupal on compatibility, dependencies, and so on. Once Drupal ascertains that the module is compatible with the version installed, it loads the .module file of the same name and processes it accordingly.

We can test if the module is working by adding a snippet such as the following:

<?php
/**
* Implementation of hook_init().
*/
function mysite_init(){
// Display a message on every page load.
drupal_set_message("Welcome to MySite!");
}

As the comment suggests, the preceding snippet will display a welcome message on every page load.

There's more...

The Drupal community routinely comes up with modules to ease the pain of development.

Module builder

There's a module available named Module builder which can be used to generate a skeleton of a general module. This can subsequently be populated as per our requirements. It is available at http://drupal.org/project/module_builder.

主站蜘蛛池模板: 琼结县| 南投县| 兰考县| 彭州市| 普定县| 卢氏县| 故城县| 中方县| 吴桥县| 科尔| 连江县| 溧水县| 织金县| 定安县| 达州市| 蒙山县| 宁南县| 红安县| 广德县| 洛浦县| 夏邑县| 泽普县| 崇信县| 阳城县| 陇南市| 齐河县| 陈巴尔虎旗| 北海市| 张家界市| 德江县| 剑河县| 钦州市| 汨罗市| 慈利县| 德兴市| 大安市| 松桃| 青阳县| 乌拉特前旗| 历史| 宜兰县|