作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
我们这里是手动的创建Breadcrumb link字段,我们希望在安装这个Breadcrumb2模块的时候,自动的帮我们创建这个字段。这个字段,对我们来说是必须的。首先,我们把现有的字段的信息导出来。这里我们使用Features模块。
这是Features导出来的代码:
/**
* Implements hook_field_default_fields().
*/
function breadcrumb_link_field_default_fields() {
$fields = array();
// Exported field: 'breadcrumb2-breadcrumb2-field_breadcrumb_link'
$fields['breadcrumb2-breadcrumb2-field_breadcrumb_link'] = array(
'field_config' => array(
'active' => '1',
'cardinality' => '-1',
'deleted' => '0',
'entity_types' => array(),
'field_name' => 'field_breadcrumb_link',
'foreign keys' => array(),
'indexes' => array(),
'module' => 'link',
'settings' => array(
'attributes' => array(
'class' => '',
'rel' => '',
'target' => 'default',
),
'display' => array(
'url_cutoff' => 80,
),
'enable_tokens' => 1,
'title' => 'optional',
'title_maxlength' => 128,
'title_value' => '',
'url' => 0,
),
'translatable' => '0',
'type' => 'link_field',
),
'field_instance' => array(
'bundle' => 'breadcrumb2',
'default_value' => NULL,
'deleted' => '0',
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'link',
'settings' => array(),
'type' => 'link_default',
'weight' => 1,
),
),
'entity_type' => 'breadcrumb2',
'field_name' => 'field_breadcrumb_link',
'label' => 'Breadcrumb Link',
'required' => 0,
'settings' => array(
'attributes' => array(
'class' => '',
'configurable_title' => 0,
'rel' => '',
'target' => 'default',
'title' => '',
),
'display' => array(
'url_cutoff' => '80',
),
'enable_tokens' => 1,
'title' => 'required',
'title_maxlength' => '128',
'title_value' => '',
'url' => 'optional',
'user_register_form' => FALSE,
'validate_url' => 1,
),
'widget' => array(
'active' => 0,
'module' => 'link',
'settings' => array(),
'type' => 'link_field',
'weight' => '2',
),
),
);
// Translatables
// Included for use with string extractors like potx.
t('Breadcrumb Link');
return $fields;
}
我把它改造了一下,放到了install文件里面了,我们这里并没有打算依赖于Features模块:
/**
* Implements hook_install().
*/
function breadcrumb2_install() {
// Add or remove the link field, as needed.
$field = field_info_field('link');
if (empty($field)) {
$field = array(
'cardinality' => '-1',
'entity_types' => array('breadcrumb2'),
'field_name' => 'link',
'module' => 'link',
'type' => 'link_field',
);
$field = field_create_field($field);
}
$instance = field_info_instance('breadcrumb2', 'link', 'breadcrumb2');
if (empty($instance)) {
$instance = array(
'bundle' => 'breadcrumb2',
'default_value' => NULL,
'deleted' => '0',
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'link',
'settings' => array(),
'type' => 'link_default',
'weight' => 1,
),
),
'entity_type' => 'breadcrumb2',
'field_name' => 'link',
'label' => 'Breadcrumb Link',
'required' => 0,
'settings' => array(
'attributes' => array(
'class' => '',
'configurable_title' => 0,
'rel' => '',
'target' => 'default',
'title' => '',
),
'display' => array(
'url_cutoff' => '80',
),
'enable_tokens' => 1,
'title' => 'required',
'title_maxlength' => '128',
'title_value' => '',
'url' => 'optional',
'user_register_form' => FALSE,
'validate_url' => 1,
),
'widget' => array(
'active' => 0,
'module' => 'link',
'settings' => array(),
'type' => 'link_field',
'weight' => '2',
),
);
$instance = field_create_instance($instance);
}
}
在卸载我们的模块时,我们还需要负责将其删除:
/**
* Implements hook_uninstall().
*/
function breadcrumb2_uninstall() {
$instance = field_info_instance('breadcrumb2', 'link', 'breadcrumb2');
if (!empty($instance)) {
field_delete_instance($instance);
}
$field = field_info_field('link');
if ($field) {
field_delete_field('link');
}
}
注意这里面的field_info_field、field_create_field、field_info_instance、field_create_instance、field_delete_instance、field_delete_field等Drupal API函数的用法。它们用来获取字段信息、创建一个字段、获取字段实例信息、创建一个字段实例、删除一个字段实例、删除一个字段。
我刚开始的时候,这样写的代码:
field_delete_field($field);
结果在卸载模块的时候,总是报错,卸载不了。后来才修正过来。所以用的时候要小心。要明白这个函数里面各个参数的具体含义。