You are here

panopoly_widgets.install in Panopoly Widgets 7

Same filename and directory in other branches
  1. 8.2 panopoly_widgets.install

An installation file for Panopoly Widgets

File

panopoly_widgets.install
View source
<?php

/**
 * @file
 * An installation file for Panopoly Widgets
 */

/**
 * Implementation of hook_install().
 */
function panopoly_widgets_install() {
}

/**
 * Implementation of hook_uninstall().
 */
function panopoly_widgets_uninstall() {

  // Retrieve the file IDs for default files
  $fids = array(
    'panopoly_widgets_default_spotlight_fid' => variable_get('panopoly_widgets_default_spotlight_fid', NULL),
    'panopoly_widgets_default_image_fid' => variable_get('panopoly_widgets_default_image_fid', NULL),
    'panopoly_widgets_default_video_fid' => variable_get('panopoly_widgets_default_video_fid', NULL),
  );
  foreach ($fids as $variable_name => $fid) {

    // Deletes the file
    if (!empty($fid) && ($file = file_load($fid))) {
      file_delete($file);
    }

    // Deletes the variable
    variable_del($variable_name);
  }
  variable_del('panopoly_widgets_spotlight_pause_play_buttons');
  variable_del('panopoly_widgets_spotlight_rotation_time');
}

/**
 * Implementation of hook_field_schema().
 */
function panopoly_widgets_field_schema($field) {
  if ($field['type'] == 'panopoly_spotlight') {
    $schema['columns']['title'] = array(
      'type' => 'varchar',
      'length' => '255',
      'not null' => FALSE,
    );
    $schema['columns']['description'] = array(
      'type' => 'text',
      'size' => 'big',
      'not null' => FALSE,
    );
    $schema['columns']['fid'] = array(
      'type' => 'int',
      'not null' => FALSE,
      'unsigned' => TRUE,
    );
    $schema['columns']['link'] = array(
      'type' => 'varchar',
      'length' => '255',
      'not null' => FALSE,
    );
    $schema['columns']['alt'] = array(
      'type' => 'varchar',
      'length' => '255',
      'not null' => FALSE,
    );
    $schema['indexes'] = array(
      'fid' => array(
        'fid',
      ),
    );
    $schema['foreign keys'] = array(
      'fid' => array(
        'table' => 'file_managed',
        'columns' => array(
          'fid' => 'fid',
        ),
      ),
    );
    return $schema;
  }
}

/**
 * Implements hook_update_dependencies().
 */
function panopoly_widgets_update_dependencies() {
  $deps = array();
  $deps['panopoly_widgets'][7005] = array(
    // I think that file_load() in our panopoly_widgets_update_7005() will
    // attempt to use pathauto, which means it's a dependency.
    'panopoly_core' => 7002,
  );
  if (module_exists('linkit')) {
    $deps['panopoly_widgets'][7017] = array(
      // The new 'Link' field depends on Linkit, so we need to make sure that
      // we create it after Linkit has had a chance to update 2.x -> 3.x.
      'linkit' => 7301,
    );
  }
  $deps['panopoly_widgets'][7021] = array(
    // Wait for tablefield to update itself.
    'tablefield' => 7006,
  );
  return $deps;
}

/**
 * Implementation of hook_update_N().
 *
 * Adding the default files for images, spotlights, and files to the database
 */
function panopoly_widgets_update_7001(&$sandbox) {

  // This section has been removed
}

/**
 * Implementation of hook_update_N().
 *
 * Revert panopoly_widget fields to avoid data loss in update 7003.
 */
function panopoly_widgets_update_7002(&$sandbox) {
  module_load_include('inc', 'features', 'features.export');
  features_include();

  // Need to give both the old Features 1.0 'field' component, and the new
  // Features 2.0 'field_base' and 'field_instance' because we can't be sure of
  // the version the user is upgrading to.
  features_revert(array(
    'panopoly_widgets' => array(
      'field',
      'field_base',
      'field_instance',
    ),
  ));
}

/**
 * Implementation of hook_update_N().
 *
 * Move video mediafield data into a file field.
 */
