开发者

Drupal 6 to Drupal 7 migration form API php code snippet

开发者 https://www.devze.com 2023-04-12 13:47 出处:网络
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:

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;

er开发者_JAVA百科rors 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).


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']);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号