function pmticket_update_7104 in Drupal PM (Project Management) 7
Adds billing status instance to PM Ticket content type.
File
- pmticket/
pmticket.install, line 362 - Installation functions for the Project Management project module
Code
function pmticket_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' => 'pmticket',
'widget' => array(
'weight' => -13,
'type' => 'options_select',
'module' => 'options',
'active' => 1,
'settings' => array(),
),
'required' => 1,
'description' => '',
'default_value' => array(
array(
'value' => variable_get('pmticket_billable_default', 0) ? 'Billable' : 'Not billable',
),
),
));
// Delete the default field value. This is now set via the core field UI.
variable_del('pmticket_billable_default');
// Get existing field values.
$query = db_select('pmticket', 'pmti');
$query
->join('node', 'n', 'n.vid = pmti.vid');
$result = $query
->fields('pmti', 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('pmticket', 'billable');
db_drop_field('pmticket', 'billed');
}