【发布时间】:2011-07-29 16:54:30
【问题描述】:
我被困了好几天,我希望有人可以帮助我。 我正在编写一个模块来链接到不同的节点(我知道这样的模块确实存在,但是我想练习编写一个模块,我确实尝试查看其他模块以供参考,但是我找不到任何问题)。
添加模块后,我激活自定义节点类型中的字段并选择允许多个值。当我添加内容(该内容类型)时,出现两个字段而不是一个,当我添加额外字段时,我无法删除。我希望有人可以就如何解决它给我一些指导。我在下面包含了问题的屏幕截图。我也希望添加一个删除按钮/一种删除额外项目的方法。经过几天的研究,我找不到一种drupal方式来做到这一点。
以下是我添加的代码以供参考
对于belong_to_relation.install,我添加了以下内容
function belong_to_relation_field_schema ($field) {
if ($field['type'] == 'belong_to_relation') {
// Declare fields in the db
$columns = array (
'to_node_type' => array (
'type' => 'varchar',
'length' => '64',
'not null' => FALSE,
'description' => 'The relation id from the belong_to_relation table',
),
'to_node_nid' => array (
'type' => 'int',
'not null' => FALSE,
'description' => 'the node id of the to node',
),
'extended_node_nid' => array (
'type' => 'int',
'not null' => FALSE,
'description' => 'the node id of the extended field node',
),
);
return array (
'columns' => $columns,
'indexes' => array(),
);
}
}
对于belong_to_relation.module,我添加了以下内容
function belong_to_relation_field_info () {
return array (
'belong_to_relation_reference' => array (
'label' => t("Belong to Relation Node Reference"),
'description' => t('This field stores a node reference to a node of another content type'),
'default_widget' => 'belong_to_relation_reference_widget',
'default_formatter' => 'belong_to_relation_reference_formatter',
),
);
}
function belong_to_relation_field_widget_info () {
return array (
'belong_to_relation_reference_widget' => array (
'label' => t('Default'),
'field types' => array ('belong_to_relation_reference'),
),
);
}
function belong_to_relation_field_widget_form (&$form, &$form_state, $field,
$instance, $langcode, $items,
$delta, $element) {
$element += array(
'#type' => 'fieldset',
'#tree' => true
);
$element['to_node_type'] = array (
'#type' => 'select',
'#title' => t('Target Node Type'),
'#options' => array(),
'#default_value' => isset($item['to_node_type']) ? $item['to_node_type'] : NULL,
'#empty_option' => 'Select a Node type',
);
$element['to_node_nid'] = array (
'#type' => 'select',
'#title' => t('Target Node Type'),
'#options' => array(),
'#default_value' => isset($item['to_node_nid']) ? $item['to_node_nid'] : NULL,
'#empty_option' => 'Select a Node',
);
$element['extended_node_nid'] = array (
'#type' => 'select',
'#title' => t('Target Node Type'),
'#options' => array(),
'#default_value' => isset($item['extended_node_nid']) ? $item['extended_node_nid'] : NULL,
'#empty_option' => 'Select a Node type',
);
return $element;
}
function belong_to_relation_field_is_empty ($item, $field) {
return FALSE;
}
function belong_to_relation_field_formatter_info () {
return array (
'belong_to_relation_reference_formatter' => array (
'label' => t('Simple Belong To Relation Formatter'),
'field types' => array ('belong_to_relation_reference'),
),
);
}
我目前在 MAMP 上运行 drupal 7.7。
【问题讨论】:
-
已解决,需要删除.install中的if ($field['type'] == 'belong_to_relation')行
-
您可以发布自己的答案作为此问题的可接受答案。见meta.stackexchange.com/questions/83432/…