我有一个网站,上面有Drupal 8以及消息和令牌模块。
我基于消息模块为我的自定义模块创建了自定义标记。这是我添加到模块中的文件:
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;
}
效果很好,即使显示替换图像。但是我令牌author-render
创建了一个错误:
Type php
Date Jeudi, 18 juillet, 2019 - 02:20
User administrateur
Emplacement https://www.example.com/fr
Référent
Message Recoverable fatal error: Object of class Drupal\user\Entity\User could not be converted to string in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 150 of /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php) #0 /var/www/www-example-com/web/core/includes/bootstrap.inc(587): _drupal_error_handler_real(4096, 'Object of class...', '/var/www/www-s1...', 150, Array) #1 [internal function]: _drupal_error_handler(4096, 'Object of class...', '/var/www/www-s1...', 150, Array) #2 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php(150): sprintf('The "%s" entity...', Object(Drupal\user\Entity\User)) #3 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php(272): Drupal\Core\Entity\EntityTypeManager->getDefinition(Object(Drupal\user\Entity\User)) #4 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php(257): Drupal\Core\Entity\EntityTypeManager->getHandler(Object(Drupal\user\Entity\User), 'view_builder') #5 /var/www/www-example-com/web/modules/custom/message_activity_stream/message_activity_stream.tokens.inc(85): Drupal\Core\Entity\EntityTypeManager->getViewBuilder(Object(Drupal\user\Entity\User)) #6 [internal function]: message_activity_stream_tokens('message', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata)) #7 /var/www/www-example-com/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('message_activit...', Array) #8 /var/www/www-example-com/web/core/lib/Drupal/Core/Utility/Token.php(304): Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array) #9 /var/www/www-example-com/web/core/lib/Drupal/Core/Utility/Token.php(196): Drupal\Core\Utility\Token->generate('message', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata)) #10 /var/www/www-example-com/web/modules/contrib/twig_tweak/src/TwigExtension.php(552): Drupal\Core\Utility\Token->replace('[message:author...', Array, Array) #11 /var/www/www-example-com/web/sites/default/files/php/twig/5d2fb8b27bc0e_message--mas-create-node-_ABvoPPSRsU3QG_Fy0iX4RU5YF/RktsZQSpJ7OAQyA75bBOog_ktp-qYvaRDNtomwz9r9Y.php(62): Drupal\twig_tweak\TwigExtension->drupalToken('message:author-...', Array) #12 /var/www/www-example-com/vendor/twig/twig/src/Template.php(455): __TwigTemplate_d33192b3dbb8dddc6e03e302a586f5d15a132dc819ff276fd420c87132608ac6->doDisplay(Array, Array) #13 /var/www/www-example-com/vendor/twig/twig/src/Template.php(422): Twig\Template-................................................................................................................................................................................................................................................................................................
Severity Erreur