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

CHAPTER 15 WORKING WITH TAXONOMY

taxonomy_term_data: This table contains the actual name of the term, which vocabulary it’s in, its optional description, and the weight that determines its position in lists of terms presented to the user for term selection (for example, on the node submit form).

taxonomy_term_hierarchy: The taxonomy_term_hierarchy table contains the term ID of a term as well as the term ID of its parent. If a term is at the root (that is, it has no parent), the ID of the parent is 0.

field_data_field_xxxxx: The table used to store related term information where xxxx is the name of the field defined for a specific vocabulary.

Module-Based Vocabularies

In addition to the vocabularies that can be created using Admin -> Structure -> Taxonomy -> Add, modules can use the taxonomy tables to store their own vocabularies. For example, the forum module uses the taxonomy tables to keep a vocabulary of containers and forums.

The module that owns a vocabulary is identified in the module column of the taxonomy_vocabulary table. Normally, this column will contain taxonomy, because the taxonomy module manages most vocabularies.

Creating a Module-Based Vocabulary

Let’s look at an example of a module-based vocabulary. The Forums module uses taxonomy as a mechanism for organizing forum topics. It creates its vocabulary programmatically (see: modules/forum/forum.install), as shown in the following example, and assumes ownership of the vocabulary by setting the module key of the $vocabulary array to the module name (without .module).

// Create the forum vocabulary if it does not exist.

$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0)); if (!$vocabulary) {

$edit = array(

'name' => t('Forums'), 'machine_name' => 'forums',

'description' => t('Forum navigation vocabulary'), 'hierarchy' => 1,

'module' => 'forum', 'weight' => -10,

);

$vocabulary = (object) $edit; taxonomy_vocabulary_save($vocabulary); variable_set('forum_nav_vocabulary', $vocabulary->vid);

}

Keeping Informed of Vocabulary Changes with Taxonomy Hooks

If you do keep a vocabulary for your own module, you’ll want to be informed of any changes that are made to the vocabulary through the standard Taxonomy user interface. You might also want to be

352

CHAPTER 15 WORKING WITH TAXONOMY

informed when a change is made to an existing vocabulary maintained by taxonomy.module. In either case, you can be informed of changes to vocabularies by implementing taxonomy hooks. The following module has an implementation of taxonomy hooks that keeps you informed of vocabulary changes by e- mail. Here’s the taxonomymonitor.info file:

name = Taxonomy Monitor

description = Sends email to notify of changes to taxonomy vocabularies. package = Pro Drupal Development

dependencies[] = taxonomy core = 7.x

files[] = taxonomymonitor.module

Here’s taxonomymonitor.module:

<?php

/**

*@file

*A module that emails a person when taxonomy changes

*/

/**

* Implements hook_term_insert() */

function taxonomymonitor_term_insert($term) { _send_notification('term', 'added', $term->name);

}

/**

* Implements hook_term_update() */

function taxonomymonitor_term_update($term) { _send_notification('term', 'updated', $term->name);

}

/**

* Implements hook_term_delete() */

function taxonomymonitor_term_delete($term) { _send_notification('term', 'deleted', $term->name);

}

/**

* Implements hook_vocabulary_insert() */

function taxonomymonitor_vocabulary_insert($vocabulary) { _send_notification('vocabulary', 'added', $vocabulary->name);

}

353

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