function panopoly_widgets_update_7003(&$sandbox) {

  // Move data from 'field_video_video' to 'field_video_file' directly in the
  // database, so don't have to worry about APIs changing over time.
  $tables = array(
    'field_data_field_video_video' => 'field_data_field_video_file',
    'field_revision_field_video_video' => 'field_revision_field_video_file',
  );
  $fields = array(
    'entity_type',
    'bundle',
    'deleted',
    'entity_id',
    'revision_id',
    'language',
    'delta',
    'field_video_video_fid',
  );
  foreach ($tables as $old_table => $new_table) {
    $result = db_select($old_table, 'f')
      ->fields('f', $fields)
      ->execute();
    foreach ($result as $data) {
      $data = (array) $data;
      $data['field_video_file_display'] = 1;
      $data['field_video_file_fid'] = $data['field_video_video_fid'];
      unset($data['field_video_video_fid']);
      db_insert($new_table)
        ->fields($data)
        ->execute();
    }
  }

  // Finally, delete the field.
  field_delete_field('field_video_video');
}

/**
 * Implements hook_update_N().
 *
 * Convert panel panes with "uuid" style subtypes into "fpid" style subtypes.
 */
function panopoly_widgets_update_7004(&$sandbox) {
  $results = db_select('panels_pane', 'p')
    ->fields('p', array(
    'pid',
    'subtype',
  ))
    ->execute()
    ->fetchAll();
  foreach ($results as $result) {
    if (substr($result->subtype, 0, 5) == 'uuid:') {
      $uuid = substr($result->subtype, 5);
      $fpid = db_select('fieldable_panels_panes', 'fpp')
        ->fields('fpp', array(
        'fpid',
      ))
        ->condition('uuid', $uuid)
        ->execute()
        ->fetchField();
      if (!empty($fpid)) {
        $subtype = "fpid:" . $fpid;
        db_update('panels_pane')
          ->fields(array(
          'subtype' => $subtype,
        ))
          ->condition('pid', $result->pid)
          ->execute();
      }
    }
  }
}

/**
 * Implements hook_update_N().
 *
 * Removes the default content from the database and file system
 */
function panopoly_widgets_update_7005(&$sandbox) {

  // Retrieve the file IDs for default files
  $fids = array(
    'panopoly_widgets_default_spotlight_fid' => variable_get('panopoly_widgets_default_spotlight_fid', NULL),
    'panopoly_widgets_default_image_fid' => variable_get('panopoly_widgets_default_image_fid', NULL),
    'panopoly_widgets_default_video_fid' => variable_get('panopoly_widgets_default_video_fid', NULL),
  );
  foreach ($fids as $variable_name => $fid) {

    // Deletes the file
    if (!empty($fid) && ($file = file_load($fid))) {
      file_delete($file);
    }

    // Deletes the variable
    variable_del($variable_name);
  }
}

/**
 * Implements hook_update_N().
 *
 * Revert the 'file_display' so YouTube works in WYSIWYG with newer Media.
 */
function panopoly_widgets_update_7006(&$sandbox) {
  features_revert(array(
    'panopoly_widgets' => array(
      'file_display',
    ),
  ));
}

/**
 * Implements hook_update_N().
 *
 * Add an "alt" field for spotlight items.
 */
function panopoly_widgets_update_7007(&$sandbox) {
  $spec = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
  );
  $data_table_name = 'field_data_field_basic_spotlight_items';
  $revision_table_name = 'field_revision_field_basic_spotlight_items';
  $field_name = 'field_basic_spotlight_items_alt';
  if (!db_field_exists($data_table_name, $field_name)) {
    db_add_field($data_table_name, $field_name, $spec);
  }
  if (!db_field_exists($revision_table_name, $field_name)) {
    db_add_field($revision_table_name, $field_name, $spec);
  }
}

/**
 * Setup the default for 'access media browser' permission.
 */
function panopoly_widgets_update_7008() {
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array(
    'access media browser',
  ));
}

/**
 * Make sure the new 'field_basic_spotlight_duration' gets created.
 */
function panopoly_widgets_update_7009() {
  features_revert(array(
    'panopoly_widgets' => array(
      'field_base',
      'field_instance',
    ),
  ));
}

/**
 * Make sure the new 'field_basic_spotlight_pager' field gets created.
 */
function panopoly_widgets_update_7010() {
  features_revert(array(
    'panopoly_widgets' => array(
      'field_base',
      'field_instance',
    ),
  ));
}

/**
 * Update the allowed file extensions in File widgets.
 */
