You are here

multipurpose_corporate_profile.install in Multipurpose Corporate Profile 8

Same filename and directory in other branches
  1. 7 multipurpose_corporate_profile.install

Installing file.

Install, update and uninstall functions for the multipurpose_corporate_profile.

File

multipurpose_corporate_profile.install
View source
<?php

/**
 * @file
 * Installing file.
 *
 * Install, update and uninstall functions
 * for the multipurpose_corporate_profile.
 */
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;

/**
 * Implements hook_install().
 *
 * Perform actions to set up the site for this profile.
 *
 * @see system_install()
 */
function multipurpose_corporate_profile_install() {

  // First, do everything in standard profile.
  include_once DRUPAL_ROOT . '/core/profiles/standard/standard.install';
  standard_install();

  // Set front page to "node".
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.front', '/home')
    ->save(TRUE);

  // Allow visitor account creation with administrative approval.
  $user_settings = \Drupal::configFactory()
    ->getEditable('user.settings');
  $user_settings
    ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
    ->save(TRUE);

  // Enable default permissions for system roles.
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'search content',
  ]);
  user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
    'search content',
  ]);

  // Assign user 1 the "administrator" role.
  $user = User::load(1);
  $user->roles[] = 'administrator';
  $user
    ->save();
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'access site-wide contact form',
  ]);
  user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
    'access site-wide contact form',
  ]);

  // Allow authenticated users to use shortcuts.
  user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
    'access shortcuts',
  ]);

  // Enable the admin theme.
  \Drupal::configFactory()
    ->getEditable('node.settings')
    ->set('use_admin_theme', TRUE)
    ->save(TRUE);

  // Set front page of the site.
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.front', '/home')
    ->save(TRUE);
  multipurpose_corporate_profile_public_files_copy();
  multipurpose_corporate_profile_libraries_files_copy();
  multipurpose_corporate_profile_theme_templates_files_copy();

  // Enable features and default content modules.
  multipurpose_corporate_profile_install_custom_modules();

  // Setting aliases for nodes.
  multipurpose_corporate_profile_set_aliases();
}

/**
 * Installing custom modules.
 */
function multipurpose_corporate_profile_install_custom_modules() {
  $modules = [];
  if (!Drupal::moduleHandler()
    ->moduleExists('feature_config')) {
    $modules[] = 'feature_config';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('feature_views')) {
    $modules[] = 'feature_views';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('feature_blocks')) {
    $modules[] = 'feature_blocks';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('exported_terms')) {
    $modules[] = 'exported_terms';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('exported_files')) {
    $modules[] = 'exported_files';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('exported_nodes')) {
    $modules[] = 'exported_nodes';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('exported_menus')) {
    $modules[] = 'exported_menus';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('exported_block_content')) {
    $modules[] = 'exported_block_content';
  }
  if (!Drupal::moduleHandler()
    ->moduleExists('feature_pathauto')) {
    $modules[] = 'feature_pathauto';
  }
  if (!empty($modules)) {
    \Drupal::service('module_installer')
      ->install($modules);
  }
}

/**
 * Setting alias.
 */
function multipurpose_corporate_profile_set_aliases() {
  $path = \Drupal::service('path.alias_storage')
    ->save('/node/2', '/typography', 'en');
  $path = \Drupal::service('path.alias_storage')
    ->save('/node/3', '/about-us', 'en');
  $path = \Drupal::service('path.alias_storage')
    ->save('/node/4', '/alerts', 'en');
  $path = \Drupal::service('path.alias_storage')
    ->save('/node/6', '/contact-us', 'en');
  $path = \Drupal::service('path.alias_storage')
    ->save('/node/51', '/pricing-tables', 'en');
}

/**
 * Copy public files for default theme.
 */
function multipurpose_corporate_profile_public_files_copy() {
  $source = 'profiles/multipurpose_corporate_profile/files/';
  $res = 'sites/default/files/';
  multipurpose_corporate_profile_recurse_copy($source, $res);
}

/**
 * Copy libraries files for our profile.
 */
function multipurpose_corporate_profile_libraries_files_copy() {
  $source = 'profiles/multipurpose_corporate_profile/libraries/';
  $res = 'profiles/multipurpose_corporate_profile/sites/all/libraries/';
  multipurpose_corporate_profile_recurse_copy($source, $res);
}

/**
 * Copy custom templates into theme.
 */
function multipurpose_corporate_profile_theme_templates_files_copy() {
  $source = 'profiles/multipurpose_corporate_profile/templates/';
  $res = 'profiles/multipurpose_corporate_profile/themes/multipurpose_corporate_theme/templates/';
  multipurpose_corporate_profile_recurse_copy($source, $res);
}

/**
 * Recursive copy.
 *
 * @param string $src
 *   - Source folder with files.
 * @param string $dst
 *   - Destination folder.
 */
function multipurpose_corporate_profile_recurse_copy($src, $dst) {
  $dir = opendir($src);
  @mkdir($dst);
  while (FALSE !== ($file = readdir($dir))) {
    if ($file != '.' && $file != '..') {
      if (is_dir($src . '/' . $file)) {
        multipurpose_corporate_profile_recurse_copy($src . '/' . $file, $dst . '/' . $file);
      }
      else {
        copy($src . '/' . $file, $dst . '/' . $file);
      }
    }
  }
  closedir($dir);
}