cancel_account-8.x-1.1/cancel_account.module
cancel_account.module
<?php
/**
* @file
* This is the module for cancel user account separate form.
*/
use Drupal\cancel_account\Form\CancelAccountForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function cancel_account_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.cancel_account':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module provides cancel user account separate form and adds it to user edit form by default if user has cancel account permissions edits his own account.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<p>' . t('You can customize this form using <em>hook_form_alter()</em> or <em>hook_form_cancel_account_form_alter()</em>.') . '</p>';
return $output;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function cancel_account_form_user_form_alter(&$form, FormStateInterface &$form_state) {
// Check if user edits his own account.
$own = $form_state->get('user');
if ($own) {
// Hide Cancel account button.
if (isset($form['actions']['delete'])) {
$form['actions']['delete']['#access'] = FALSE;
}
// Check cancel account permissions and prevent adding form for superadmin.
if ($own->hasPermission('cancel account') && $own->id() != 1) {
// Get cancel user account separate form.
$cancel_form = \Drupal::formBuilder()->getForm(CancelAccountForm::class, $own->id());
// Add it to the end of user edit form.
$form['#suffix'] = \Drupal::service('renderer')->render($cancel_form);
}
}
}
