Pages

Wednesday 9 March 2016

Create custom Token for Password

/**
 * Implements hook_token_info().
 */
function module_name_token_info() {
  $info['tokens']['user']['password'] = array(
    'name' => t('Password'),
    'description' => t('The password by the user'),
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  $url_options = array('absolute' => TRUE);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }

  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'password':
          $replacements[$original] = $account->password;
          break;
      }
    }
  }

  return $replacements;
}