我的自定义模块不适用于Drupal 8和消息堆栈

时间:2018-09-30 02:37:53

标签: php drupal drupal-modules drupal-8

我安装了“消息”模块。我想用节点的参考字段显示令牌。当前:

从父节点的标题中替换

[message:node-title],但不替换[message:field_node_reference:entity:title]

这是我的自定义模块的完整代码。它基于子模块Message Example:

<?php

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

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\group\Entity\Group;
use Drupal\node\Entity\Node;
use Drupal\comment\Entity\Comment;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_product\Entity\Product;
use Drupal\user\Entity\User;
use Drupal\profile\Entity\Profile;
use Drupal\message\Entity\Message;

/**
 * Implements hook_profile_insert().
 */
function message_activity_stream_profile_insert(Profile $profile) {
  $message = Message::create(['template' => 'mas_create_node', 'uid' => $profile->get('uid')]);
  $message->set('field_profile_reference', $profile);
  $message->set('field_published', $profile->isPublished());
  $message->save();
}

/**
 * Implements hook_commerce_store_insert().
 */
function message_activity_stream_commerce_store_insert(Store $commerce_store) {
  $message = Message::create(['template' => 'mas_create_store', 'uid' => $commerce_store->get('uid')]);
  $message->set('field_store_reference', $commerce_store);
  $message->save();
}

/**
 * Implements hook_commerce_product_insert().
 */
function message_activity_stream_commerce_product_insert(Product $commerce_product) {
  $message = Message::create(['template' => 'mas_create_product', 'uid' => $commerce_product->get('uid')]);
  $message->set('field_product_reference', $commerce_product);
  $message->set('field_published', $commerce_product->isPublished());
  $message->save();
}

/**
 * Implements hook_group_insert().
 */
function message_activity_stream_group_insert(Group $group) {
  $message1 = Message::create(['template' => 'mas_create_group', 'uid' => $group->get('uid')]);
  $message1->set('field_group_reference', $group);
  $message1->save();

  $message2 = Message::create(['template' => 'mas_create_node', 'uid' => $group->get('uid')]);
  $message2->set('field_group_reference', $group);
  $message2->save();
}

/**
 * Implements hook_node_insert().
 */
function message_activity_stream_node_insert(Node $node) {
  $message1 = Message::create(['template' => 'mas_create_node', 'uid' => $node->get('uid')]);
  $message1->set('field_node_reference', $node);
  $message1->set('field_published', $node->isPublished());
  $message1->save();

  $message2 = Message::create(['template' => 'mas_create_comment', 'uid' => $node->get('uid')]);
  $message2->set('field_node_reference', $node);
  $message2->set('field_published', $node->isPublished());
  $message2->save();
}

/**
 * Implements hook_comment_insert().
 */
function message_activity_stream_comment_insert(Comment $comment) {
  $message = Message::create(['template' => 'mas_create_comment', 'uid' => $comment->getOwnerId()]);
  $message->set('field_comment_reference', $comment);
  $message->set('field_published', $comment->isPublished());
  $message->save();
}

/**
 * Implements hook_user_insert().
 */
function message_activity_stream_user_insert(User $account) {
  Message::create(['template' => 'mas_user_register', 'uid' => $account->id()])->save();
}

/**
 * Implements hook_commerce_store_update().
 */
function message_activity_stream_commerce_store_update(Store $commerce_store) {
  if (empty($commerce_store->original)) {
    return;
  }

  message_activity_stream_update_message_status($commerce_store);
}

/**
 * Implements hook_commerce_product_update().
 */
function message_activity_stream_commerce_product_update(Product $commerce_product) {
  if (empty($commerce_product->original)) {
    return;
  }

  message_activity_stream_update_message_status($commerce_product);
}

/**
 * Implements hook_group_update().
 */
function message_activity_stream_group_update(Group $group) {
  if (empty($group->original)) {
    return;
  }

  message_activity_stream_update_message_status($group);
}

/**
 * Implements hook_node_update().
 */
function message_activity_stream_node_update(Node $node) {
  if (empty($node->original)) {
    return;
  }

  message_activity_stream_update_message_status($node);
}

/**
 * Implements hook_comment_update().
 */
function message_activity_stream_comment_update(Comment $comment) {
  if (empty($comment->original)) {
    return;
  }

  message_activity_stream_update_message_status($comment);
}

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

我创建了2个消息模板:

消息活动流-创建评论 mas_create_comment

消息活动流-创建节点 mas_create_node

全部都有参考字段field_node_reference

消息活动流-创建评论 mas_create_comment

这是我的消息模板。它不起作用,令牌也不能替换为url和标题:

<a href='[message:field_node_reference:entity:url]'>[message:field_node_reference:entity:title]</a>

消息活动流-创建节点 mas_create_node

这是我的消息模板。它可以正常工作,并且令牌被替换为url和标题:

<h3><a href='[message:field_node_reference:entity:url]'>[message:field_node_reference:entity:title]</a></h3>

我的问题

我的模块看起来不错,为什么不起作用。

现在,当我创建一个节点时,它会创建2条消息:

  • 1及其模型消息活动流-创建节点 mas_create_node

  • 1与模型消息活动流-创建评论 mas_create_comment

为什么?

0 个答案:

没有答案