Adds the default body field to a node type.
$type: A node type object.
$label: (optional) The label for the body instance.
Body field instance.
function node_add_body_field($type, $label = 'Body') {
// Add or remove the body field, as needed.
$field = field_info_field('body');
$instance = field_info_instance('node', 'body', $type->type);
if (empty($field)) {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array(
'node',
),
);
$field = field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => 'body',
'entity_type' => 'node',
'bundle' => $type->type,
'label' => $label,
'settings' => array(
'display_summary' => TRUE,
),
);
$instance = field_create_instance($instance);
// Assign widget settings for the 'default' form mode.
entity_get_form_display('node', $type->type, 'default')
->setComponent($field['field_name'], array(
'type' => 'text_textarea_with_summary',
))
->save();
// Assign display settings for the 'default' and 'teaser' view modes.
entity_get_display('node', $type->type, 'default')
->setComponent($field['field_name'], array(
'label' => 'hidden',
'type' => 'text_default',
))
->save();
entity_get_display('node', $type->type, 'teaser')
->setComponent($field['field_name'], array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
))
->save();
}
return $instance;
}