2

I have a content type "Room" with this config:

  • Title
  • Body
  • field_comments (Comments)
  • field_members (Users Entity reference)

When I create a room I send with hook_entity_insert() an email to users add to the room

But my problem is when I edit this room and I add New members, I want to send an email only for new.

I try it with this code in custom module with hook_entity_update:

notify.module
/**
* Implements hook_entity_update().
*/
function notify_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
 if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'room')) {
   return;
 }
 $mailManager = \Drupal::service('plugin.manager.mail');
 $module = ‘notify’;
 $key = 'new_notif';


 //send to all members in room 
 $members = $entity->get('field_members')->getValue();
    $tos = [];
    foreach ( $members as $member ) {
        if ($member['target_id'] !== '1') {
            $tid = intval($member['target_id']);
            $user = \Drupal\user\Entity\User::load( $tid );
            $tos[] = $user->getEmail();
        }
    }

 $membersTo = implode (', ', $tos);

 $to = $membersTo;

 $params['message'] = 'You access to the room';

 $params['node_title'] = $entity->label();
 $langcode = \Drupal::currentUser()->getPreferredLangcode();
 $send = true;
 $result = $mailManager->mail($module, $key, $to, $langcode, $params, 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.'));
 }
}

I Don't know how to exclude old members after edit, add new members and save a room.

2

But my problem is when I edit this room and I add New members, I want to send an email only for new.

As hook_entity_update mentions

Get the original entity object from $entity->original.

$entity->original gives you access to the values before the update.

So all you gotta do is compare the original members to the updated members to find out who are the new members.

$og_members = array();
$og_members = $entity->original->get('field_members')->getValue();

$up_members = array();
$up_members = $entity->get('field_members')->getValue();

$new_members = array();
// use array_diff() to find the difference.
$new_members = array_diff($up_members, $og_members);
| improve this answer | |
0

Ok you have options here...

Option1, (Harder) If the node revisionable ... you can do a difference check between the last revision and the newly added values ...

Option 2 (easy)

make a new field called field_members_to_add (Users Entity reference) when you edit the node you would put the new people on the new field ... then in your "hook_entity_update" you can get the values of this field.. send the emails, update the normal "field_members" with the new values .. then set "field_members_to_add" it to null so it will be empty next time you add new people.

so then it would be...

  $new_members = $entity->get('field_members_to_add')->getValue();
  // send email ...then later... 
  $entity->set('field_members_to_add', NULL);
  $old_members = $entity->get('field_members')->getValue();
  // merge values. 
  $all_members = array_merge($old_members,$new_members);
  $entity->set('field_members', $all_members);
  // Party.
| improve this answer | |
  • Thx Taggart for your answer, I try the option 2 but when I save my node after edit, old and new members don't merge. – Kipmag Jun 14 '18 at 9:45
  • It's work with this methode but not for entity_presave, Thx Taggart – Kipmag Jun 14 '18 at 12:12

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.