You are here

function mailchimp_lists_update_7200 in Mailchimp 7.2

Add name, status, and module fields to make lists exportable.

File

modules/mailchimp_lists/mailchimp_lists.install, line 141
Install, update and uninstall functions for the mailchimp_lists module.

Code

function mailchimp_lists_update_7200() {

  // The update was previously broken. To fix the broken state, we need to
  // remove the left-over field first.
  if (db_field_exists('mailchimp_lists', 'name')) {
    db_drop_field('mailchimp_lists', 'name');
  }

  // Add the machine name field.
  db_add_field('mailchimp_lists', 'name', array(
    'description' => 'The machine-readable name of this mailchimp_list.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => FALSE,
  ));

  // Add the exportable status field.
  if (!db_field_exists('mailchimp_lists', 'status')) {
    db_add_field('mailchimp_lists', 'status', array(
      'type' => 'int',
      'not null' => TRUE,
      // Set the default to ENTITY_CUSTOM without using the constant as it is
      // not safe to use it at this point.
      'default' => 0x1,
      'size' => 'tiny',
      'description' => 'The exportable status of the entity.',
    ));
  }

  // Add the exportable module field.
  if (!db_field_exists('mailchimp_lists', 'module')) {
    db_add_field('mailchimp_lists', 'module', array(
      'description' => 'The name of the providing module if the entity has been defined in code.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    ));
  }

  // Generate a machine name for existing lists.
  $lists = db_select('mailchimp_lists', 'm')
    ->fields('m')
    ->execute()
    ->fetchAll();
  foreach ($lists as $list) {
    $list->name = strtolower(str_replace(' ', '_', $list->label));
    db_update('mailchimp_lists')
      ->fields(array(
      'name' => substr($list->name, 0, 32),
    ))
      ->condition('id', $list->id)
      ->execute();
  }

  // Now set the name field to be required.
  db_change_field('mailchimp_lists', 'name', 'name', array(
    'description' => 'The machine-readable name of this mailchimp_list.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
  ));
}