Commit 2b48946f authored by Rajab Natshah's avatar Rajab Natshah
Browse files

Issue #3272182: Switch to use the Module Installer Factory in the Varbase API module

parent 8f031cda
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
    "cweagans/composer-patches": "~1.0",
    "drupal/core": "~9.0",
    "vardot/entity-definition-update-manager": "~1.0",
    "vardot/module-installer-factory": "~1.0",
    "drupal/consumers": "~1.0",
    "drupal/openapi": "~2.0",
    "drupal/openapi_jsonapi": "3.0.2",
+20 −51
Original line number Diff line number Diff line
@@ -5,70 +5,28 @@
 * Contains varbase_api.install.
 */

use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Vardot\Entity\EntityDefinitionUpdateManager;
use Vardot\Installer\ModuleInstallerFactory;

/**
 * Implements hook_install().
 */
function varbase_api_install() {
  $module_handler = \Drupal::moduleHandler();
  $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);
    }
  }

  // Processer for install: in varbase_api.info.yml file using ModuleInstallerFactory.
  // ---------------------------------------------------------------------------.
  ModuleInstallerFactory::installList('varbase_api');

  // 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 = \Drupal::service('file_system')->scanDirectory($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);
      }
    }
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_api', '/^field.storage.*\\.(yml)$/i');
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_api', '/^.*(settings.yml)$/i');

    // 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 = \Drupal::service('file_system')->scanDirectory($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);
      }
    }
  }
  // --------------------------------------------------------------------------.
  // If the openapi_ui_redoc and jsonapi modules are installed, give the API
  // documentation a nice path alias.
  if ($module_handler->moduleExists('openapi_ui_redoc')
      && $module_handler->moduleExists('jsonapi')) {
  if (Drupal::service('module_handler')->moduleExists('openapi_ui_redoc')
      && Drupal::service('module_handler')->moduleExists('jsonapi')) {

    $alias_manager = \Drupal::service('path_alias.manager');
    $path = $alias_manager->getPathByAlias('/api-docs');
@@ -183,3 +141,14 @@ function varbase_api_update_8704() {
    \Drupal::service('module_installer')->install(['openapi_rest'], FALSE);
  }
}

/**
 * Issue #3272182: Switch to use the Module Installer Factory.
 *
 * In the Varbase API module.
 */
function varbase_api_update_90001() {
  // Set the weight of the module after installation of list of modules.
  // To make sure that any hook or event subscriber workes after all used modules.
  ModuleInstallerFactory::setModuleWeightAfterInstallation('varbase_api');
}