Commit 3b8b52a3 authored by Purushotam Rai's avatar Purushotam Rai Committed by Purushotam Rai
Browse files

Issue #2651762 by purushotam.rai: Configuration Form - Recovery Password Module

parent 617d532b
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -17,3 +17,47 @@ function recovery_pass_help($route_name, RouteMatchInterface $route_match) {
      return $output;
  }
}

/**
 * Returns a mail string for a variable name.
 *
 * Used by recovery_pass_mail() and the settings forms to retrieve strings.
 */
function _recovery_pass_mail_text($key, $language = NULL, $replace = TRUE, $user = array()) {
  $langcode = isset($language) ? $language->language : NULL;

  if ($admin_setting = \Drupal::config('recovery_pass.settings')->get('recovery_pass_' . $key, FALSE)) {
    // An admin setting overrides the default string.
    $text = $admin_setting;
  }
  else {
    // No override, return default string.
    switch ($key) {
      case 'email_subject':
        $text = t('Replacement login information for [user:name] at [site:name]', array(), array('langcode' => $langcode));
        break;

      case 'email_text':
        $text = t("[user:name],

A request to reset the password for your account has been made at [site:name].

Your new password is [user_new_password].


--  [site:name] team", array(), array('langcode' => $langcode));
        break;

      case 'old_pass_warning':
        $text = t('You are using <strong>old password</strong>, your password was reset recently. New Password was sent to your registered email id.');
        break;
    }
  }

  if ($replace && \Drupal::moduleHandler()->moduleExists('token')) {
    // Token Replace the text.
    return Drupal::token()->replace($text, array('user' => $user));
  }

  return $text;
}
+120 −0
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains \Drupal\recovery_pass\RecoveryPassSettingsForm
 */
namespace Drupal\recovery_pass;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure recovery_pass settings for this site.
 */
class RecoveryPassSettingsForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'recovery_pass_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'recovery_pass.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    //$config = $this->config('recovery_pass.settings');

    // $form['recovery_pass_thing'] = array(
    //   '#type' => 'textfield',
    //   '#title' => $this->t('thing'),
    //   '#default_value' => $config->get('things'),
    // );
    $form['recovery_pass_help_text'] = array(
      '#type' => 'item',
      '#markup' => t('Edit the e-mail messages sent to users who request a new password. The list of available tokens that can be used in e-mails is provided below. For displaying new password please use <strong>[user_new_password]</strong> placeholder.'),
    );

    $form['recovery_pass_email_subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#required' => TRUE,
      '#default_value' => _recovery_pass_mail_text('email_subject'),
    );

    $form['recovery_pass_email_text'] = array(
      '#type' => 'textarea',
      '#title' => t('Email Body'),
      '#required' => TRUE,
      '#default_value' => _recovery_pass_mail_text('email_text'),
    );

    if (\Drupal::moduleHandler()->moduleExists("htmlmail")) {
      // Adding description incase HTMLMAIL module exists.
      $form['recovery_pass_email_text']['#description'] = t('Supports HTML Mail provided HTMLMAIL module is enabled.');
    }

    if (\Drupal::moduleHandler()->moduleExists("token")) {
      $form['token_help'] = array(
        '#type' => 'markup',
        '#token_types' => array('user'),
        '#theme' => 'token_tree_link',
      );
    }

    $form['recovery_pass_fpass_redirect'] = array(
      '#type' => 'textfield',
      '#title' => t('Redirect Path after Forgot Password Page'),
      '#maxlength' => 255,
      '#default_value' => \Drupal::config('recovery_pass.settings')->get('recovery_pass_fpass_redirect') ? \Drupal::config('recovery_pass.settings')->get('recovery_pass_fpass_redirect') : 'user',
      '#description' => t('The path to redirect user, after forgot password form. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.',
        array(
          '%front' => '<front>',
          '%add-node' => 'node/add',
          '%drupal' => 'http://drupal.org',
        )
      ),
      '#required' => TRUE,
      '#element_validate' => array(array($this, '_recovery_pass_validate_path')),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('recovery_pass.settings')
      ->set('recovery_pass_email_subject', $form_state->getValue('recovery_pass_email_subject'))
      ->save();
    $this->config('recovery_pass.settings')
      ->set('recovery_pass_email_text', $form_state->getValue('recovery_pass_email_text'))
      ->save();
    $this->config('recovery_pass.settings')
      ->set('recovery_pass_fpass_redirect', $form_state->getValue('recovery_pass_fpass_redirect'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}

/**
 * Validates path entered by user.
 */
function _recovery_pass_validate_path($element, &$form_state, $form) {
  if (!empty($element['#value'])) {
    $path = $element['#value'];
    if (!drupal_valid_path($path) && !drupal_lookup_path('source', $path) && !url_is_external($path)) {
      form_error($element, t('Please provide valid redirect path.'));
    }
  }
}