Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

Download from Wow! eBook <www.wowebook.com>

CHAPTER 2 WRITING A MODULE

'#options' => array( t('Never'), t('Randomly'), t('After 30 days')

)

When PHP gets an array with no keys, it implicitly inserts numeric keys, so internally the array is really as follows:

'#options' => array(

[0]=> t('Never'),

[1]=> t('Randomly'),

[2]=> t('After 30 days')

)

When we set the default value for this field, we use the following, which means, in effect, default to item 0 of the array, which is t('Never').

'#default_value' => variable_get('annotate_deletion', 0) // Default to Never

Retrieving Stored Values with variable_get()

When your module retrieves settings that have been stored, variable_get() should be used:

// Get stored setting of maximum number of annotations per node. $max = variable_get('annotate_limit_per_node', 1);

Note the use of a default value for variable_get() here also, in case no stored values are available (maybe the administrator has not yet visited the settings page).

Further Steps

We’ll be sharing this module with the open source community, naturally, so a README.txt file should be created and placed in the annotations directory alongside the annotate.info, annotate.module, annotate.admin.inc, and annotate.install files. The README.txt file generally contains information about who wrote the module and how to install it. Licensing information need not be included, as all modules uploaded to drupal.org are GPL-licensed and the packaging script on drupal.org will automatically add a LICENSE.txt file. Next, you could upload it to the contributions repository on drupal.org, and create a project page to keep track of feedback from others in the community.

30

CHAPTER 2 WRITING A MODULE

Summary

After reading this chapter, you should be able to perform the following tasks:

Create a Drupal module from scratch.

Understand how to hook into Drupal’s code execution.

Store and retrieve module-specific settings.

Create and process simple forms using Drupal’s forms API.

Create a new administrative category on Drupal’s main administration page.

Define a form for the site administrator to choose options using check boxes, text input fields, and radio buttons.

Validate settings and present an error message if validation fails.

Understand how Drupal stores and retrieves settings using the built-in persistent variable system.

31

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]