function pmexpense_update_7104 in Drupal PM (Project Management) 7
Adds billing status instance to PM Expense content type.
File
- pmexpense/
pmexpense.install, line 291 - Installation file for pmexpense module.
Code
function pmexpense_update_7104() {
// Uncache node types
node_types_rebuild();
// Set up instance of billing status field.
field_create_instance(array(
'field_name' => 'pm_billing_status',
'label' => 'Billing status',
'entity_type' => 'node',
'bundle' => 'pmexpense',
'widget' => array(
'weight' => -17,
'type' => 'options_select',
'module' => 'options',
'active' => 1,
'settings' => array(),
),
'required' => 1,
'description' => '',
'default_value' => array(
array(
'value' => variable_get('pmexpense_billable_default', 0) ? 'Billable' : 'Not billable',
),
),
));
// Delete the default field value. This is now set via the core field UI.
variable_del('pmexpense_billable_default');
// Get existing field values.
$query = db_select('pmexpense', 'pmex');
$query
->join('node', 'n', 'n.vid = pmex.vid');
$result = $query
->fields('pmex', array(
'nid',
'vid',
'billable',
'billed',
))
->execute();
// Save existing field value into Field API field.
foreach ($result as $record) {
$node = node_load($record->nid);
$new_value = $record->billed ? 'Billed' : ($record->billable ? 'Billable' : 'Not billable');
$node->pm_billing_status[LANGUAGE_NONE][0]['value'] = $new_value;
field_attach_presave('node', $node);
field_attach_update('node', $node);
}
// Remove existing field from custom database table.
db_drop_field('pmexpense', 'billable');
db_drop_field('pmexpense', 'billed');
}