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

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

CHAPTER 7 WORKING WITH NODES

tnid: When a node serves as the translated version of another node, the nid of the source node being translated is stored here. For example, if node 3 is in English and node 5 is the same content as node 3 but in Swedish, the tnid field of node 5 will be 3.

translate: A value of 1 indicates that the translation needs to be updated; a value of 0 means translation is up to date.

If you’re using the node revisions system, Drupal will create a revision of the content as well as track who made the last edit.

Not Everything Is a Node

Users, blocks, and comments are not nodes. Each of these specialized data structures has its own hook system geared toward its intended purpose. Nodes (usually) have title and body content, and a data structure representing a user doesn’t need that. Rather, users need an e-mail address, a username, and a safe way to store passwords. Blocks are lightweight storage solutions for smaller pieces of content such as menu navigation, a search box, a list of recent comments, and so on. Comments aren’t nodes either, which keeps them lightweight as well. It’s quite possible to have 100 or more comments per page, and if each of those comments had to go through the node hook system when being loaded, that would be a tremendous performance hit.

In the past, there have been great debates about whether users or comments should be nodes, and some contributed modules actually implement this. Be warned that raising this argument is like shouting “Emacs is better!” at a programming convention.

Creating a Node Module

Traditionally, when you wanted to create a new content type in Drupal, you would write a node module that took responsibility for providing the new and interesting things your content type needed. We say “traditionally” because recent advents within the Drupal framework allow you to create content types within the administrative interface and extend their functionality with contributed modules rather than writing a node module from scratch. I’ll cover both solutions within this chapter.

I’ll write a node module that lets users add a job posting to a site. A job posting node will include a title, a body where the details of the job posting will be entered, and a field where the user can enter the name of the company. For the job posting title and a body, I’ll use the built-in node title and body that are standard with all Drupal nodes. I’ll need to add a new custom field for the company’s name.

I’ll start by creating a folder named job_post in your sites/all/modules/custom directory.

Creating the .install File

The install file for the job post module performs all of the set-up operations for things like defining the node type, creating the fields that make up our new node type, and handling the uninstall process when an administrator uninstalls the module.

140

CHAPTER 7 WORKING WITH NODES

<?php

/**

*@file

*Install file for Job Post module.

*/

/**

*Implements hook_install().

*- Add the body field.

*- Configure the body field.

*- Create the company name field.

*/

function job_post_install() { node_types_rebuild();

$types = node_type_get_types();

// add the body field to the node type node_add_body_field($types['job_post']);

// Load the instance definition for our content type's body $body_instance = field_info_instance('node', 'body', 'job_post'); // Configure the body field

$body_instance['type'] = 'text_summary_or_trimmed';

//Save our changes to the body field instance. field_update_instance($body_instance);

//Create all the fields we are adding to our content type. foreach (_job_post_installed_fields() as $field) {

field_create_field($field);

}

//Create all the instances for our fields.

foreach (_job_post_installed_instances() as $instance) { $instance['entity_type'] = 'node'; $instance['bundle'] = 'job_post'; field_create_instance($instance);

}

}

/**

*Return a structured array defining the fields created by this content type.

*For the job post module there is only one additional field – the company name

* Other fields could be added by defining them in this function as additional elements

*in the array below

*/

141

CHAPTER 7 WORKING WITH NODES

function _job_post_installed_fields() { $t = get_t();

return array(

'job_post_company' => array(

'field_name'

=> 'job_post_company',

'label'

=> $t('Company posting the job listing'),

'type'

=> 'text',

),

 

);

 

}

 

/**

 

* Return a structured array defining the field instances associated with this content type.

*/

 

function _job_post_installed_instances() {

$t = get_t();

 

return array(

'job_post_company'

=> array(

'field_name'

=>

'job_post_company',

'type'

=>

'text',

'label'

=>

$t('Company posting the job listing'),

'widget'

=> array(

'type'

=> 'text_textfield',

),

'display' => array( 'example_node_list' => array(

'label' => $t('Company posting the job listing'), 'type' => 'text',

),

),

),

);

}

/**

* Implements hook_uninstall(). */

function job_post_uninstall() {

//Gather all the example content that might have been created while this

//module was enabled.

$sql = 'SELECT nid FROM {node} n WHERE n.type = :type'; $result = db_query($sql, array(':type' => 'job_post')); $nids = array();

foreach ($result as $row) { $nids[] = $row->nid;

}

// Delete all the nodes at once node_delete_multiple($nids);

142

CHAPTER 7 WORKING WITH NODES

//Loop over each of the fields defined by this module and delete

//all instances of the field, their data, and the field itself. foreach (array_keys(_job_post_installed_fields()) as $field) {

field_delete_field($field);

}

//Loop over any remaining field instances attached to the job_post

//content type (such as the body field) and delete them individually. $instances = field_info_instances('node', 'job_post');

foreach ($instances as $instance_name => $instance) { field_delete_instance($instance);

}

//Delete our content type

node_type_delete('job_post');

// Purge all field infromation field_purge_batch(1000);

}

Creating the .info File

Let’s also create the job_post.info file and add it to the job post folder.

name = Job Post

description = A job posting content type package = Pro Drupal Development

core = 7.x

files[] = job_post.install files[] = job_post.module

Creating the .module File

Last, you need the module file itself. Create a file named job_post.module, and place it inside sites/all/modules/custom/job_posting. After you’ve completed the module, you can enable the module on the module listings page (Modules). You begin with the opening PHP tag and Doxygen comments.

<?php

/**

*@file

*This module provides a node type called job post

*/

143

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