ump-8.x-1.0-alpha1/ump.module
ump.module
<?php
/**
* @file
* User Management Plus module file.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Entity\EntityInterface;
require_once dirname(__FILE__) . '/ump.helper.inc';
/**
* Implements hook_form_alter().
*/
function ump_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == "user_register_form" || $form_id == "user_form") {
$rules = ump_get_ump_crud_rules();
$allow_roles = [];
foreach ($rules as $rule) {
if ($rule["allow_to_create_enable"] == 1) {
$allow_roles[] = $rule["allow_to_create_role"];
if ($form_id == "user_register_form" && $rule["allow_to_create_overtake"] == 1) {
// Add submit callback that will overtake the field from creator.
$user_fields = ump_get_user_user_fields();
$form['actions']['submit']['#submit'][] = 'ump_user_register_form_submit';
\Drupal::messenger()->addWarning(t('The value of @field will be overtaken from your user, if @role will be selected.', ['@field' => $user_fields[$rule["allow_to_create_overtake_field"]], '@role' => $rule["allow_to_create_role"]]));
}
// Validate the roles selected.
$form['#validate'][] = 'ump_user_register_form_validate';
}
}
if (count($allow_roles) > 0) {
// Avoid loading cached data from browser local storage.
$form['#attributes']['data-user-info-from-browser'] = FALSE;
// Disaply only the roles that are allowed for current user.
$allow_roles[] = 'authenticated';
$form['account']['roles']["#access"] = TRUE;
foreach ($form['account']['roles']['#options'] as $option_key => $option_value) {
if (!in_array($option_key, $allow_roles)) {
unset($form['account']['roles']['#options'][$option_key]);
}
}
// Display the status field.
$form['account']['status']['#access'] = TRUE;
if ($form_id == "user_register_form") {
// Set default value for status.
$form['account']['status']['#default_value'] = 1;
// Display the notify field.
$form['account']['notify']['#access'] = TRUE;
// This setting allow proper notification and redirect after form
// submit.
$form['administer_users']['#value'] = TRUE;
}
// Make sure our custom handlers are being added only once.
$form['actions']['submit']['#submit'] = array_unique($form['actions']['submit']['#submit']);
$form['#validate'] = array_unique($form['#validate']);
}
}
if ($form_id == "user_cancel_form") {
$rules = ump_get_ump_crud_rules();
$allow_roles = [];
foreach ($rules as $rule) {
if ($rule["allow_to_delete_enable"] == 1) {
$allow_roles[] = $rule["allow_to_delete_role"];
}
}
if (count($allow_roles) > 0) {
// Display the hidden fields.
$form['user_cancel_method']['#access'] = TRUE;
$form['user_cancel_confirm']['#access'] = TRUE;
$form['user_cancel_notify']['#access'] = TRUE;
// Allow the immediate cancealtion of the account.
$form['access']['#value'] = TRUE;
}
}
}
/**
* UMP submit callback for user_register_form.
*/
function ump_user_register_form_submit($form, FormStateInterface $form_state) {
$parent_user = User::load(\Drupal::currentUser()->id());
$rules = ump_get_ump_crud_rules();
$new_user = User::load($form_state->getValue('uid'));
foreach ($rules as $rule) {
if ($rule["allow_to_create_overtake"] == 1) {
if (in_array($rule["allow_to_create_role"], $new_user->getRoles())) {
$field_to_overtake = $rule["allow_to_create_overtake_field"];
$field_value = $parent_user->$field_to_overtake->getValue();
$new_user->$field_to_overtake->setValue($field_value);
$new_user->save();
}
}
}
}
/**
* UMP validate callback for user_register_form.
*/
function ump_user_register_form_validate($form, FormStateInterface $form_state) {
$found_valid_role = FALSE;
$rules = ump_get_ump_crud_rules();
foreach ($rules as $rule) {
if ($rule["allow_to_create_enable"] == 1) {
if (is_array($form_state->getValue('roles'))) {
if (in_array($rule["allow_to_create_role"], $form_state->getValue('roles'))) {
$found_valid_role = TRUE;
}
}
}
}
if ($found_valid_role == FALSE) {
$form_state->setErrorByName('roles', t('Please select a valid role.'));
}
// We need to make sure username and email are unique.
if ($form_state->getValue('form_id') == 'user_register_form') {
if (user_load_by_name($form_state->getValue('name'))) {
$form_state->setErrorByName('name', t('The username is already taken.'));
}
if (user_load_by_mail($form_state->getValue('mail'))) {
$form_state->setErrorByName('name', t('The email address is already taken.'));
}
}
}
/**
* Implements hook_entity_field_access().
*/
function ump_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Hide the field field_ump_created_by for everybody, we user it only
// programatically.
if ($field_definition->getName() == 'field_ump_created_by') {
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function ump_user_insert(EntityInterface $entity) {
// Log the UID of the user who created the new user.
$entity->field_ump_created_by->setValue(\Drupal::currentUser()->id());
$entity->save();
}
