Commit be5ae35d authored by Abdullah Yassin's avatar Abdullah Yassin Committed by Rajab Natshah
Browse files

Issue #3270834 by Abdullah Yassin: Switch to use the Module Installer Factory...

Issue #3270834 by Abdullah Yassin: Switch to use the Module Installer Factory with custom install: for the Varbase Core module
parent 0388dec6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@
    "cweagans/composer-patches": "~1.0",
    "drupal/core": "~9.0",
    "vardot/entity-definition-update-manager": "~1.0",
    "vardot/module-installer-factory": "~1.0",
    "drupal/config_update": "~1.0",
    "drupal/config_ignore": "~2.0",
    "drupal/config_inspector": "~1.0",
+8 −49
Original line number Diff line number Diff line
@@ -7,12 +7,11 @@

use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
use Drupal\shortcut\Entity\Shortcut;
use Vardot\Entity\EntityDefinitionUpdateManager;
use Vardot\Installer\ModuleInstallerFactory;
use Drupal\user\UserInterface;

/**
@@ -20,57 +19,14 @@ use Drupal\user\UserInterface;
 */
function varbase_admin_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.
  // Processer for install: in varbase_admin.info.yml file using ModuleInstallerFactory.
  // ---------------------------------------------------------------------------.
  $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'])) {

      foreach ($module_info_data['install'] as $module) {
        if (!\Drupal::moduleHandler()->moduleExists($module)) {
          \Drupal::service('module_installer')->install([$module], TRUE);
        }
      }
    }
  }
  ModuleInstallerFactory::installList('varbase_admin');

  // 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_admin', '/^field.storage.*\\.(yml)$/i');
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_admin', '/^.*(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);
      }
    }
  }
  // ---------------------------------------------------------------------------
  // Assign user 1 the "administrator" role.
  $user = User::load(1);
@@ -139,6 +95,9 @@ function varbase_admin_install() {
    'views.view.user_admin_people',
  ];

  $module_path = Drupal::service('module_handler')->getModule('varbase_admin')->getPath();
  $optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;

  foreach ($forced_configs_import_after_entity_updates as $config_name) {
    $config_path = $optional_install_path . '/' . $config_name . '.yml';
    $config_content = file_get_contents($config_path);
+5 −46
Original line number Diff line number Diff line
@@ -5,11 +5,8 @@
 * Install, update and uninstall functions for the Varbase Default content.
 */

use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Vardot\Entity\EntityDefinitionUpdateManager;
use Vardot\Installer\ModuleInstallerFactory;
use Drupal\path_alias\Entity\PathAlias;

/**
@@ -17,52 +14,14 @@ use Drupal\path_alias\Entity\PathAlias;
 */
function varbase_default_content_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.
  // Processer for install: in varbase_default_content.info.yml file using ModuleInstallerFactory.
  // ---------------------------------------------------------------------------.
  $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);
    }
  }
  ModuleInstallerFactory::installList('varbase_default_content');

  // 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_default_content', '/^field.storage.*\\.(yml)$/i');
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_default_content', '/^.*(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);
      }
    }
  }
  // ---------------------------------------------------------------------------
  // Entity updates to clear up any mismatched entity and/or field definitions
  // And Fix changes were detected in the entity type and field definitions.
+5 −51
Original line number Diff line number Diff line
@@ -5,68 +5,22 @@
 * Install, update and uninstall functions for the Varbase Development.
 */

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

/**
 * Implements hook_install().
 */
function varbase_development_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.
  // Processer for install: in varbase_development.info.yml file using ModuleInstallerFactory.
  // --------------------------------------------------------------------------.
  $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'])) {

      foreach ($module_info_data['install'] as $module) {
        if (!\Drupal::moduleHandler()->moduleExists($module)) {
          \Drupal::service('module_installer')->install([$module], TRUE);
        }
      }
    }
  }
  ModuleInstallerFactory::installList('varbase_development');

  // 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);
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_development', '/^field.storage.*\\.(yml)$/i');
  ModuleInstallerFactory::importConfigsFromScanedDirectory('varbase_development', '/^.*(settings.yml)$/i');

    // 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);
      }
    }

    // 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);
      }
    }
  }
  // --------------------------------------------------------------------------.
  // Entity updates to clear up any mismatched entity and/or field definitions
  // And Fix changes were detected in the entity type and field definitions.
+4 −18
Original line number Diff line number Diff line
@@ -5,32 +5,18 @@
 * Contains install and update for Varbase Internationalization module.
 */

use Symfony\Component\Yaml\Yaml;
use Vardot\Entity\EntityDefinitionUpdateManager;
use Vardot\Installer\ModuleInstallerFactory;

/**
 * Implements hook_install().
 */
function varbase_internationalization_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.
  // Processer for install: in varbase_internationalization.info.yml file using ModuleInstallerFactory.
  // ---------------------------------------------------------------------------.
  $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'])) {

      foreach ($module_info_data['install'] as $module) {
        if (!\Drupal::moduleHandler()->moduleExists($module)) {
          \Drupal::service('module_installer')->install([$module], TRUE);
        }
      }
    }
  }
  ModuleInstallerFactory::installList('varbase_internationalization');

  // ---------------------------------------------------------------------------
  // Entity updates to clear up any mismatched entity and/or field definitions
  // And Fix changes were detected in the entity type and field definitions.
Loading