You are here

function pmtask_update_7104 in Drupal PM (Project Management) 7

Adds billing status instance to PM Task content type.

File

pmtask/pmtask.install, line 367
Install, disable, uninstall and update functions for pmtask module.

Code

function pmtask_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' => 'pmtask',
    'widget' => array(
      'weight' => -14,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmtask_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));

  // Delete the default field value. This is now set via the core field UI.
  variable_del('pmtask_billable_default');

  // Get existing field values.
  $query = db_select('pmtask', 'pmta');
  $query
    ->join('node', 'n', 'n.vid = pmta.vid');
  $result = $query
    ->fields('pmta', 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('pmtask', 'billable');
  db_drop_field('pmtask', 'billed');
}