6

I would like to force users to provide a reason for cancelling (a text area would be ideal) their accounts. I have had several users close their accounts on one of my sites recently and I would like to know why.

6
+50

Here you go, alter the user_cancel_confirm_form.

function MYMODULE_form_user_cancel_confirm_form_alter(&$form, &$form_state, $form_id) {
  $form['reason'] = array(
    '#title' => t('Reason'),
    '#type' => 'textarea',
    '#description' => t('We are sad to see you go, why do you want to leave?'),
    '#required' => TRUE, // force to input
  );

  $form['#submit'][] = 'user_cancel_log_reason'; // add custom submit handler
}

function user_cancel_log_reason($form, &$form_state) {
  $reason = $form_state['values']['reason'];
  // do whatever with the reason, e.g watchdog
  watchdog('user', 'User cancel reason:' . $reason);
}
0

I think you can just use this api function http://api.drupal.org/api/drupal/modules!user!user.api.php/function/hook_user_cancel/7

0

For Drupal 8:

function MYMODULE_form_user_cancel_form_alter(&$form, &$form_state, $form_id) {
  $form['reason'] = array(
    '#title' => t('Reason'),
    '#type' => 'textarea',
    '#description' => t('Please let us know why you are closing your account.'),
    '#required' => TRUE,
  );

  $form['#submit'][] = 'MYMODULE_logcancelreason'; // add custom submit handler
}

function MYMODULE_logcancelreason($form, &$form_state) {
  $reason = $form_state->getValue('reason');
  $uid = $form['uid']['#value'];
  $reason = 'UID ' . $uid . ' cancellation reason: ' . $form_state['values']['reason'];
  // Log the reason.
  \Drupal::logger('user')->info($reason);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.