0

I have a content type of event. I need to send all users an email after each 5th events saved, listing this 5 recently created events. What would be the most proper way to do that? I am tending to use hook_node_insert but I'm not sure how to check if it is the 5th node after the last email sent.

7

You could run an EntityFieldQuery on hook_node_insert to count all of the nodes of type event and then see if that count plus one (because the new node isn't saved to the DB yet) is divisible by five. Something like this:

function mymodule_node_insert() {
  $query = new EntityFieldQuery();
  $result = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'event')
    ->execute();
  $count = (count($result['node']) + 1);
  if (!($count % 5)) {
    // Generate/Send your email
  }
}

Note that the code above is totally untested.

  • Wow! Makes sense. I'll try that tomorrow, many thanks! – Елин Й. Nov 26 '13 at 16:18
  • Thanks, the the idea worked great! I didn't use EntityFieldQuery, but the idea counts ;) – Елин Й. Nov 27 '13 at 11:42
  • I've used db_select instead. $query = db_select('node', 'n')->condition('type', 'event'); $query->addExpression('COUNT(nid)', 'node_count'); $result = $query->execute()->fetchAssoc(); $count = $result['node_count'] + 1; – Елин Й. Nov 27 '13 at 12:07
  • Glad it worked. I can't think of any reason EntityFieldQuery is any better than db_select - maybe better or at least "more correct". – Adam Balsam Nov 27 '13 at 15:12
  • They both have pros and cons regarding the context. In my case all I needed was the amount of the nodes of a type, so I used db_select with a query that returns only what I need :) – Елин Й. Nov 28 '13 at 8:54
0

You can use a combination of the Rules module and the Goals and Tasks module.

  • Thanks, but I need to do that programmatically. Adam's answer is what was I need. – Елин Й. Nov 26 '13 at 17:47

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.