You are here

agreement.module in Agreement 3.0.x

Agreement module code - agreement.module.

Module allows the administrator to force a user role to accept an agreement before accessing any site content.

File

agreement.module
View source
<?php

/**
 * @file
 * Agreement module code - agreement.module.
 *
 * Module allows the administrator to force a user role to accept an agreement
 * before accessing any site content.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;

/**
 * Implements hook_user_update().
 */
function agreement_user_update(EntityInterface $account) {

  /** @var \Drupal\user\Entity\User $account */
  if ($account
    ->getEntityTypeId() === 'user') {
    $current_user = \Drupal::currentUser();
    $agreements = \Drupal::entityTypeManager()
      ->getStorage('agreement')
      ->loadMultiple();

    /** @var \Drupal\agreement\Entity\Agreement $agreement */
    foreach ($agreements as $agreement) {

      // Do not require user to re-accept agreement if they've just changed
      // their password.
      $settings = $agreement
        ->getSettings();
      if ($settings['frequency'] === 0 && isset($account->original) && $account
        ->checkExistingPassword($account->original) && $current_user
        ->id() === $account
        ->id()) {
        \Drupal::service('agreement.handler')
          ->agree($account, $agreement, 2);
      }
    }
  }
}

/**
 * Implements hook_mail().
 */
function agreement_mail($key, &$message, $params) {
  if (isset($message['langcode'])) {
    $langcode = $message['langcode'];
  }
  elseif (isset($message['language'])) {
    $langcode = $message['language']->language;
  }
  else {
    $language_manager = \Drupal::service('language_manager');
    $langcode = $language_manager
      ->getDefaultLanguage()->language;
  }
  switch ($key) {
    case 'notice':
      $variables = _agreement_get_mail_variables($params);
      $message['subject'] = t('%site_name: %username accepted %agreement', $variables, [
        'langcode' => $langcode,
      ]);
      $message['body'][] = t('The user has accepted the agreement, %agreement.', $variables);
      break;
    case 'revoked':
      $variables = _agreement_get_mail_variables($params);
      $message['subject'] = t('%site_name: %username revoked acceptance of %agreement', $variables, [
        'langcode' => $langcode,
      ]);
      $message['body'][] = t('The user has revoked their acceptance of the agreement, %agreement.', $variables);
      break;
  }
}

/**
 * Gets the agreement types for views.
 *
 * @return array
 *   An associative array of agreement types and labels.
 */
function agreement_get_agreement_options() {
  $options = [];
  try {
    $agreements = \Drupal::entityTypeManager()
      ->getStorage('agreement')
      ->loadMultiple();
    foreach ($agreements as $id => $agreement) {
      $options[$agreement
        ->id()] = $agreement
        ->label();
    }
  } catch (InvalidPluginDefinitionException $e) {
    \Drupal::messenger()
      ->addError(t('Unable to load agreement types.'));
  }
  return $options;
}

/**
 * Gets mail variables based on parameters sent to agreement_mail().
 *
 * @param array $params
 *   The mail parameters.
 *
 * @return array
 *   An array keyed by the placeholder name of corresponding placeholder values.
 *
 * @internal
 */
function _agreement_get_mail_variables(array $params) {

  /** @var \Drupal\Core\Session\AccountInterface $account */
  $account = $params['account'];

  /** @var \Drupal\agreement\Entity\Agreement $agreement */
  $agreement = $params['context']['agreement'];
  return [
    '%site_name' => \Drupal::config('system.site')
      ->get('name'),
    '%username' => $account
      ->getDisplayName(),
    '%agreement' => $agreement
      ->label(),
  ];
}

Functions

Namesort descending Description
agreement_get_agreement_options Gets the agreement types for views.
agreement_mail Implements hook_mail().
agreement_user_update Implements hook_user_update().
_agreement_get_mail_variables Gets mail variables based on parameters sent to agreement_mail().