0

我试图弄清楚我试图在 Drupal 7 中工作的这段 Drupal 6 php 代码有什么问题:

function node_widget_get_fields(&$form) {
  $fields = array();
   if (isset($form['#type'])) {
     $content_type = content_types($form['#type']['#value']);
     foreach ($content_type['fields'] as $field_name => $field) {
        if ($field['type'] == 'nodereference' && $field['widget']['type'] == 
       'node_widget_node_form') {
    $fields[$field_name] = $field;
  }
}
}
return $fields;

这是我已经走了多远:

function node_widget_get_fields(&$form) {
  $fields = array();
   if (isset($form['#type'])) {
     $content_type = field_info_instances($form['#type']['#value']);
     foreach ($content_type['fields'] as $field_name => $field) {
        if ($field['type'] == 'nodereference' && $field['widget']['type'] == 
       'node_widget_node_form') {
    $fields[$field_name] = $field;
  }
}
} 
return $fields;

返回的错误:

Notice: Undefined index: f in field_info_instances() (line 682 of /modules/field/field.info.inc).
Warning: Invalid argument supplied for foreach() in node_widget_get_fields() (line 164 of all/modules/node_widget/includes/node_widget.form.inc).

Notice: Undefined index: how_to in field_info_instances() (line 682 of /var/www/bitbybit/modules/field/field.info.inc).
Warning: Invalid argument supplied for foreach() in node_widget_get_fields() (line 164 of /var/www/bitbybit/sites/all/modules/node_widget/includes/node_widget.form.inc).
Notice: Undefined index: type in node_widget_get_fields() (line 163 of /var/www/bitbybit/sites/all/modules/node_widget/includes/node_widget.form.inc).
Notice: Undefined index: fields in node_widget_get_fields() (line 164 of /var/www/bitbybit/sites/all/modules/node_widget/includes/node_widget.form.inc).
4

1 回答 1

0

field_info_instances接受两个参数(尽管它们不是必需的),第一个是实体类型(node在您的情况下很可能),第二个是捆绑包。

在 Drupal 7 中,所有节点都是实体,内容类型是该实体中的捆绑包。因此,要使用您的特定内容类型获取附加到 a 的所有字段node,您需要执行以下操作:

$content_type = field_info_instances('node', $form['#type']['#value']);
于 2011-10-10T16:38:18.980 回答