You are here

function nodeblock_update_7102 in Nodeblock 7

Adding a new nodeblock table and create entries for all existing nodeblocks.

File

./nodeblock.install, line 127
Define module install logic.

Code

function nodeblock_update_7102() {
  module_load_include('module', 'nodeblock');
  if (!db_table_exists('nodeblock')) {
    $schema = nodeblock_schema();
    db_create_table('nodeblock', $schema['nodeblock']);
    $types = node_type_get_types();
    $enabled_types = array();
    foreach ($types as $type_name => $type) {
      if (nodeblock_type_enabled($type_name)) {
        $enabled_types[] = $type_name;
      }
    }
    if (count($enabled_types)) {
      $results = db_select('node', 'n')
        ->fields('n', array(
        'nid',
      ))
        ->condition('type', $enabled_types)
        ->execute()
        ->fetchCol();
      $defaults = array(
        'comment_link' => 'node_block_default',
        'node_link' => 'node_block_default',
        'view_mode' => 'node_block_default',
      );
      foreach ($results as $nid) {
        $settings = variable_get('nodeblock_block_' . $nid, $defaults);
        $settings = array_merge($defaults, $settings);
        if (isset($settings['teaser']) && $settings['teaser'] == 1) {
          $settings['view_mode'] == 'teaser';
        }
        if (isset($settings['links']) && $settings['links'] == 1) {
          $settings['node_link'] = 1;
          $settings['comment_link'] = 1;
        }
        db_insert('nodeblock')
          ->fields(array(
          'nid',
          'machine_name',
          'view_mode',
          'node_link',
          'comment_link',
          'translation_fallback',
        ))
          ->values(array(
          $nid,
          $nid,
          $settings['view_mode'],
          $settings['node_link'],
          $settings['comment_link'],
          variable_get('nodeblock_translation_fallback_' . $nid, 0),
        ))
          ->execute();
        variable_del('nodeblock_translation_fallback_' . $nid);
        variable_del('nodeblock_block_' . $nid);
      }
    }
  }
}