0

I have allowed a certain 'role' to change/edit fields in 'registered' users' fields. I want to receive an email in the admin's inbox detailing these changes using Rules.

I tried and failed!

Any help to achieve this?

i.e.

Subject: [user, who changed the field in a registered user's profile] changed [user, whose profile settings have been changed] profile
Body: [changed by]
[user whose profile was changed]
[value of changed filed in profile]
[value changed at]
  • 1
    Have you made a new account and received an e-mail? Just want to check that your e-mailing system works and that Rules is indeed the where the issue is at. – No Sssweat Dec 21 '18 at 3:06
0

I am giving you the alternate idea instead of using rules,you can use hook_form_alter.

use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
 function getCurrentUserRecord()
 {
  $myArray = []; 
  $x = db_select('users_field_data','cw')
    ->fields('cw',['name','pass','mail'])
    ->execute()->fetchAll();

  foreach($x as $v){
    $myArray = $v;  
  }
  return $myArray;
  }

function nestle_viewalter_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

      if($form_id == "user_form")
       {

            foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
  $form['actions'][$action]['#submit'][] = 'nestle_viewalter_form_submit';
   }
   }
   }

   }
function nestle_viewalter_form_submit(array $form, FormStateInterface $form_state)
{
$records = getCurrentUserRecord();

$field=$form_state->getValues();
$mail = $field[mail];
$name = $field[name];

if($records->name == $name || $records->mail == $mail)  {
    drupal_set_message(t("No changes so far"), 'error');
}
  else{

    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = 'nestle_viewalter';
    $key = 'update_profile';
    $to = 'vikram@sparxitsolutions.com';
    $send = true;
    $result = $mailManager->mail($module, $key, $to,NULL, $send);

    if ($result['result'] !== true) {
        drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
    }
    else {
        drupal_set_message(t('Your message has been sent.'));
    }

    drupal_set_message(t("Send mail"), 'status');
}
}

  function nestle_viewalter_mail($key, &$message, $params) {
 switch ($key) {
 case 'update_profile':
 $message['from'] = \Drupal::currentUser()->getEmail();
 $message['subject'] = t('Profile updated');
 $message['body'][] = 'this is a test mail from the current user as i have updated my profile.';
 break;
 }
 }
  • That was sweet. That makes me to ask you to send me a link which explains IN DETAIL the process of attaching this hook to drupal 8 through module. Thanks in advance. – Moonglade Dec 29 '18 at 8:11
  • Hi Vikram can you help me to make a module named inform_user_changes – Moonglade Jan 18 at 13:22

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.