Drupal 6 CCK模块
一、 原理
通过hook_form_alter()挂接CCK Field,通过hook_nodeapi()对挂接的CCK Field进行各项操作(CRUD)。核心函数_content_type_info()搜集当前节点系统的field types列表,widget types列表,['field types'] ['formatters']列表,fields列表和content types列表,模拟如下
Array(
'field types' => array(
‘text’ => array(
‘label’ =>
‘description’ =>
‘callbacks’ => array (
‘tables’ => CONTENT_CALLBACK_DEFAULT
‘arguments’ => CONTENT_CALLBACK_DEFAULT
)
‘module’ =>
‘formatters’ => array (
‘theme_name’ => array(
‘label’ => t(),
‘field types’ => array(),
‘multiple values’ => CONTENT_HANDLE_CORE
‘module’ =>
)
)
),
)
‘widget types’ => array(
‘text_textfield’ => array(
‘label’ =>
‘multiple values’ => CONTENT_HANDLE_CORE,
‘callbacks’ => array(
‘default value’ => CONTENT_CALLBACK_DEFAULT,
)
‘module’ =>
‘field types’ => array(
‘text’
)
)
)
‘fields’ => array(
‘field_name’ => array(
)
),
‘content types’ => array(
‘node_type’ => array(
‘url_str’ => ,
‘fields’ = array(
‘field_name’ => array()
),
‘tables’ =>
‘extra’ => array()
)
)
)
对CCK Field的核心操作函数是content_field($op, &$node, $field, &$items, $teaser, $page)和content_storage($op, $node)两个函数,后者处理node的CRUD操作,前者处理CCK Field的view、alter、validate、presave等操作,是hook_field()的特殊实现,但是其他定制的hook_field都可以自定义这些操作的实现。
二、 Form流程
1、 调用content_form()挂载CCK Field Form,先调用content_types()获取当前节点类型的字段信息,遍历该节点字段,调用content_field_form()填充必要的Field Form数据;
2、 通过content_types()调整非CCK Field的weight值;
3、 对于widget在Form中的显示问题,可以通过设置widget的’#type’为自定义element,然后自定义的theme函数里进行显示的设置。
三、 Field CRUD流程
1、 通过hook_nodeapi()来完成Field CRUD的各项操作;
2、 对于view,先调用各个hook_field的sanitize操作,然后调用_content_field_invoke_default()获得CCK各项Field的输出,添加到$node->content上,调整非CCK Field的weight值,
3、 对于load、validate、insert、update、delete等操作,通过调用_content_field_invoke_default($op, &$node, $teaser = NULL, $page = NULL)和_content_field_invoke($op, &$node, $teaser = NULL, $page = NULL)来执行操作,不过不同操作的两个函数调用顺序可能不一样。
四、 API
1、 hook_field_info()
return array(
‘field_name’ => array(
‘label’ => t(),
‘description’ => t(),
‘callbacks’ = array(
‘tables’ => CONTENT_CALLBACK_DEFAULT,
‘arguments’ => CONTENT_CALLBACK_DEFAULT
)
)
)
2、 hook_widget_info()
return array(
‘widget_name’ => array(
‘label’ =>
‘field types’ => array()
‘multiple values’ => CONTENT_HANDLE_CORE
‘callbacks’ => array(
‘default value’ => CONTENT_CALLBACK_DEFAULT,
)
)
)
3、 hook_field_formatter_info()
return array(
‘theme_name’ => array(
‘label’ => t(),
‘field types’ => array(),
‘multiple values’ => CONTENT_HANDLE_CORE
)
)
返回的formatter需要通过hook_theme注册为theme_module_formatter_name()形式的theme项。通过’ multiple values’来指定是否处理多值。
4、 hook_content_extra_fields()
用于修改非CCK Field的参数。
5、 hook_field()
6、 hook_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}
- wilson's blog
- Login or register to post comments

