Problem/Motivation

I'm working on a site where a specific node type can be posted to groups. These nodes start life unpublished, with a date range field defining the window of time they should be published. I need to automatically notify all group members when one of these nodes is published.

#3076248: Notification toggle on save and edit gives us the possibility to conditionally send group notifications when editing a node.

However, because it all works at the Form API level, if you have something programmatically updating (and in this case, publishing) nodes, there's no way to twiddle those checkboxes and generate the emails.

Proposed resolution

Not entirely sure what's best for this. If there was some code at the hook_ENTITY_TYPE_update() level that looked for the presence of a magic property, I could force this to work with something like:

        // Array of Group entity IDs to notify with this node update:
        $node->group_notify[] = $group->id();
        $node->setNewRevision(TRUE);
        $node->setPublished();
        $node->save(); 

Or maybe there should be both an array if you want specific Group IDs (above) or a bool if you want everything:

        $node->group_notify_all = TRUE;

Remaining tasks

  1. Decide on how this should work.
  2. Implement.
  3. Testing.
  4. Automated tests?
  5. Reviews / refinements.
  6. Commit.

User interface changes

None. This would be entirely at the Entity API level.

API changes

TBD.

Data model changes

None.

Comments

dww created an issue. See original summary.

dww’s picture

Issue summary: View changes

Looking more closely at the group_notify code, I guess if a node is related to multiple groups, someone might only want to force notifications to certain groups. So maybe $node->group_notify should be an array of group IDs to notify, not a bool. Something like this:

    $node->group_notify[] = $group_id;

or:

    $node->group_notify = [
      $group_id_1,
      $group_id_2,
      ...
    ];
dww’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new2.81 KB

After quite a bit of manual local testing, I think this is ready.

Not sure where to document this. The README.txt seems both targeted to a less technical audience, and also somewhat incomplete.

Feedback welcome.

Thanks!
-Derek

dww’s picture

Title: Forced notification when a node is published » Forced notification when a node is created or updated via API (not via UI forms)
Issue tags: +API-First Initiative

More accurate title.

dww’s picture

StatusFileSize
new2.76 KB
new525 bytes

Upon further testing, these particular code paths shouldn't care about the notify mode -- if you programmatically set these properties, we should generate the emails, regardless of 'enforce' vs. 'toggle' mode.

dww’s picture

StatusFileSize
new2.81 KB
new1.67 KB

Upon further reflection, 'group_notify_all' vs 'group_notify' seemed a little vague. I think 'group_notify_group_ids' is a better name for the property where you put specific group IDs. I'm thinking someday someone will need a 'group_notify_group_types' to generate notifications to all groups of the given type(s), etc. More self-documenting seems better.

gregcube’s picture

This all checks out -- looks great to me. Tested locally, works as expected.

Not sure where to document this. The README.txt seems both targeted to a less technical audience, and also somewhat incomplete.

I can affirm to both. README.txt needs to be updated.

  • gregcube committed 37842e0 on 8.x-1.x authored by dww
    Issue #3164688 by dww: Forced notification when a node is created or...
dww’s picture

Status: Needs review » Fixed

Lovely, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.