You are here

varbase_default_content.install in Varbase Core 8.8

Install, update and uninstall functions for the Varbase Default content.

File

modules/varbase_default_content/varbase_default_content.install
View source
<?php

/**
 * @file
 * 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 Drupal\varbase\Entity\VarbaseEntityDefinitionUpdateManager;
use Drupal\path_alias\Entity\PathAlias;

/**
 * Implements hook_install().
 */
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.
  // ---------------------------------------------------------------------------.
  $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 = \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.
  \Drupal::classResolver()
    ->getInstanceFromDefinition(VarbaseEntityDefinitionUpdateManager::class)
    ->applyUpdates();

  // Set front page to "/node".
  // Issue #3188641: Change the set front page to "/node" process from
  // using static node id to front page path by the alias.
  // https://www.drupal.org/project/varbase_core/issues/3188641
  try {
    $path_alias_query = \Drupal::entityQuery('path_alias');
    $path_alias_query
      ->condition('alias', '/node', '=');
    $alias_ids = $path_alias_query
      ->execute();
    if (count($alias_ids) > 0) {
      foreach ($alias_ids as $alias_id) {
        if (!end($alias_ids)) {
          $path_alias = PathAlias::load($alias_id);
          $path_alias
            ->delete();
        }
        else {
          $page_front_path = PathAlias::load($alias_id)
            ->getPath();
          \Drupal::configFactory()
            ->getEditable('system.site')
            ->set('page.front', $page_front_path)
            ->save();
        }
      }
    }
  } catch (\Exception $e) {
    \Drupal::messenger()
      ->addError($e
      ->getMessage());
  }

  // Clear cached definitions.
  \Drupal::service('plugin.cache_clearer')
    ->clearCachedDefinitions();
  \Drupal::service('plugin.manager.menu.contextual_link')
    ->clearCachedDefinitions();
  \Drupal::service('plugin.manager.menu.local_task')
    ->clearCachedDefinitions();
  \Drupal::service('plugin.manager.menu.local_action')
    ->clearCachedDefinitions();

  // Invalidating.
  \Drupal::service('cache.menu')
    ->invalidateAll();
  \Drupal::service('cache.render')
    ->invalidateAll();

  // Rebuilding.
  \Drupal::service('plugin.manager.menu.link')
    ->rebuild();

  // Rebuild permissions. The content access permissions need to be rebuilt.
  node_access_rebuild();

  // Rebuild the menu router based on all rebuilt data.
  // Important: This rebuild must happen last, so the menu router is guaranteed
  // to be based on up to date information.
  \Drupal::service('router.builder')
    ->rebuild();

  // Resets all static caches.
  drupal_static_reset();
}

Functions