Advertising sustains the DA. Ads are hidden for members. Join today

On this page

Subscribe Example

Last updated on
29 January 2024

This documentation needs work. See "Help improve this page" in the sidebar.

Simple

An example of sending messages to subscribers.

/**
 * Implements hook_node_update().
 */
function message_subscribe_example_node_update(NodeInterface $node) {
  $message = Message::create([
    'template' => 'node_update',
    'uid' => $node->getOwnerId(),
  ]);
  $message->set('field_node_reference', $node);
  $message->set('field_published', $node->isPublished());
  $message->save();

  // Send message to subscribers about changes to the node.
  $subscribers = \Drupal::service('message_subscribe.subscribers');
  $subscribers->sendMessage($node, $message);
}

Help improve this page

Page status: Needs work

You can: