You are here

slick_views.install in Slick Views 7.3

Installation actions for Slick Views.

File

slick_views.install
View source
<?php

/**
 * @file
 * Installation actions for Slick Views.
 */

/**
 * Rebuild the theme and registry to migrate from Slick 2.x to 3.x.
 *
 * Tasks:<br>
 * - Removed slick-views.tpl.php for theme_slick_wrapper().<br>
 * - Removed slick_views.theme.inc for slick.theme.inc.<br>
 * - Removed SlickViews.inc to Drupal\slick_views\Plugin\views\style\SlickViews.
 */
function slick_views_update_7300() {

  // Prevents potential blocking updates due to file removals.
  // Rebuild theme registry.
  drupal_theme_rebuild();

  // Rebuild Slick Views class registry.
  registry_rebuild();
}

/**
 * Utility function to update formatter and style options within Views displays.
 *
 * @todo use batch for potential large sets of slicks.
 */
function _slick_views_update_style_options(&$display_options) {
  if (!function_exists('_slick_update_formatter_settings')) {
    module_load_include('inc', 'slick', 'includes/slick.update');
  }
  $updated = FALSE;

  // 1. Update style options.
  if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'slick' && isset($display_options['style_options'])) {
    $updated = TRUE;
    _slick_update_formatter_settings($display_options['style_options']);
  }

  // 2. Also update formatters within views.
  if (isset($display_options['fields'])) {
    foreach ($display_options['fields'] as $field_name => $field_display) {
      if (isset($field_display['type']) && $field_display['type'] == 'slick' && isset($field_display['settings'])) {
        $updated = TRUE;
        $field = field_info_field($field_name);

        // Update deprecated settings, and formatter to the new ones.
        _slick_update_formatter_settings($display_options['fields'][$field_name]['settings']);
        $display_options['fields'][$field_name]['type'] = 'slick_' . $field['type'];
      }
    }
  }
  return $updated;
}

/**
 * Utility function to get available Views using Slick Views style plugins.
 */
function _slick_views_get_view_names() {
  if (!db_table_exists('views_view')) {
    return [];
  }

  // Cannot query from database as views may be stored in code.
  // Cannot use vid as nothing in database when stored in code.
  $views = ctools_export_crud_load_all('views_view', TRUE);
  $names = [];
  foreach ($views as $view) {
    if (!empty($view->display)) {
      foreach ($view->display as $display) {
        $display_options = isset($display->display_options) ? $display->display_options : [];
        if ($display_options) {

          // 1. Collect all slick views ids.
          if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'slick') {
            $names[] = $view->name;
          }
          elseif (isset($display_options['fields'])) {
            foreach ($display_options['fields'] as $field_display) {
              if (isset($field_display['type']) && $field_display['type'] == 'slick') {
                $names[] = $view->name;
              }
            }
          }
        }
      }
    }
  }
  $names = $names ? array_values(array_unique($names)) : [];

  // In order for the update to work, we must save exports into database so that
  // we have a View ID (vid) to work with.
  if ($names) {
    foreach ($names as $name) {
      $view = ctools_export_crud_load('views_view', $name);
      ctools_export_crud_save('views_view', $view);
    }
  }
  unset($views);
  return $names;
}

/**
 * Update the deprecated Slick Views options to migrate from Slick 2.x to 3.x.
 *
 * Tasks:<br>
 * - Updated deprecated settings for both Slick Views style and formatters.<br>
 * - Updated deprecated Slick formatter to the new one based on field type.<br>
 */
function slick_views_update_7301() {
  if (!db_table_exists('views_display')) {
    return;
  }
  module_load_include('inc', 'ctools', 'includes/export');
  module_load_include('inc', 'slick', 'includes/slick.update');

  // Update Slick Views deprecated settings to use more generic settings.
  $names = _slick_views_get_view_names();
  if (empty($names)) {
    return;
  }
  $success = FALSE;
  foreach ($names as $name) {
    $view = ctools_export_crud_load('views_view', $name);

    // Code has been saved into database previously, the view which was stored
    // in codebase has now an ID to work with. Here is just in case.
    if (empty($view->vid)) {
      continue;
    }
    foreach ($view->display as &$display) {
      if (!empty($display->display_options) && _slick_views_update_style_options($display->display_options)) {
        db_update('views_display')
          ->fields([
          'display_options' => serialize($display->display_options),
        ])
          ->condition('id', $view->vid)
          ->execute();
        $success = TRUE;
      }
    }

    // Without re-saving the entire view, the above views_display is ignored.
    if ($success) {
      ctools_export_crud_save('views_view', $view);
    }
  }

  // Clear caches that might contain stale Views displays.
  // Rebuild CTools cache for the views_view.
  if ($success) {
    ctools_export_load_object_reset('views_view');
    cache_clear_all('*', 'cache_views', TRUE);
    cache_clear_all('*', 'cache_views_data', TRUE);
    cache_clear_all('*', 'cache_block', TRUE);
    cache_clear_all('*', 'cache_page', TRUE);
  }
  return $success ? t('Slick Views style plugin updated.') : t('Doh! Slick Views failed updating.');
}

Functions

Namesort descending Description
slick_views_update_7300 Rebuild the theme and registry to migrate from Slick 2.x to 3.x.
slick_views_update_7301 Update the deprecated Slick Views options to migrate from Slick 2.x to 3.x.
_slick_views_get_view_names Utility function to get available Views using Slick Views style plugins.
_slick_views_update_style_options Utility function to update formatter and style options within Views displays.