You are here

varbase_core.install in Varbase Core 8.6

Install, update and uninstall functions for the Varbase core module.

File

varbase_core.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Varbase core module.
 */
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\update_helper_checklist\Entity\Update;

/**
 * Implements hook_install().
 */
function varbase_core_install() {
  $module_name = preg_replace('/_install$/', '', __FUNCTION__);
  $module_path = Drupal::service('module_handler')
    ->getModule($module_name)
    ->getPath();

  // Processer for install: in [$module_name].info.yml file.
  // --------------------------------------------------------------------------.
  $module_info_file = $module_path . '/' . $module_name . '.info.yml';
  if (file_exists($module_info_file)) {
    $module_info_data = (array) Yaml::parse(file_get_contents($module_info_file));
    if (isset($module_info_data['install']) && is_array($module_info_data['install'])) {
      \Drupal::service('module_installer')
        ->install($module_info_data['install'], TRUE);
    }
  }

  // Install optional configs.
  $optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
  if (is_dir($optional_install_path)) {
    $config_installer = \Drupal::service('config.installer');
    $config_installer
      ->installDefaultConfig('module', $module_name);

    // Create field storage configs first in active config.
    $storage_config_files = file_scan_directory($optional_install_path, '/^field.storage.*\\.(yml)$/i');
    if (isset($storage_config_files) && is_array($storage_config_files)) {
      foreach ($storage_config_files as $storage_config_file) {
        $storage_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $storage_config_file->uri);
        $storage_config_file_data = (array) Yaml::parse($storage_config_file_content);
        $config_factory = \Drupal::configFactory()
          ->getEditable($storage_config_file->name);
        $config_factory
          ->setData($storage_config_file_data)
          ->save(TRUE);
      }
    }

    // Install any optional config the module provides.
    $storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
    $config_installer
      ->installOptionalConfig($storage, '');

    // Have the .settings.yml configs into the active config.
    $settings_config_files = file_scan_directory($optional_install_path, '/^.*\\(settings.yml)$/i');
    if (isset($settings_config_files) && is_array($settings_config_files)) {
      foreach ($settings_config_files as $settings_config_file) {
        $settings_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $settings_config_file->uri);
        $settings_config_file_data = (array) Yaml::parse($settings_config_file_content);
        $config_factory = \Drupal::configFactory()
          ->getEditable($settings_config_file->name);
        $config_factory
          ->setData($settings_config_file_data)
          ->save(TRUE);
      }
    }
  }

  // --------------------------------------------------------------------------.
}

/**
 * Enable [Varbase Update Helper] module.
 */
function varbase_core_update_8600() {
  if (!\Drupal::moduleHandler()
    ->moduleExists('varbase_update_helper')) {
    \Drupal::service('module_installer')
      ->install([
      'checklistapi',
      'update_helper',
      'update_helper_checklist',
      'varbase_update_helper',
    ], FALSE);
  }
}

/**
 * Enhance the usability of admin views (Content, Media, and People).
 */
