我有一个Drupal网站,以及“消息”和“令牌”模块。
我基于模块“消息示例”创建了一个自定义模块。
我为实体创建了一种名为message_activity_stream
的显示模式。
我创建了一个令牌来显示节点的渲染实体,并且它起作用。
现在,我想创建一个自定义标记,以显示消息作者的渲染实体。
如何在我的模块中创建自定义令牌,以显示消息作者的用户帐户的呈现实体message_activity_stream
?
我已经尝试过下面的代码,但是它不起作用:
case 'author-render':
$entity = $message->getOwner();
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity);
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
这是message_activity_stream.tokens.inc
文件:
<?php
/**
* @file
* Place holder for the Message Activity Stream module.
*/
/**
* Implements hook_token_info().
*
* We need this part until the token module will be exported to Drupal 8.
*/
function message_activity_stream_token_info() {
$type = [
'name' => t('Message Activity Stream'),
'description' => t('Tokens for the Message Activity Stream module.'),
'needs-data' => 'message',
];
// User relate tokens.
$message['author-render'] = [
'name' => t("Rendered author"),
'description' => t("The related author rendered to HTML."),
];
// Store related tokens.
$message['store-render'] = [
'name' => t("Rendered store"),
'description' => t("The related store rendered to HTML."),
];
// Product related tokens.
$message['product-render'] = [
'name' => t("Rendered product"),
'description' => t("The related product rendered to HTML."),
];
// Group related tokens.
$message['group-render'] = [
'name' => t("Rendered group"),
'description' => t("The related group rendered to HTML."),
];
// Node related tokens.
$message['node-render'] = [
'name' => t("Rendered node"),
'description' => t("The related node rendered to HTML."),
];
return [
'types' => ['message' => $type],
'tokens' => [
'message' => $message,
],
];
}
/**
* Implements hook_tokens().
*/
function message_activity_stream_tokens($type, $tokens, array $data = [], array $options = []) {
$token_service = \Drupal::token();
$url_options = ['absolute' => TRUE];
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = [];
if ($type == 'message' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the comment.
case 'author-render':
$entity = $message->getOwner();
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity);
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
case 'store-render':
if ($message->bundle() == 'mas_create_store') {
$entity = $message->field_store_reference->get(0)->entity;
}
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
case 'product-render':
if ($message->bundle() == 'mas_create_product') {
$entity = $message->field_product_reference->get(0)->entity;
}
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
case 'group-render':
if ($message->bundle() == 'mas_create_group') {
$entity = $message->field_group_reference->get(0)->entity;
}
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
case 'node-render':
if ($message->bundle() == 'mas_create_node') {
$entity = $message->field_node_reference->get(0)->entity;
}
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$message_view = $view_builder->view($entity, 'message_activity_stream');
$replacements[$original] = \Drupal::service('renderer')->renderRoot($message_view);
break;
}
}
}
return $replacements;
}