You are here

function message_example_update_message_status in Message 8

Same name and namespace in other branches
  1. 7 message_example/message_example.module \message_example_update_message_status()

Set message entity published field when it changes in the related entity.

Parameters

\Drupal\Core\Entity\ContentEntityBase $entity: The entity object.

2 calls to message_example_update_message_status()
message_example_comment_update in modules/message_example/message_example.module
Implements hook_comment_update().
message_example_node_update in modules/message_example/message_example.module
Implements hook_node_update().

File

modules/message_example/message_example.module, line 69
Holds hook implementation for the Message example module.

Code

function message_example_update_message_status(ContentEntityBase $entity) {
  if ($entity
    ->isPublished() == $entity->original
    ->isPublished()) {
    return;
  }
  $query = \Drupal::entityQuery('message');
  $field = 'field_' . $entity
    ->getEntityType()
    ->id() . '_reference';
  $query
    ->condition($field . '.target_id', $entity
    ->id());
  $results = $query
    ->execute();
  if (empty($results)) {
    return;
  }
  $messages = Message::loadMultiple($results);
  foreach ($messages as $message) {
    $message
      ->set('field_published', $entity
      ->isPublished());
    $message
      ->save();
  }
}