user_api-1.0.0-beta1/modules/user_api_passwordless/user_api_passwordless.module

modules/user_api_passwordless/user_api_passwordless.module
<?php

/**
 * @file
 * Module file.
 */

declare(strict_types=1);

use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Update translation config.
 *
 * Update config_translation definitions to allow translation
 * of user_api_passwordless emails in user admin form.
 */
function user_api_passwordless_config_translation_info(&$definitions) {
  if (array_key_exists('entity.user.admin_form', $definitions)) {
    $definitions['entity.user.admin_form']['names'][] = 'user_api_passwordless.settings';
    $definitions['entity.user.admin_form']['names'][] = 'user_api_passwordless.mail';
  }
}

/**
 * Implements hook_form_user_admin_settings_alter().
 */
function user_api_passwordless_form_user_admin_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  $mailConfig = \Drupal::config('user_api_passwordless.mail');

  $form['unset_password'] = [
    '#type' => 'details',
    '#title' => new TranslatableMarkup('Unset Password'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'email',
    '#weight' => 13,
  ];
  $form['unset_password']['user_api_passwordless_unset_password_subject'] = [
    '#type' => 'textfield',
    '#title' => new TranslatableMarkup('Subject'),
    '#default_value' => $mailConfig->get('unset_password.subject'),
    '#maxlength' => 180,
  ];
  $form['unset_password']['user_api_passwordless_unset_password_body'] = [
    '#type' => 'textarea',
    '#title' => new TranslatableMarkup('Body'),
    '#default_value' => $mailConfig->get('unset_password.body'),
    '#rows' => 12,
  ];

  $form['#submit'][] = '_user_api_passwordless_form_user_admin_settings_submit';
}

/**
 * Custom form submit handler for user admin form.
 */
function _user_api_passwordless_form_user_admin_settings_submit(array &$form, FormStateInterface $form_state) {
  \Drupal::configFactory()->getEditable('user_api_passwordless.mail')
    ->set('unset_password', [
      'subject' => $form_state->getValue('user_api_passwordless_unset_password_subject'),
      'body' => $form_state->getValue('user_api_passwordless_unset_password_body'),
    ])
    ->save();
}

/**
 * Implements hook_mail().
 */
function user_api_passwordless_mail($key, &$message, $params) {
  $token_service = \Drupal::token();
  $language_manager = \Drupal::languageManager();
  $langcode = $message['langcode'];
  $variables = ['user' => $params['account']];

  $language = $language_manager->getLanguage($langcode);
  $original_language = $language_manager->getConfigOverrideLanguage();
  $language_manager->setConfigOverrideLanguage($language);
  $mail_config = \Drupal::config('user_api_passwordless.mail');

  $token_options = [
    'langcode' => $langcode,
    'callback' => 'user_mail_tokens',
    'clear' => TRUE,
  ];
  $message['subject'] .= PlainTextOutput::renderFromHtml($token_service->replace($mail_config->get($key . '.subject'), $variables, $token_options));
  $message['body'][] = $token_service->replace($mail_config->get($key . '.body'), $variables, $token_options);

  $language_manager->setConfigOverrideLanguage($original_language);
}

/**
 * Custom user email notification.
 *
 * This is basically the same as _user_mail_notify() but altered
 * to work with the emails introduced by this module.
 *
 * @param string $op
 *   The operation being performed on the account. Possible values:
 *   - 'unset_password': E-Mail to verify password removal.
 * @param \Drupal\Core\Session\AccountInterface $account
 *   The user object of the account being notified. Must contain at
 *   least the fields 'uid', 'name', and 'mail'.
 *
 * @return array
 *   An array containing various information about the message.
 *   See \Drupal\Core\Mail\MailManagerInterface::mail() for details.
 *
 * @see _user_mail_notify()
 */
function _user_api_passwordless_mail_notify($op, AccountInterface $account) {
  $params['account'] = $account;
  $langcode = $account->getPreferredLangcode();
  // Get the custom site notification email to use as the from email address
  // if it has been set.
  $site_mail = \Drupal::config('system.site')->get('mail_notification');
  // If the custom site notification email has not been set, we use the site
  // default for this.
  if (empty($site_mail)) {
    $site_mail = \Drupal::config('system.site')->get('mail');
  }
  if (empty($site_mail)) {
    $site_mail = ini_get('sendmail_from');
  }

  $mail = \Drupal::service('plugin.manager.mail')->mail('user_api_passwordless', $op, $account->getEmail(), $langcode, $params, $site_mail);

  return empty($mail) ? NULL : $mail['result'];
}

/**
 * Implements hook_magic_code_user_mail_token_operations_alter().
 */
function user_api_passwordless_magic_code_user_mail_token_operations_alter(&$operations) {
  $operations[] = 'unset-password';
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc