Skip to content

Instantly share code, notes, and snippets.

@larowlan
Created January 21, 2013 21:49
Show Gist options
  • Save larowlan/4589799 to your computer and use it in GitHub Desktop.
Save larowlan/4589799 to your computer and use it in GitHub Desktop.
Working with rjkha on http://drupal.org/node/626546
/**
* Adds the default body field to a node type.
*
* @param $type
* A node type object.
* @param bool $subject
* (optional) Include a summary field. Defaults to TRUE.
* @param $label
* (optional) The label for the body instance.
*
* @return
* Body field instance.
*/
function node_add_body_field($type, $summary = TRUE, $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' => $summary ? 'text_with_summary' : 'text_long',
'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,
'widget' => array('type' => $summary ? 'text_textarea_with_summary' : 'text_textarea'),
'settings' => array('display_summary' => TRUE),
);
$instance = field_create_instance($instance);
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment