Drupal 7 Field多值删除与显示

发布于 2024-11-27 03:57:12 字数 3277 浏览 0 评论 0原文

我已经被困了好几天了,我希望有人能帮助我。 我正在编写一个模块来链接到不同的节点(我知道这样的模块确实存在,但是我想练习编码模块,我确实尝试查看其他模块以供参考,但是,我无法发现任何问题)。

添加模块后,我激活自定义节点类型中的字段并选择允许多个值。当我添加(该内容类型的)内容时,会出现两个字段而不是一个,并且当我添加额外字段时,我无法删除。我希望有人能给我一些如何修复它的指导。我在下面提供了问题的屏幕截图。我还希望添加一个删除按钮/一种删除额外项目的方法。经过几天的研究,我找不到 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上运行。

I have been stuck for days and I hope that somebody could help me out.
I am writing a module to link to different node (I understand that such module does exist however I would like to practice coding a module, I did tried to look at other module for reference, however, I cannot spot any problem).

After I added the module, I activate the field in a custom node type and select allow multiple value. When I added the content (of that content type) two of the field appears instead of one, and when I added extra field, I cannot delete. I hope that someone can give me some direction on how to fix it. I have included a screenshot of the problem below. I am also hoping to add a delete button / a way to delete an extra item. After research for days, I cannot find a drupal way to do it.

Below are the code I added for reference
for belong_to_relation.install, I added the following

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(),
);
  }

}

For belong_to_relation.module, I added the following

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'),
    ),
);
}

I am currently running on drupal 7.7 on MAMP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2024-12-04 03:57:12

OP 写道:

已解决,需要删除.install中的if ($field['type'] == 'belong_to_relation')

The OP wrote:

Solved, need to delete the if ($field['type'] == 'belong_to_relation') line in .install

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文