You are here

module_blacklist.install in Module Blacklist 8

Install, update and uninstall functions for the Module Blacklist module.

File

module_blacklist.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Module Blacklist module.
 */
use Drupal\Core\Site\Settings;
use Drupal\module_blacklist\Exception\ModuleInstallationBlocked;

/**
 * Implements hook_install().
 */
function module_blacklist_install() {
  if (!\Drupal::isConfigSyncing()) {
    return;
  }

  // The hook_requirements() is not automatically called during the module
  // installation by config management, so it is necessary to call it manually.
  // The requirement is to fail the installation if blacklisted modules were
  // installed before Module Blacklist.
  $requirements = \Drupal::moduleHandler()
    ->invoke('module_blacklist', 'requirements', [
    'install',
  ]);
  if (!$requirements) {
    return;
  }

  // Hard fail installation.
  $exception_message = $requirements['blacklisted_modules_discovery']['description']
    ->render();
  throw new ModuleInstallationBlocked($exception_message);
}

/**
 * Implements hook_requirements().
 */
function module_blacklist_requirements($phase) {
  if ($phase !== 'install') {
    return [];
  }
  $blacklisted_modules = Settings::get('module_blacklist');
  if (!$blacklisted_modules) {
    return [];
  }
  $enabled_modules = array_keys(\Drupal::moduleHandler()
    ->getModuleList());
  $blacklisted_modules_enabled = array_intersect($enabled_modules, $blacklisted_modules);
  if (!$blacklisted_modules_enabled) {
    return [];
  }
  $requirements = [
    'blacklisted_modules_discovery' => [
      'description' => t('You need to uninstall the blacklisted module(s) @modules before installing Module Blacklist.', [
        '@modules' => implode(',', $blacklisted_modules_enabled),
      ]),
      'severity' => REQUIREMENT_ERROR,
    ],
  ];
  return $requirements;
}