You are here

function newsletter_update_7103 in Newsletter 7

Attach a text_with_summary field to newsletter_template and newsletter_newsletter.Remove obsolete database fields.

File

./newsletter.install, line 669
Contains install,uninstall and update functions for Newsletter module.

Code

function newsletter_update_7103(&$sandbox) {
  $instance = array(
    'field_name' => 'field_newsletter_body',
    'entity_type' => 'newsletter_template',
    'bundle' => 'newsletter_template',
    'label' => 'Body',
    'widget' => array(
      'type' => 'text_textarea_with_summary',
    ),
    'settings' => array(
      'display_summary' => TRUE,
    ),
    'display' => array(
      'default' => array(
        'label' => 'hidden',
        'type' => 'text_default',
      ),
      'teaser' => array(
        'label' => 'hidden',
        'type' => 'text_summary_or_trimmed',
      ),
    ),
  );
  field_create_instance($instance);

  // Change values and attach same field to newsletter
  $instance['entity_type'] = 'newsletter_newsletter';
  $instance['bundle'] = 'newsletter_newsletter';
  field_create_instance($instance);

  // Transfer old data to field
  $ntids = db_query('SELECT ntid FROM {newsletter_template}')
    ->fetchCol();
  foreach ($ntids as $ntid) {
    $template = newsletter_template_load($ntid);
    $template->field_newsletter_body[LANGUAGE_NONE][0] = array(
      'value' => db_query('SELECT body FROM {newsletter_template} WHERE ntid = :ntid', array(
        ':ntid' => $ntid,
      ))
        ->fetchField(),
      'format' => db_query('SELECT format FROM {newsletter_template} WHERE ntid = :ntid', array(
        ':ntid' => $ntid,
      ))
        ->fetchField(),
    );
    entity_get_controller('newsletter_template')
      ->save($template);
  }

  // Drop the fields
  db_drop_field('newsletter_template', 'body');
  db_drop_field('newsletter_template', 'format');
}