作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
这就是我们想要的效果。不过,当我们访问节点页面时,并没有看到这个字段的内容,系统已经为其生成内容了阿。对于字段的显示,仍然需要由我们的模块来负责,谁让这个字段是由这个模块定义的呢。我们添加最后的两个钩子函数:
/**
* Implements hook_field_formatter_info().
*
*/
function transliteration_title_field_formatter_info() {
$formats = array(
'transliteration_title_default' => array(
'label' => t('Default'),
'description' => t('Default display for the transliteration title.'),
'field types' => array('transliteration_title_field'),
),
'transliteration_title_plain' => array(
'label' => t('Plain text'),
'description' => t('Display the transliteration title as plain text.'),
'field types' => array('transliteration_title_field'),
),
);
return $formats;
}
/**
* Implements hook_field_formatter_view().
*/
function transliteration_title_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'transliteration_title_default':
foreach ($items as $delta => $item) {
//drupal_set_message(print_r($item));
$element[$delta] = array('#markup' => $item['value']);
}
break;
case 'transliteration_title_plain':
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => check_plain($item['value']));
}
break;
}
return $element;
}
首先,我们实现了hook_field_formatter_info钩子函数,在这里我们定义了两个显示格式,一个是默认的格式,一个是纯文本的格式,本质上两者也没有太大的区别。此时清空缓存,然后访问字段的管理显示页面,我们会看到我们定义的字段格式,如图所示:
我们定义的格式显示在了管理显示页面
最后,我们在hook_field_formatter_view钩子中,定义了两种显示格式的具体实现,这里需要注意的是,这个钩子函数返回的是一个呈现数组。当我们再次访问节点页面时,就可以看到这个字段的值了。
我们的这个实例模块,到这里就讲解完毕,这里我将模块的实现,按照我们使用字段的先后顺序,逐一的剥离处理。希望读完这一节后,大家能对通过模块定义一个字段类型有所了解,对我们日常所用的字段有更深一层的把握。