You are here

contextual_view_modes.install in Contextual View Modes 7.2

Same filename and directory in other branches
  1. 7.3 contextual_view_modes.install
  2. 7 contextual_view_modes.install

File

contextual_view_modes.install
View source
<?php

/**
 * Implements hook_enable().
 * Create a field for use later
 */
function contextual_view_modes_install() {
}

/**
 * Implements hook_disable().
 */
function contextual_view_modes_disable() {
}

/**
 * Implements hook_uninstall().
 */
function contextual_view_modes_uninstall() {
}

/**
 * Implements hook_update().
 */
function contextual_view_modes_update_7101(&$sandbox) {
  db_update('system')
    ->fields(array(
    'weight' => 100,
  ))
    ->condition('name', 'contextual_view_modes')
    ->execute();
}

/**
 * Please back up your database before running as this does some pretty shady stuff.
 */
function contextual_view_modes_update_7102(&$sandbox) {
  if (!module_exists('cvm_field')) {
    if (!module_enable(array(
      'cvm_field',
    ))) {
      drupal_set_message(t('could not enable cvm_module'), 'error');
      throw new Exception("Could Not Enable cvm_field Module");
    }
  }

  // CVM field moved to sub module. We need to fix that for existing content.
  db_update('field_config')
    ->fields(array(
    'module' => 'cvm_field',
    'type' => 'cvm_field_cvm',
  ))
    ->condition('field_name', 'field_cvm_cvm')
    ->execute();
  $instances = db_select('field_config_instance', 'fci')
    ->fields("fci")
    ->condition('field_name', 'field_cvm_cvm')
    ->execute();
  while ($result = $instances
    ->fetchAssoc()) {
    $data = unserialize($result['data']);
    $data['widget']['type'] = "cvm_field_picker";
    $data['widget']['module'] = 'cvm_field';
    db_update('field_config_instance')
      ->fields(array(
      'data' => serialize($data),
    ))
      ->condition('id', $result['id'])
      ->execute();
  }
}

/**
 * Upgrade contextual view modes from 7.x-1.x -> 7.x-2.x
 * - Convert cvm field settings into variable
 * - Clean up and remove cvm field settings
 * - Convert global settings into new format
 * - Enable contextual view modes nodes ui module
 */
function contextual_view_modes_update_7200($node) {

  // Enable the contextual view modes nodes module.
  module_enable(array(
    "contextual_view_modes_nodes",
    "contextual_view_modes_users",
  ), TRUE);

  // Variable storage variable.
  $storage = array();

  // Convert the per type settings by getting the data from the field.
  $values = db_select('field_data_field_cvm_cvm', 'fdfcc')
    ->fields("fdfcc", array(
    "entity_type",
    "entity_id",
    "field_cvm_cvm_view_mode",
    "field_cvm_cvm_context",
    "delta",
  ))
    ->execute();

  // Loop through the results and set into the storage variables.
  while ($o = $values
    ->fetchObject()) {
    $type = $o->entity_type;
    if (isset($storage["contextual_view_modes_" . $type])) {
      $vars = $storage["contextual_view_modes_" . $type];
    }
    else {
      $vars = variable_get("contextual_view_modes_" . $type, array());
    }

    // Create a storage array if not set.
    if (isset($vars[$o->entity_id])) {
      $vars[$o->entity_id] = array();
    }

    // Create the entry
    $vars[$o->entity_id][$o->delta] = array(
      'context_name' => $o->field_cvm_cvm_context,
      'view_mode' => $o->field_cvm_cvm_view_mode,
    );

    // Put the storage back so we can continue to build the loop.
    $storage["contextual_view_modes_" . $type] = $vars;
  }

  // Loop through the storage and save the the database.
  foreach ($storage as $cvm_type => $variables) {
    variable_set($cvm_type, $variables);
  }

  // Now drop the tables.
  db_drop_table("field_data_field_cvm_cvm");
  db_drop_table("field_revision_field_cvm_cvm");

  // And the field config.
  db_delete("field_config")
    ->condition("module", "cvm_field")
    ->execute();
  db_delete("field_config_instance")
    ->condition("field_name", "field_cvm_cvm")
    ->execute();

  // Convert the global settings to the new format.
  // ----------------------------------------------
  $old_global = variable_get("cvm_global_content_type_modes", array());
  $new_global = variable_get("contextual_view_modes_global", array());
  foreach ($old_global as $bundle => $settings) {
    foreach ($settings as $context_name => $view_mode) {
      $new_global["node"][] = array(
        'bundle' => $bundle,
        'context' => $context_name,
        'view_mode' => $view_mode,
      );
    }
  }
  variable_set("contextual_view_modes_global", $new_global);
  variable_del("cvm_enabled_content_types");
  variable_del("cvm_global_content_type_modes");
}

Functions

Namesort descending Description
contextual_view_modes_disable Implements hook_disable().
contextual_view_modes_install Implements hook_enable(). Create a field for use later
contextual_view_modes_uninstall Implements hook_uninstall().
contextual_view_modes_update_7101 Implements hook_update().
contextual_view_modes_update_7102 Please back up your database before running as this does some pretty shady stuff.
contextual_view_modes_update_7200 Upgrade contextual view modes from 7.x-1.x -> 7.x-2.x