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

CHAPTER 8 WORKING WITH FIELDS

Adding Fields Programmatically

The Field API can be used to add fields programmatically to a content type or node type. The following example demonstrates using the Field API to add a new field to a content type created through a module. The Job Post module creates a content type that extends a traditional node (title and body) by adding a new field that stores and displays the name of the company that is sponsoring the job posting. Adding fields takes place in the .install file of a module in the hook_install() or hook_update() functions.

The first step in the hook_install() function adds the body field to our new Job Post content type. By default a content type created through a module contains only the title field; you must implicitly add the body field. The node.module defines a function named node_add_body_field(), which adds the standard body field to our Job Post content type. The next step that I go through is adding the definition of all the fields that I want to add to the Job Post content type. In the case of this example, there is a single field that I’ll add named job_post_companies. If I wanted to add multiple fields, I could do so by simply creating additional field definitions in the _job_post_installed_fields() function. Defining the new field is a simple matter of giving it a name, a label, and field type. The final step in creating the field is to instantiate the fields using the field_create_instance() function. This function does all the behind-the-scenes work of creating the storage mechanism in the database to hold the values entered by the user. When you use this approach, there’s no need to define the tables in the database—the Field API does all the work for you. After you install the module, the fields are there on the node edit form, ready for your module to use. For additional information on how to use and theme the output of custom fields, please see Chapter 7.

<?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(); 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');

//Add our job_post_list view mode to the body instance display by. $body_instance['type'] = 'text_summary_or_trimmed';

//Save our changes to the body field instance.

field_update_instance($body_instance);

181

CHAPTER 8 WORKING WITH FIELDS

//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. */

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 instances for 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( job_post_list' => array(

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

),

),

),

);

}

182

CHAPTER 8 WORKING WITH FIELDS

/**

* 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);

//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 information. field_purge_batch(1000);

}

Summary

In this chapter, I covered the basics of using Drupal 7’s core functionality to create a custom content type that contains additional fields beyond the title and body, how to create a custom field type, and how to programmatically add new fields to a module. In the next chapter, we’ll enter the realm of theming, learning how to apply visual styling to the content Drupal renders on our site.

183

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