Drupal 6 到 Drupal 7 迁移表单 API php 代码片段

发布于 2024-12-09 11:31:57 字数 1863 浏览 0 评论 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).

I'm trying to figure out what is wrong with this bit of Drupal 6 php code i'm trying to get working in Drupal 7:

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;

This is how far I've got:

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;

errors returned:

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).

and

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).

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

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

发布评论

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

评论(1

执着的年纪 2024-12-16 11:31:57

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

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

$content_type = field_info_instances('node', $form['#type']['#value']);

field_info_instances takes two arguments (albeit they're not required), the first is the entity type (more than likely node in your case), and the second is the bundle.

In Drupal 7 all nodes are entities and content types are bundles within that entity. So to get all the fields attached to a node with your specific content type you need to do this:

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