function panopoly_widgets_update_7011() {
  if ($file_instance = field_info_instance('fieldable_panels_pane', 'field_basic_file_file', 'basic_file')) {

    // If the file extensions match the old default, then we set them to the new default.
    if ($file_instance['settings']['file_extensions'] == 'pdf txt') {
      $file_instance['settings']['file_extensions'] = PANOPOLY_WIDGETS_FILE_EXTENSIONS_DEFAULT;
      field_update_instance($file_instance);
    }
  }
}

/**
 * Update 'Content item' widgets to reference NID rather than title.
 */
function panopoly_widgets_update_7012() {

  // Make sure the View for the 'Content item' widget gets updated.
  features_revert(array(
    'panopoly_widgets' => array(
      'views_view',
    ),
  ));
  views_invalidate_cache();

  // Go through existing panes and update configuration.
  // We're going to load the panes directly from the 'panels_pane' table and
  // replace them with the new pane, reusing the same IDs and everything.
  // Don't try this at home, kids!
  $result = db_query("SELECT * FROM {panels_pane} WHERE type = 'views_panes' AND subtype = 'panopoly_widgets_general_content-piece_of_content'");
  $serialized_fields = array(
    'access',
    'configuration',
    'cache',
    'style',
    'css',
    'extras',
    'locks',
  );
  foreach ($result as $pane) {

    // Unserialize all the serialized fields.
    foreach ($serialized_fields as $field) {
      $pane->{$field} = unserialize($pane->{$field});
    }

    // Load the node using the View, set the NID and clear out the title.
    $exposed =& $pane->configuration['exposed'];
    $view = views_get_view('panopoly_widgets_general_content');
    $view
      ->set_display('piece_of_content');
    $view
      ->set_exposed_input($exposed);
    $view
      ->pre_execute();
    $view
      ->execute($view->current_display);
    $view
      ->post_execute();
    if (count($view->result) > 0) {
      $exposed['nid'] = $view->result[0]->nid;
      $exposed['title'] = '';

      // Write back to the database.
      drupal_write_record('panels_pane', $pane, array(
        'pid',
      ));
    }
  }
}

/**
 * Update the tablefield formatter to the new name.
 */
function panopoly_widgets_update_7013() {
  if ($tablefield_instance = field_info_instance('fieldable_panels_pane', 'field_basic_table_table', 'table')) {

    // If the formatter matches the old default, then we set it to the new default.
    if ($tablefield_instance['display']['default']['type'] == 'default') {
      $tablefield_instance['display']['default']['type'] = 'tablefield_default';
      field_update_instance($tablefield_instance);
      field_info_cache_clear();
    }
  }
}

/**
 * Rebuild theme registry so that spotlight theme functions are available.
 */
function panopoly_widgets_update_7014() {
  drupal_theme_rebuild();
}

/**
 * Unset the default image for the image widget.
 */
function panopoly_widgets_update_7015() {
  if ($image_field = field_info_field('field_basic_image_image')) {
    $image_field['settings']['default_image'] = 0;
    field_update_field($image_field);
    field_info_cache_clear();
  }
}

/**
 * Make sure that users can insert into the WYSIWYG.
 */
function panopoly_widgets_update_7016() {

  // Panopoly 1.29 was released without granting this permission by default, so
  // new sites launched on that version won't have it set. To avoid granting it
  // in situations where it was explicity removed on other sites, we only grant
  // it to roles that have the 'access media browser' permission (just like in
  // the media_wysiwyg_update_7201() when the permission was added by Media).
  if (module_exists('media_wysiwyg')) {
    $roles = user_roles(TRUE, 'access media browser');
    foreach ($roles as $rid => $role) {
      user_role_grant_permissions($rid, array(
        'use media wysiwyg',
      ));
    }
  }
}

/**
 * Make sure the new 'field_basic_image_link' gets created.
 */
function panopoly_widgets_update_7017() {
  features_revert(array(
    'panopoly_widgets' => array(
      'field_base',
      'field_instance',
    ),
  ));
}

/**
 * Make sure the image widget switches to the Media browser widget.
 */
function panopoly_widgets_update_7018() {
  features_revert(array(
    'panopoly_widgets' => array(
      'field_instance',
    ),
  ));
}

/**
 * Make sure the image widget disables URL truncating.
 */
function panopoly_widgets_update_7019() {
  features_revert(array(
    'panopoly_widgets' => array(
      'field_instance',
    ),
  ));
}

/**
 * Attempt to clean-up from bad Media upgrade.
 */
