You are here

function pmexpense_update_7103 in Drupal PM (Project Management) 7

Convert Expense Date field to Field API.

File

pmexpense/pmexpense.install, line 232
Installation file for pmexpense module.

Code

function pmexpense_update_7103() {

  // Expense date field.
  field_create_field(array(
    'type' => 'datetime',
    'field_name' => 'pmexpense_date',
    'settings' => array(
      'granularity' => array(
        'year' => 'year',
        'month' => 'month',
        'day' => 'day',
        'hour' => 0,
        'minute' => 0,
        'second' => 0,
      ),
      'tz_handling' => '',
      'timezone_db' => '',
      'todate' => '',
    ),
    'cardinality' => '1',
  ));
  field_create_instance(array(
    'field_name' => 'pmexpense_date',
    'bundle' => 'pmexpense',
    'label' => 'Expense Date',
    'required' => 1,
    'entity_type' => 'node',
    'widget' => array(
      'weight' => '-18',
      'type' => 'date_text',
      'active' => 1,
    ),
    'settings' => array(
      'default_value' => 'now',
    ),
  ));

  // Migrate data.
  $query = db_select('pmexpense', 'pmex');
  $query
    ->join('node', 'n', 'n.vid = pmex.vid');
  $result = $query
    ->fields('pmex', array(
    'nid',
    'vid',
    'expensedate',
  ))
    ->execute();
  foreach ($result as $record) {
    $node = node_load($record->nid);
    $node->pmexpense_date[LANGUAGE_NONE][0]['value'] = date('Y-m-d H:i:s', $record->expensedate);
    field_attach_presave('node', $node);
    field_attach_update('node', $node);
  }

  // Delete columns from existing database table.
  db_drop_field('pmexpense', 'expensedate');
}