如何在消息中显示注释的父节点的标题?

时间:2018-09-17 16:34:49

标签: php themes 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) {
  $message = Message::create(['template' => 'mas_create_group', 'uid' => $group->get('uid')]);
  $message->set('field_group_reference', $group);
  $message->save();
}

/**
 * Implements hook_node_insert().
 */
function message_activity_stream_node_insert(Node $node) {
  $message = Message::create(['template' => 'mas_create_node', 'uid' => $node->get('uid')]);
  $message->set('field_node_reference', $node);
  $message->set('field_published', $node->isPublished());
  $message->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

enter image description here

此模板未填写field_node_reference参考字段,因为它不在模块代码中。

消息活动流-创建评论 mas_create_comment

enter image description here

field_node_reference参考字段已很好地填充了此模板,因为它在模块的代码中。

消息活动流-创建节点 mas_create_node

enter image description here

我的问题

我应该在我的模块中进行哪些修改,以便2个模板填充有参考字段field_node_reference

我试图替换此代码:

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

作者:

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

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

但这不起作用。

0 个答案:

没有答案