function panopoly_widgets_update_7020() {
  if (db_table_exists('media_view_mode_wysiwyg')) {
    $wysiwyg_view_modes = db_select('media_view_mode_wysiwyg', 'vm')
      ->fields('vm', array(
      'type',
      'view_mode',
    ))
      ->execute()
      ->fetchAllKeyed();
    $probably_bad_upgrade = TRUE;
    foreach ($wysiwyg_view_modes as $file_type => $view_mode) {

      // If the view_mode isn't the default value, then the user certainly
      // has configured this manually.
      if ($view_mode != 'wysiwyg') {
        $probably_bad_upgrade = FALSE;
        break;
      }

      // If the file displays aren't the same as the 'default' view_mode,
      // then the user certainly has configured this manually.
      $default_displays = file_displays($file_type, 'default');
      $wysiwyg_displays = file_displays($file_type, 'wysiwyg');
      if ($default_displays !== $wysiwyg_displays) {
        $probably_bad_upgrade = FALSE;
        break;
      }

      // Otherwise, this is probably a bad upgrade (not 100%, though).
    }
    if ($probably_bad_upgrade) {

      // Delete all the entries from the 'media_view_mode_wysiwyg' table.
      db_delete('media_view_mode_wysiwyg')
        ->execute();
    }
  }
}

/**
 * Grant new tablefield permission to admin roles. Update field settings.
 */
function panopoly_widgets_update_7021() {

  // Make sure new permissions from tablefield get added to the admin role.
  if (function_exists('user_modules_installed')) {
    user_modules_installed(array(
      'tablefield',
    ));
  }
  $instance_info = field_info_instance('fieldable_panels_pane', 'field_basic_table_table', 'table');
  $instance_info['display']['default']['settings']['sticky_header'] = 1;
  $instance_info['display']['default']['settings']['striping'] = 1;
  $instance_info['display']['default']['settings']['trim_trailing_cols'] = 1;
  $instance_info['display']['default']['settings']['trim_trailing_rows'] = 1;
  field_update_instance($instance_info);
}

/**
 * Mark important widget fields as required.
 */
function panopoly_widgets_update_7022() {
  $required_bundle_fields = array(
    'basic_file' => array(
      'field_basic_file_file',
    ),
    'map' => array(
      'field_map_address',
    ),
    'quick_links' => array(
      'field_quick_links_links',
    ),
    'table' => array(
      'field_basic_table_table',
    ),
    'text' => array(
      'field_basic_text_text',
    ),
    'video' => array(
      'field_video_file',
    ),
  );
  foreach ($required_bundle_fields as $bundle => $required_fields) {
    foreach ($required_fields as $field_name) {
      $instance_info = field_info_instance('fieldable_panels_pane', $field_name, $bundle);
      $instance_info['required'] = 1;
      field_update_instance($instance_info);
    }
  }
}

/**
 * Make sure that "Slide duration" on Spotlight widgets always has a value.
 */
function panopoly_widgets_update_7023(&$sandbox) {
  if (!isset($sandbox['fpids'])) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'fieldable_panels_pane')
      ->entityCondition('bundle', 'spotlight');
    $result = $query
      ->execute();
    if (isset($result['fieldable_panels_pane'])) {
      $sandbox['fpids'] = array_keys($result['fieldable_panels_pane']);
    }
  }
  else {
    $count = 10;
    while (!empty($sandbox['fpids']) && $count >= 0) {
      $fpp = fieldable_panels_panes_load(array_shift($sandbox['fpids']));
      $wrapper = entity_metadata_wrapper('fieldable_panels_pane', $fpp);
      if (!$wrapper->field_basic_spotlight_duration
        ->value()) {
        $wrapper->field_basic_spotlight_duration
          ->set(variable_get('panopoly_widgets_spotlight_rotation_time', 5));
        $wrapper
          ->save();
      }
    }
  }
  $sandbox['#finished'] = empty($sandbox['fpids']);
  if ($sandbox['#finished']) {
    variable_del('panopoly_widgets_spotlight_rotation_time');
  }
}

/**
 * Update field instance settings for duration fields on Spotlight widgets.
 */
function panopoly_widgets_update_7024() {

  // Remove legacy field if it's still around.
  if ($instance_info = field_info_instance('fieldable_panels_pane', 'field_spotlight_rotation_time', 'spotlight')) {
    field_delete_instance($instance_info);
  }

  // Make the default value 5 and mark as required.
  if ($instance_info = field_info_instance('fieldable_panels_pane', 'field_basic_spotlight_duration', 'spotlight')) {
    $instance_info['default_value'] = array(
      0 => array(
        'value' => 5,
      ),
    );
    $instance_info['required'] = 1;
    field_update_instance($instance_info);
  }
}

