commerce_kickstart-3.x-dev/commerce_kickstart.install
commerce_kickstart.install
<?php /** * @file * Install, update and uninstall functions for the commerce_base installation profile. */ use Drupal\shortcut\Entity\Shortcut; use Drupal\user\Entity\User; use Drupal\user\RoleInterface; use Drupal\user\UserInterface; /** * Implements hook_install(). * * Perform actions to set up the site for this profile. * * @see system_install() */ function commerce_kickstart_install() { // Set front page to "node" by default. \Drupal::configFactory()->getEditable('system.site')->set('page.front', '/node')->save(TRUE); // Allow visitor account creation with administrative approval. $user_settings = \Drupal::configFactory()->getEditable('user.settings'); $user_settings ->set('register', UserInterface::REGISTER_VISITORS) ->set('verify_mail', FALSE) ->save(TRUE); // Enable default permissions for system roles. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']); user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access comments', 'post comments', 'skip comment approval']); // Assign user 1 the "administrator" role. $user = User::load(1); $user->roles[] = 'administrator'; $user->save(); // Allow authenticated users to use shortcuts. user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access shortcuts']); // Populate the default shortcut set. $shortcut = Shortcut::create([ 'shortcut_set' => 'default', 'title' => t('Add content'), 'weight' => -20, 'link' => ['uri' => 'internal:/node/add'], ]); $shortcut->save(); $shortcut = Shortcut::create([ 'shortcut_set' => 'default', 'title' => t('All content'), 'weight' => -19, 'link' => ['uri' => 'internal:/admin/content'], ]); $shortcut->save(); // Enable the admin theme. \Drupal::configFactory()->getEditable('node.settings')->set('use_admin_theme', TRUE)->save(TRUE); } /** * Implement hook_install_tasks(). */ /** * Implements hook_install_tasks(). */ function commerce_kickstart_install_tasks(&$install_state): array { $tasks = []; $tasks['commerce_kickstart_install_configure_form'] = [ 'display_name' => t('Select features'), 'type' => 'form', 'function' => 'Drupal\commerce_kickstart\Installer\Form\KickstartConfigureForm', ]; $tasks['commerce_kickstart_install_process'] = [ 'display_name' => t('Install features'), 'type' => 'batch', ]; return $tasks; } /** * Implements hook_install_tasks_alter(). */ function commerce_kickstart_install_tasks_alter(&$tasks, $install_state) { $tasks['install_profile_modules']['display_name'] = t('Install Commerce Kickstart'); $tasks['install_configure_form']['display_name'] = t('Configure site'); $tasks['install_finished']['function'] = 'commerce_kickstart_install_finished'; } /** * Installs chosen modules via a batch process. * * @param $install_state * An array of information about the current installation state. * * @return array The batch definition. * The batch definition. */ function commerce_kickstart_install_process(&$install_state) { // Import a currency to speed up initial store setup. // If no default country is set, import the US Dollar, since it's common. $default_country = \Drupal::config('system.date')->get('country.default'); $default_country = $default_country ?: 'US'; $currency_importer = \Drupal::service('commerce_price.currency_importer'); $currency_importer->importByCountry($default_country); $commerce_kickstart_install_demo = \Drupal::state() ->get('commerce_kickstart.install_demo', FALSE); if ($commerce_kickstart_install_demo) { $modules = ['commerce_demo']; } else { $modules = \Drupal::state() ->get('commerce_kickstart.install_modules', []);; } $files = \Drupal::service('extension.list.module')->getList(); // Always install required modules first. Respect the dependencies between // the modules. $required = []; $non_required = []; // Add modules that other modules depend on. foreach ($modules as $module) { if ($files[$module]->requires) { // Create a list of dependencies that haven't been installed yet. $dependencies = array_keys($files[$module]->requires); $dependencies = array_filter($dependencies, '_commerce_kickstart_filter_dependencies'); // Add them to the module list. $modules = array_merge($modules, $dependencies); } } $modules = array_unique($modules); foreach ($modules as $module) { if (!empty($files[$module]->info['required'])) { $required[$module] = $files[$module]->sort; } else { $non_required[$module] = $files[$module]->sort; } } arsort($required); arsort($non_required); $operations = []; foreach ($required + $non_required as $module => $weight) { $operations[] = ['_install_module_batch', [$module, $files[$module]->info['name']]]; } return [ 'operations' => $operations, 'title' => $commerce_kickstart_install_demo ? t('Installing Kickstart Demo') : t('Installing Kickstart Features'), 'error_message' => t('The installation has encountered an error.'), ]; } /** * Performs final installation steps and displays a 'finished' page. * * @param $install_state * An array of information about the current installation state. */ function commerce_kickstart_install_finished(&$install_state) { install_finished($install_state); // Remove the Search API indexing messages, and all the other status messages. $messenger = \Drupal::service('messenger'); $messenger->deleteByType('status'); $warning_messages = $messenger->deleteByType('warning'); $success_message = t('Congratulations, you installed @drupal!', [ '@drupal' => drupal_install_profile_distribution_name(), ]); $messenger->addMessage($success_message); // Re-add the warning messages (after the installation success message). foreach ($warning_messages as $warning_message) { $messenger->addMessage($warning_message, 'warning'); } } /** * array_filter() callback used to filter out already installed dependencies. */ function _commerce_kickstart_filter_dependencies($dependency) { return !\Drupal::moduleHandler()->moduleExists($dependency); }