You are here

function media_update_7204 in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 media.install \media_update_7204()
  2. 7.2 media.install \media_update_7204()

Update old Media view modes to the new File Entity ones.

File

./media.install, line 642
Install, update and uninstall functions for the Media module.

Code

function media_update_7204() {
  $view_mode_updates = array(
    'media_preview' => 'preview',
    'media_small' => 'teaser',
    'media_large' => 'full',
  );

  // Update the media__wysiwyg_default_view_mode variable.
  $wysiwyg_default_view_mode = variable_get('media__wysiwyg_default_view_mode');
  if (isset($wysiwyg_default_view_mode) && isset($view_mode_updates[$wysiwyg_default_view_mode])) {
    $wysiwyg_default_view_mode = $view_mode_updates[$wysiwyg_default_view_mode];
    variable_set('media__wysiwyg_default_view_mode', $wysiwyg_default_view_mode);
  }

  // Update view mode references in the 'field_bundle_settings' variable.
  $field_bundle_settings = variable_get('field_bundle_settings');
  if (!empty($field_bundle_settings['file'])) {
    foreach ($field_bundle_settings['file'] as $file_type => $info) {

      // Per-bundle information about the view modes.
      foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
        if (isset($info['view_modes'][$old_view_mode])) {
          $field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode] = $info['view_modes'][$old_view_mode];
          unset($field_bundle_settings['file'][$file_type]['view_modes'][$old_view_mode]);
        }

        // The File Entity module defaults to not use custom settings for the
        // new view modes, but the Media module used to default to using custom
        // settings, so if this variable is not defined, use the prior default.
        if (!isset($field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode]['custom_settings'])) {
          $field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode]['custom_settings'] = TRUE;
        }
      }

      // Settings for the "extra fields" configured on the Manage Display page.
      if (!empty($info['extra_fields']['display'])) {
        foreach ($info['extra_fields']['display'] as $extra_field_name => $extra_field_info) {
          foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
            if (isset($extra_field_info[$old_view_mode])) {
              $field_bundle_settings['file'][$file_type]['extra_fields']['display'][$extra_field_name][$new_view_mode] = $extra_field_info[$old_view_mode];
              unset($field_bundle_settings['file'][$file_type]['extra_fields']['display'][$extra_field_name][$old_view_mode]);
            }
          }
        }
      }
    }
  }
  variable_set('field_bundle_settings', $field_bundle_settings);

  // Move settings for fields attached to files from the old view modes to the
  // new ones.
  $instances = field_read_instances(array(
    'entity_type' => 'file',
  ));
  foreach ($instances as $instance) {
    $updated = FALSE;
    foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
      if (isset($instance['display'][$old_view_mode])) {
        $instance['display'][$new_view_mode] = $instance['display'][$old_view_mode];
        unset($instance['display'][$old_view_mode]);
        $updated = TRUE;
      }
    }
    if ($updated) {
      field_update_instance($instance);
    }
  }

  // Move "Manage file display" settings from old view modes to new ones.
  $file_display_names = db_query('SELECT name FROM {file_display}')
    ->fetchCol();
  foreach ($file_display_names as $old_file_display_name) {
    list($file_type, $view_mode, $formatter) = explode('__', $old_file_display_name, 3);
    if (isset($view_mode_updates[$view_mode])) {
      $view_mode = $view_mode_updates[$view_mode];
      $new_file_display_name = implode('__', array(
        $file_type,
        $view_mode,
        $formatter,
      ));
      db_delete('file_display')
        ->condition('name', $new_file_display_name)
        ->execute();
      db_update('file_display')
        ->fields(array(
        'name' => $new_file_display_name,
      ))
        ->condition('name', $old_file_display_name)
        ->execute();
    }
  }

  // Update file/image/media fields that use a formatter that reference an old
  // file view modes to reference the new ones.
  foreach (field_read_instances() as $instance) {
    if (!empty($instance['display'])) {
      $updated = FALSE;
      foreach ($instance['display'] as $instance_view_mode => $display) {
        if (isset($display['settings']['file_view_mode']) && isset($view_mode_updates[$display['settings']['file_view_mode']])) {
          $instance['display'][$instance_view_mode]['settings']['file_view_mode'] = $view_mode_updates[$display['settings']['file_view_mode']];
          $updated = TRUE;
        }
      }
      if ($updated) {
        field_update_instance($instance);
      }
    }
  }

  // Update formatter settings that reference the old view modes within saved
  // Views.
  if (db_table_exists('views_display')) {
    $result = db_select('views_display', 'v')
      ->fields('v', array(
      'vid',
      'id',
      'display_options',
    ))
      ->execute();
    foreach ($result as $record) {
      if (!empty($record->display_options)) {
        $display_options = unserialize($record->display_options);
        if (_media_update_7204_update_views_display_options($display_options, $view_mode_updates)) {
          db_update('views_display')
            ->fields(array(
            'display_options' => serialize($display_options),
          ))
            ->condition('vid', $record->vid)
            ->condition('id', $record->id)
            ->execute();
        }
      }
    }
  }

  // Update formatter settings that reference the old view modes within unsaved
  // Views in the CTools object cache. Objects in the CTools cache are instances
  // of classes, so the Views module must be enabled to unserialize it
  // correctly.
  if (db_table_exists('ctools_object_cache') && module_exists('views')) {
    $result = db_select('ctools_object_cache', 'c')
      ->fields('c', array(
      'sid',
      'name',
      'obj',
      'data',
    ))
      ->condition('obj', 'view')
      ->execute();
    foreach ($result as $record) {
      $view = unserialize($record->data);
      if (!empty($view->display)) {
        $updated = FALSE;
        foreach ($view->display as $display_name => $display) {
          if (!empty($display->display_options) && _media_update_7204_update_views_display_options($display->display_options, $view_mode_updates)) {
            $updated = TRUE;
          }
        }
        if ($updated) {
          db_update('ctools_object_cache')
            ->fields(array(
            'data' => serialize($view),
          ))
            ->condition('sid', $record->sid)
            ->condition('name', $record->name)
            ->condition('obj', $record->obj)
            ->execute();
        }
      }
    }
  }

  // Clear caches that might contain stale Views displays.
  if (module_exists('views')) {
    cache_clear_all('*', 'cache_views', TRUE);
    cache_clear_all('*', 'cache_views_data', TRUE);
  }
  if (module_exists('block')) {
    cache_clear_all('*', 'cache_block', TRUE);
  }
  cache_clear_all('*', 'cache_page', TRUE);

  // We still have the old media_link and media_original view modes that must be
  // supported for now.
  // @TODO: Make this apply only to updates from Media 1.x.
  // @see media_entity_info_alter()
  variable_set('media__show_deprecated_view_modes', TRUE);
}