/**
 * Replaced by panopoly_widgets_update_7026().
 */
function panopoly_widgets_update_7025() {

  // This was a broken version of panopoly_widgets_update_7026() that was
  // too brittle and didn't work in all situations.
}

/**
 * Switch the file widget to the Media browser widget.
 */
function panopoly_widgets_update_7026() {
  $new_widget = array(
    'active' => 1,
    'module' => 'media',
    'settings' => array(
      'allowed_schemes' => array(
        'private' => 'private',
        'public' => 'public',
        'vimeo' => 0,
        'youtube' => 0,
      ),
      'allowed_types' => array(
        'audio' => 0,
        'document' => 'document',
        'image' => 0,
        'video' => 0,
      ),
      'browser_plugins' => array(
        'media_default--media_browser_1' => 'media_default--media_browser_1',
        'media_default--media_browser_my_files' => 0,
        'media_internet' => 0,
        'upload' => 'upload',
      ),
      'manualcrop_crop_info' => 1,
      'manualcrop_default_crop_area' => 1,
      'manualcrop_enable' => 0,
      'manualcrop_inline_crop' => 0,
      'manualcrop_instant_crop' => FALSE,
      'manualcrop_instant_preview' => 1,
      'manualcrop_keyboard' => 1,
      'manualcrop_maximize_default_crop_area' => 0,
      'manualcrop_require_cropping' => array(),
      'manualcrop_styles_list' => array(),
      'manualcrop_styles_mode' => 'include',
      'manualcrop_thumblist' => 0,
    ),
    'type' => 'media_generic',
    'weight' => 2,
  );
  $info = field_info_instance("fieldable_panels_pane", "field_basic_file_file", "basic_file");
  if ($info['widget']['type'] == 'file_generic') {
    $info['widget'] = $new_widget;
    $info['widget']['weight'] = $info['widget']['weight'];
    field_update_instance($info);
  }
}

Functions

Namesort descending Description
panopoly_widgets_field_schema Implementation of hook_field_schema().
panopoly_widgets_install Implementation of hook_install().
panopoly_widgets_uninstall Implementation of hook_uninstall().
panopoly_widgets_update_7001 Implementation of hook_update_N().
panopoly_widgets_update_7002 Implementation of hook_update_N().
panopoly_widgets_update_7003 Implementation of hook_update_N().
panopoly_widgets_update_7004 Implements hook_update_N().
panopoly_widgets_update_7005 Implements hook_update_N().
panopoly_widgets_update_7006 Implements hook_update_N().
panopoly_widgets_update_7007 Implements hook_update_N().
panopoly_widgets_update_7008 Setup the default for 'access media browser' permission.
panopoly_widgets_update_7009 Make sure the new 'field_basic_spotlight_duration' gets created.
panopoly_widgets_update_7010 Make sure the new 'field_basic_spotlight_pager' field gets created.
panopoly_widgets_update_7011 Update the allowed file extensions in File widgets.
panopoly_widgets_update_7012 Update 'Content item' widgets to reference NID rather than title.
panopoly_widgets_update_7013 Update the tablefield formatter to the new name.
panopoly_widgets_update_7014 Rebuild theme registry so that spotlight theme functions are available.
panopoly_widgets_update_7015 Unset the default image for the image widget.
panopoly_widgets_update_7016 Make sure that users can insert into the WYSIWYG.
panopoly_widgets_update_7017 Make sure the new 'field_basic_image_link' gets created.
panopoly_widgets_update_7018 Make sure the image widget switches to the Media browser widget.
panopoly_widgets_update_7019 Make sure the image widget disables URL truncating.
panopoly_widgets_update_7020 Attempt to clean-up from bad Media upgrade.
panopoly_widgets_update_7021 Grant new tablefield permission to admin roles. Update field settings.
panopoly_widgets_update_7022 Mark important widget fields as required.
panopoly_widgets_update_7023 Make sure that "Slide duration" on Spotlight widgets always has a value.
panopoly_widgets_update_7024 Update field instance settings for duration fields on Spotlight widgets.
panopoly_widgets_update_7025 Replaced by panopoly_widgets_update_7026().
panopoly_widgets_update_7026 Switch the file widget to the Media browser widget.
panopoly_widgets_update_dependencies Implements hook_update_dependencies().