You are here

password_policy.install in Password Policy 8.3

Install, update and uninstall functions for the password_policy module.

File

password_policy.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the password_policy module.
 */
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
 * Implements hook_install().
 */
function password_policy_install() {

  // Give precedence to modules like prlp or simple_pass_reset.
  module_set_weight('password_policy', 10);

  // Set user password reset timestamp to now.
  $timestamp = \Drupal::service("date.formatter")
    ->format(\Drupal::time()
    ->getRequestTime(), "custom", DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE);
  \Drupal::state()
    ->set('password_policy.install_time', $timestamp);

  // Rebuild user entity form display for new fields.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $user_form_display */
  $user_form_display = $storage
    ->load('user.user.default');
  if (!$user_form_display) {
    $user_form_display = $storage
      ->create([
      'targetEntityType' => 'user',
      'bundle' => 'user',
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $user_form_display
    ->setComponent('field_last_password_reset', [
    'type' => 'datetime_default',
    // Display before contact standard profile field.
    'weight' => 4,
    'settings' => [],
  ])
    ->setComponent('field_password_expiration', [
    'type' => 'boolean_checkbox',
    'weight' => 3,
    'settings' => [
      'display_label' => TRUE,
    ],
  ])
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function password_policy_uninstall() {
  \Drupal::state()
    ->delete('password_policy.install_time');
}

/**
 * Add cron threshold to prevent memory overloads.
 */
function password_policy_update_8201() {
  $config = \Drupal::service('config.factory')
    ->getEditable('password_policy.settings');

  // Set and save new message value.
  $config
    ->set('cron_threshold', '250')
    ->save();
}

/**
 * Modify module weight so that it runs after others.
 */
function password_policy_update_8301() {
  module_set_weight('password_policy', 10);
}

Functions

Namesort descending Description
password_policy_install Implements hook_install().
password_policy_uninstall Implements hook_uninstall().
password_policy_update_8201 Add cron threshold to prevent memory overloads.
password_policy_update_8301 Modify module weight so that it runs after others.