function varbase_core_optional_update_8601($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8601', $force);

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable Blazy loading for most media sizes (Image, Video, Remote Video).
 */
function varbase_core_optional_update_8602($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8602', $force);

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable Varbase Email.
 */
function varbase_core_optional_update_8603($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8603');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable colored length indicator for Varbase Basic page.
 */
function varbase_core_optional_update_8604($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8604');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable colored length indicator for Varbase Landing page.
 */
function varbase_core_optional_update_8605($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8605');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable colored length indicator for Varbase Hero Slider.
 */
function varbase_core_optional_update_8606($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8606');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable colored length indicator for Varbase Blog.
 */
function varbase_core_optional_update_8607($force = FALSE) {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8607');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Change and shift Varbase Media responsive image styles to work with.
 *
 * The mapping of Bootstrap 3 breakpoints into Bootstrap 4 breakpoints.
 */
function varbase_core_update_8608() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_update_8608');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Rename config updates in Varbase Update Helper to match with new.
 *
 * Update ids standard naming conventions.
 */
function varbase_core_update_8609() {

  // Mapping old update ids naming conventions.
  $map_old_to_new_update_ids = [
    'varbase_core:varbase_admin_vbo_8601' => 'varbase_core:varbase_core_optional_update_8601',
    'varbase_core:varbase_media_blazy_8602' => 'varbase_core:varbase_core_optional_update_8602',
    'varbase_core:varbase_email_enable_8603' => 'varbase_core:varbase_core_optional_update_8603',
    'varbase_core:varbase_page_li_8604' => 'varbase_core:varbase_core_optional_update_8604',
    'varbase_core:varbase_landing_li_8605' => 'varbase_core:varbase_core_optional_update_8605',
    'varbase_core:varbase_heroslider_media_li_8606' => 'varbase_core:varbase_core_optional_update_8606',
    'varbase_core:varbase_blog_li_8607' => 'varbase_core:varbase_core_optional_update_8607',
    'varbase_core:varbase_media_bs3_to_bs4_8608' => 'varbase_core:varbase_core_update_8608',
  ];

  // Check the old update ids and change them with new update ids.
  foreach ($map_old_to_new_update_ids as $old_update_id => $new_update_id) {

    // If we do have the old update id in the system.
    if ($old_update = Update::load($old_update_id)) {

      // Create the new update id if it was not in the system.
      $new_update = Update::load($new_update_id);
      if (isset($new_update) && !$new_update) {
        Update::create([
          'id' => $new_update_id,
          'successful_by_hook' => $old_update
            ->get('successful_by_hook'),
        ])
          ->save();
      }

      // Delete old update id.
      $old_update
        ->delete();
    }
  }
}

/**
 * Fix regression of responsive image styles when updating.
 *
 * To Bootstrap 4 breakpoints.
 */
function varbase_core_update_8610() {

  // Force import of responsive image styles.
  $responsive_image_styles_config_names = [
    'responsive_image.styles.d01',
    'responsive_image.styles.d01_square',
    'responsive_image.styles.d01_standard',
    'responsive_image.styles.d01_traditional',
    'responsive_image.styles.d02',
    'responsive_image.styles.d02_square',
    'responsive_image.styles.d02_standard',
    'responsive_image.styles.d02_traditional',
    'responsive_image.styles.d03',
    'responsive_image.styles.d03_square',
    'responsive_image.styles.d03_standard',
    'responsive_image.styles.d03_traditional',
    'responsive_image.styles.d04',
    'responsive_image.styles.d04_square',
    'responsive_image.styles.d04_standard',
    'responsive_image.styles.d04_traditional',
    'responsive_image.styles.d05',
    'responsive_image.styles.d05_square',
    'responsive_image.styles.d05_standard',
    'responsive_image.styles.d05_traditional',
    'responsive_image.styles.d06',
    'responsive_image.styles.d06_square',
    'responsive_image.styles.d06_standard',
    'responsive_image.styles.d06_traditional',
    'responsive_image.styles.d07',
    'responsive_image.styles.d07_square',
    'responsive_image.styles.d07_standard',
    'responsive_image.styles.d07_traditional',
    'responsive_image.styles.d08',
    'responsive_image.styles.d08_square',
    'responsive_image.styles.d08_standard',
    'responsive_image.styles.d08_traditional',
    'responsive_image.styles.d09',
    'responsive_image.styles.d09_square',
    'responsive_image.styles.d09_standard',
    'responsive_image.styles.d09_traditional',
    'responsive_image.styles.d10',
    'responsive_image.styles.d10_square',
    'responsive_image.styles.d10_standard',
    'responsive_image.styles.d10_traditional',
    'responsive_image.styles.d11',
    'responsive_image.styles.d11_square',
    'responsive_image.styles.d11_standard',
    'responsive_image.styles.d11_traditional',
    'responsive_image.styles.d12',
    'responsive_image.styles.d12_square',
    'responsive_image.styles.d12_standard',
    'responsive_image.styles.d12_traditional',
    'responsive_image.styles.de2e',
    'responsive_image.styles.de2e_4_1',
    'responsive_image.styles.de2e_square',
    'responsive_image.styles.de2e_standard',
    'responsive_image.styles.de2e_traditional',
    'responsive_image.styles.default',
  ];
  $module_path = Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();
  $optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
  foreach ($responsive_image_styles_config_names as $config_name) {
    $config_path = $optional_install_path . '/' . $config_name . '.yml';
    $config_content = file_get_contents($config_path);
    $config_data = (array) Yaml::parse($config_content);
    $config_factory = \Drupal::configFactory()
      ->getEditable($config_name);
    $config_factory
      ->setData($config_data)
      ->save(TRUE);
  }

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_update_8610');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

Functions

Namesort descending Description
varbase_core_install Implements hook_install().
varbase_core_optional_update_8601 Enhance the usability of admin views (Content, Media, and People).
varbase_core_optional_update_8602 Enable Blazy loading for most media sizes (Image, Video, Remote Video).
varbase_core_optional_update_8603 Enable Varbase Email.
varbase_core_optional_update_8604 Enable colored length indicator for Varbase Basic page.
varbase_core_optional_update_8605 Enable colored length indicator for Varbase Landing page.
varbase_core_optional_update_8606 Enable colored length indicator for Varbase Hero Slider.
varbase_core_optional_update_8607 Enable colored length indicator for Varbase Blog.
varbase_core_update_8600 Enable [Varbase Update Helper] module.
varbase_core_update_8608 Change and shift Varbase Media responsive image styles to work with.
varbase_core_update_8609 Rename config updates in Varbase Update Helper to match with new.
varbase_core_update_8610 Fix regression of responsive image styles when updating.