flashpoint_message-8.x-1.x-dev/flashpoint_message.module

flashpoint_message.module
<?php

/**
 * @file
 * Holds hook implementation for the Flashpoint Message module.
 */

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\node\Entity\Node;
use Drupal\group\Entity\GroupContent;
use Drupal\user\Entity\User;
use Drupal\message\Entity\Message;

/**
 * Implements hook_node_insert().
 */
function flashpoint_message_node_insert(Node $node) {
  if ($node->bundle() === 'flashpoint_announcement') {
    $message = Message::create(['template' => 'flashpoint_announcement', 'field_flashpoint_announcement' => $node]);
    $message->save();
  }
}


/**
 * Implements hook_node_update().
 */
function flashpoint_message_node_update(Node $node) {
  if ($node->bundle() === 'flashpoint_announcement') {
    if (empty($node->original)) {
      return;
    }

    flashpoint_message_update_message_status($node);
  }
}


/**
 * Set message entity published field when it changes in the related entity.
 *
 * @param \Drupal\Core\Entity\ContentEntityBase $entity
 *   The entity object.
 */
function flashpoint_message_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();
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc