Commit 404bbdd2 authored by Oleh Vehera's avatar Oleh Vehera Committed by Edouard Cunibil
Browse files

Issue #2489232: Port Multiple E-mail Addresses to Drupal 8/9

parent 4e972667
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
confirmation:
  subject: 'Confirm your e-mail address at [site:name]'
  subject: 'Confirm your email address at [site:name]'
  body: |
    [user:name],
    [user:display-name],

    You have added the e-mail address '!email' to your account at [site:name]. In order to complete the registration of this email, you must confirm it by clicking the link below and entering this confirmation code: !confirm_code
    You have added the email address '[multiple_email:email]' to your account at [site:name]. In order to complete the registration of this email, you must confirm it by clicking the link below and entering this confirmation code: [multiple_email:confirm_code]

    !confirm_url
    [multiple_email:confirm_url]

    If the web address does not appear as a link, you must copy the address out of this email, and paste it into the address bar of your web browser.

    If you do not confirm this e-mail in !confirm_deadline, it will be unregistered from your account.
    If you do not confirm this email in [multiple_email:confirm_deadline], it will be unregistered from your account.

    --
    [site:name] team
expiration:
  subject: 'Your e-mail address at [site:name] has expired'
  subject: 'Your email address at [site:name] has expired'
  body: |
    [user:name],
    [user:display-name],

    You have failed to confirm the the e-mail address '!email' within the confirmation period of !confirm_deadline. Therefore, the e-mail address has been removed from your account.
    You have failed to confirm the the email address '[multiple_email:email]' within the confirmation period of [multiple_email:confirm_deadline]. Therefore, the email address has been removed from your account.

    You may add this address again, but you must confirm the address within the specified deadline!

multiple_email.api.php

deleted100644 → 0
+0 −52
Original line number Diff line number Diff line
<?php

/**
 * @file
 * API functions for Multiple Email module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Signal that an email address has been registered by Multiple Email.
 *
 * @param $email
 *   The fully-loaded email object created by Multiple Email.
 */
function hook_multiple_email_register($email) {
  drupal_set_message(t('Do not forget to confirm %email!', array('%email' => $email->mail)));
}

/**
 * Signal that an email address has been confirmed, or un-confirmed.
 *
 * Check $email->confirmed to determine if the email was confirmed or not.
 *
 * @param $email
 *   The fully-loaded email object created by Multiple Email.
 */
function hook_multiple_email_confirm($email) {
  drupal_set_message(t('Now that %email has been confirmed, you may make it your primary address.', array('%email' => $email->mail)));
}

/**
 * Signal that an email address has been deleted by Multiple Email.
 *
 * When an email address is deleted by multiple email, this hook is invoked to
 * notify other modules this has happened. Addresses might also be deleted in
 * multiple_email's implementation of hook_user(), without triggering this
 * hook.
 *
 * @param $eid
 *   Email Address ID.
 */
function hook_multiple_email_delete($eid) {
  db_query("DELETE FROM {mytable} WHERE eid = %d", $eid);
}

/**
 *@} End of "addtogroup hooks".
 */
+1 −1
Original line number Diff line number Diff line
name: 'Multiple E-mail Addresses'
type: module
description: 'Allows users to have more than one registered e-mail address.'
core: 8.x
core_version_requirement: ^8.8 || ^9.0
configure: multiple_email.admin.settings
+36 −121
Original line number Diff line number Diff line
@@ -5,136 +5,51 @@
 * Install file for multiple_email module
 */

use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\multiple_email\EmailInterface;

/**
 * Implements hook_install().
 *
 * Imports current users' emails.
 */
function multiple_email_install() {
// @TODO fix and re-enable
//  db_query("
//    INSERT INTO {multiple_email}
//      (uid, email, time_registered, confirmed)
//    SELECT
//      uid,
//      mail,
//      created,
//      1
//    FROM {users}
//    WHERE uid != 0
//    AND mail != ''
//    GROUP BY mail
//  ");
}
  $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  $email_storage = \Drupal::entityTypeManager()->getStorage('multiple_email');

/**
 * Implements hook_uninstall().
 */
function multiple_email_uninstall() {
  $variables = array(
    'multiple_email_hide_field',
    'multiple_email_edit_emails',
    'multiple_email_password_reset',
    'multiple_email_confirm_deadline',
    'multiple_email_confirm_attempts',
    'multiple_email_confirmation_subject',
    'multiple_email_confirmation_body',
    'multiple_email_expire_subject',
    'multiple_email_expire_body',
  );
  foreach ($variables as $key) {
    variable_del($key);
  }
}
  /** @var \Drupal\user\UserInterface[] $users */
  $users = $user_storage->loadMultiple();

/**
 * Implements hook_enable().
 */
function multiple_email_enable() {
  drupal_set_message(t("Multiple E-mail settings are available under !link", array(
    '!link' => l(t('Administer') . ' > ' . t('Configuration') . ' > ' . t('People') . ' > ' . t('Multiple E-mail'), 'admin/config/people/multiple-email'),
  )));
  foreach ($users as $user) {
    // Skip the anonymous user.
    if ($user->id() == 0) {
      continue;
    }

/**
 * Implements hook_schema().
 */
function multiple_email_schema() {
  $schema['multiple_email'] = array(
    'description' => 'The base table for multiple email.',
    'fields' => array(
      'eid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'email' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'time_registered' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'confirmed' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'confirm_code' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'time_code_generated' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'attempts' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
    ),
    'unique keys' => array(
      'email' => array('email'),
    ),
    'primary key' => array('eid'),
  );
    $email = $email_storage->create([
      'uid' => $user->id(),
      'email' => $user->getEmail(),
      'time_registered' => $user->getCreatedTime(),
      'status' => EmailInterface::CONFIRMED,
    ]);

  return $schema;
    $email->save();
  }

/**
 * Remove email addresses of deleted users
 */
function multiple_email_update_7100() {
  $subquery = db_select('users', 'u');
  $subquery->addField('u', 'uid');
  $query = db_delete('multiple_email');
  $query->condition('uid', $subquery, 'NOT IN');
  $query->execute();
}
  $link_text_parts = [
    new TranslatableMarkup('Administer'),
    new TranslatableMarkup('Configuration'),
    new TranslatableMarkup('People'),
    new TranslatableMarkup('Multiple Email'),
  ];
  $link_text = implode(' > ', $link_text_parts);

/**
 * Give 'administer multiple emails' to all roles having 'administer users'.
 */
function multiple_email_update_7101() {
  $roles = user_roles(FALSE, 'administer users');
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array('administer multiple emails'));
  }
  $link = Link::createFromRoute($link_text, 'multiple_email.admin.settings');

  $message = new TranslatableMarkup('Multiple Email settings are available under @link', [
    '@link' => $link->toString(),
  ]);

  \Drupal::messenger()->addMessage($message);
}
+5 −0
Original line number Diff line number Diff line
multiple_email.admin.settings:
  title: 'Multiple Emails'
  route_name: multiple_email.admin.settings
  description: 'Configure Multiple Email settings.'
  parent: user.admin_index
Loading