On this page
Drupal 8 Message Stack documentation
Introduction
Information on the implementation of Message Stack in Drupal 8. Message Stack is comprised of Message, Message Notify and Message Subscribe. Development is performed on GitHub for those modules.
Message
The message table itself does not contain many columns and only requires the template bundle id to be created as you will see below.
Message Template
A Message entity is always linked to Message template. You can create this message template at admin/structure/message.
A message is a fieldable entity, meaning that you can create fields for your message template which will be linked to message entity in the database.
Create a message
User Interface
If you want to make a message through the interface you must use the message ui module.
Custom
The code below is an quick example of how the writer saved a message entity for their message template. They filled the two fields that they specified on the message_template via the message service.
/**
* Set message.
*
* @param $subject
* @param $entity
*/
public function setMessage($subject, $entity) {
$message = Message::create(['template' => 'short_notification']);
if ($entity instanceof NodeInterface) {
$message->set('field_node', $entity);
}
if (!empty($subject)) {
$message->set('field_subject', $subject);
}
$message->save();
}
You can also fill the message through argument which can help avoid using fields, as in the example below:
/**
* @inheritdoc
*/
public function createMessage($entity, $user, $template, $overview_link = '') {
// Get the entity and data where the comment is belongs to.
$entityType = $entity->getEntityType();
// Build the token identifiers and the link to the entity where the comment
// belongs to.
$bundle_label_token = '@' . $entityType->getBundleEntityType();
$link_token = '@' . $entityType->id() . '_link';
$overview_link_token = '@' . $entityType->id() . '_overview_link';
$link = $entity->toLink()->toString();
/** @var \Drupal\Core\Entity\EntityStorageInterface $message_template_storage */
$messageTemplateStorage = $this->entityTypeManager->getStorage('message_template');
$messageTemplate = $messageTemplateStorage->load($template);
if (!is_object($messageTemplate)) {
return NULL;
}
/** @var \Drupal\Core\Entity\EntityStorageInterface $message_storage */
$messageStorage = $this->entityTypeManager->getStorage('message');
// Create the message entity as anonymous to make sure it does not appear in
// views of notifications from the author perspective.
/** @var \Drupal\message\MessageInterface $message */
$message = $messageStorage->create(['template' => $messageTemplate->id()]);
$message->setArguments([
$bundle_label_token => $entity->label(),
$link_token => $link,
$overview_link_token => $overview_link,
]);
$message->setOwnerId($user->id());
$message->save();
return $message;
}
See also
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion