You are here

function rest_views_modules_installed in REST Views 2.0.x

Implements hook_modules_installed().

File

./rest_views.module, line 36
Provides a new Views Field plugin for all entity fields.

Code

function rest_views_modules_installed($modules) {

  /** @var \Drupal\Core\Extension\ModuleExtensionList $availableModules */
  $availableModules = Drupal::service('extension.list.module');

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $installedModules */
  $installedModules = Drupal::service('module_handler');

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $moduleInstaller */
  $moduleInstaller = Drupal::service('module_installer');
  $to_install = [];
  $reasons = [];

  // Check all modules in the REST Views package.
  foreach ($availableModules
    ->getAllAvailableInfo() as $name => $info) {
    if ($info['package'] === 'REST Views' && $name !== 'rest_views') {
      $install = FALSE;
      $dependency_names = [];

      // Check if all dependencies are installed, and one of them just now.
      foreach ($info['dependencies'] as $dependency) {
        $dependency = explode(' ', $dependency)[0];
        if (!$installedModules
          ->moduleExists($dependency)) {
          $install = FALSE;
          break;
        }
        if (in_array($dependency, $modules, TRUE)) {
          $install = TRUE;
        }
        if ($dependency !== 'rest_views') {
          $dependency_names[] = $installedModules
            ->getName($dependency);
        }
      }
      if ($install) {
        $to_install[$name] = $availableModules
          ->getName($name);
        $reasons[] = implode(', ', $dependency_names);
      }
    }
    if ($to_install) {
      try {
        $moduleInstaller
          ->install(array_keys($to_install));
        Drupal::messenger()
          ->addStatus(t('REST Views automatically installed @modules to support @dependencies.', [
          '@modules' => implode(', ', $to_install),
          '@dependencies' => implode(', ', $reasons),
        ]));
      } catch (MissingDependencyException $e) {

        // Don't do anything on failure.
        watchdog_exception($e);
      }
    }
  }
}