<?php
/**
 * @file
 * Install, update and uninstall functions for the phpedu_profile installation
 * profile.
 */

  use Drupal\user\Entity\User;
  use Drupal\user\RoleInterface;
  use Drupal\shortcut\Entity\Shortcut;

  /**
   * Implements hook_install().
   *
   * Perform actions to set up the site for Phpedu Profile.
   *
   * @see system_install()
   */
  function phpedu_profile_install() {
    // Set front page to "node".
    \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',
      USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save(TRUE);

    // Enable default permissions for system roles.
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID,
      array('access content', 'access comments', 'search content'));
    user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID,
      array('access content', 'access comments', 'post comments', 'skip comment approval', 'access shortcuts', 'search content'));

    // Assign user 1 the "administrator" role.
    $user          = User::load(1);
    $user->roles[] = 'administrator';
    $user->save();

    // Restrict user registration to admin role creation
    \Drupal::configFactory()
           ->getEditable('user.settings')
           ->set('register', USER_REGISTER_ADMINISTRATORS_ONLY)
           ->save(TRUE);

    // 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::create(
      [
        'shortcut_set' => 'default',
        'title' => t('Content library'),
        'weight' => 1,
        'link' => ['uri' => 'internal:/admin/content'],
      ]
    )->save();
    Shortcut::create(
      [
        'shortcut_set' => 'default',
        'title' => t('Media library'),
        'weight' => 2,
        'link' => ['uri' => 'internal:/admin/content/media'],
      ]
    )->save();

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


/**
 * Implements hook_install_tasks_alter().
 */
function phpedu_profile_install_tasks_alter(&$tasks, $install_state) {
  // Install only in English.
  $tasks['install_select_language']['display']      = FALSE;
  $tasks['install_select_language']['run'] = INSTALL_TASK_SKIP;
  $tasks['install_download_translation']['run'] = INSTALL_TASK_SKIP;
  $tasks['install_profile_modules']['display_name'] = t('Install PhpEdu');
}
