Skip to content
Snippets Groups Projects
Commit 92b9e550 authored by Will Jackson's avatar Will Jackson
Browse files

#3422774: Add feature to track changes to specific fields.

parent 59dfc006
No related branches found
No related tags found
1 merge request!2#3422774: Add feature to track changes to specific fields.
......@@ -14,4 +14,6 @@ email_body: |
View Profile: [user:url]
Edit Profile: [user:edit-url]
targeting_type: target_specific
target_roles: { }
selected_fields: { }
......@@ -53,20 +53,19 @@ class UserUpdateNotifyFieldCompare {
public function compare(EntityInterface $original, EntityInterface $updated): array {
$changes = [];
// Check for tracked user fields and track changes.
foreach ($updated->getFields(FALSE) as $fieldName => $fieldItemList) {
if ($fieldName === 'pass' || !$original->hasField($fieldName)) {
// Exclude password and non-existent fields in original entity.
continue;
}
$originalValues = $original->get($fieldName)->getValue();
$updatedValues = $updated->get($fieldName)->getValue();
if ($this->normalizeValues($originalValues) !== $this->normalizeValues($updatedValues)) {
$changes[$fieldName] = [
'original' => $originalValues,
'updated' => $updatedValues,
];
$tracked_fields = $this->configFactory->get('user_update_notify.settings')->get('user_fields');
if (in_array($fieldName, $tracked_fields)) {
$originalValues = $original->get($fieldName)->getValue();
$updatedValues = $updated->get($fieldName)->getValue();
if ($this->normalizeValues($originalValues) !== $this->normalizeValues($updatedValues)) {
$changes[$fieldName] = [
'original' => $originalValues,
'updated' => $updatedValues,
];
}
}
}
......
......@@ -4,6 +4,7 @@ namespace Drupal\user_update_notify\Form;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -35,6 +36,13 @@ class SettingsForm extends FormBase {
*/
protected EmailValidatorInterface $emailValidator;
/**
* EntityFieldManagerInterface instance.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected EntityFieldManagerInterface $entityFieldManager;
/**
* Constructs a SettingsForm object.
*
......@@ -44,13 +52,17 @@ class SettingsForm extends FormBase {
* Entity type manager service.
* @param \Drupal\Component\Utility\EmailValidatorInterface $email_validator
* The email validator service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* EntityFieldManagerInterface service.
*/
public function __construct(ConfigFactoryInterface $config_factory,
EntityTypeManagerInterface $entity_type_manager,
EmailValidatorInterface $email_validator) {
EmailValidatorInterface $email_validator,
EntityFieldManagerInterface $entityFieldManager) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->emailValidator = $email_validator;
$this->entityFieldManager = $entityFieldManager;
}
/**
......@@ -60,7 +72,8 @@ class SettingsForm extends FormBase {
return new static(
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('email.validator')
$container->get('email.validator'),
$container->get('entity_field.manager')
);
}
......@@ -86,6 +99,28 @@ class SettingsForm extends FormBase {
$role_options[$role_id] = $role->label();
}
// Define a list of field machine names to exclude.
$exclude_fields = [
'uid', 'uuid', 'status', 'created', 'changed', 'access', 'login',
'path', 'metatag', 'preferred_admin_langcode', 'preferred_langcode',
'langcode', 'init', 'roles', 'default_langcode',
];
// Retrieve field definitions and populate options for a form element.
$user_fields = $this->entityFieldManager->getFieldDefinitions('user', 'user');
$field_options = [];
foreach ($user_fields as $field_name => $field_definition) {
// Skip fields in the exclude list.
if (in_array($field_name, $exclude_fields)) {
continue;
}
// Ensure the field has a label before adding it.
if (!empty($field_definition->getLabel())) {
$field_options[$field_name] = $field_definition->getLabel();
}
}
$form['notification_settings'] = [
'#type' => 'details',
'#title' => $this->t('Notification Settings'),
......@@ -155,6 +190,31 @@ class SettingsForm extends FormBase {
'#dialog' => TRUE,
];
$form['targeted_fields'] = [
'#type' => 'details',
'#title' => $this->t('Targeted Fields'),
'#open' => TRUE,
];
$form['targeted_fields']['targeting_type'] = [
'#type' => 'radios',
'#title' => $this->t('Field Targeting Choice'),
'#options' => [
'target_specific' => $this->t('Target only the following fields'),
'exclude_specific' => $this->t('Target all fields except for the following'),
],
'#default_value' => $config->get('targeting_type'),
'#description' => $this->t('Choose how to specify which fields should trigger notifications.'),
];
$form['targeted_fields']['selected_fields'] = [
'#type' => 'checkboxes',
'#title' => $this->t('User Fields'),
'#options' => $field_options,
'#default_value' => $config->get('selected_fields'),
'#description' => $this->t('Select user fields to include.'),
];
$form['targeted_roles'] = [
'#type' => 'details',
'#title' => $this->t('Targeted Roles'),
......@@ -207,12 +267,15 @@ class SettingsForm extends FormBase {
->set('admin_email_recipient', $form_state->getValue('admin_email_recipient'))
->set('recipient_email', $form_state->getValue('recipient_email'))
->set('recipient_role', $form_state->getValue('recipient_role'))
->set('targeting_type', $form_state->getValue('targeting_type'))
->set('email_subject', $form_state->getValue('email_subject'))
->set('email_body', $form_state->getValue('email_body'));
// Handle the target_roles since its checkboxes.
$target_roles = array_filter($form_state->getValue('target_roles'));
$target_fields = array_filter($form_state->getValue('selected_fields'));
$config->set('target_roles', $target_roles);
$config->set('user_fields', $target_fields);
// Save the configuration.
$config->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment