function private_message_node_view in Private Message 8
Same name and namespace in other branches
- 8.2 private_message.module \private_message_node_view()
Implements hook_ENTITY_TYPYE_view().
Adds new elements to the Node entity.
See also
File
- ./
private_message.module, line 219 - Contains hooks for the private message module.
Code
function private_message_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($display
->getComponent('private_message_link')) {
$author = $entity
->getOwner();
$current_user = \Drupal::currentUser();
if ($current_user
->isAuthenticated()) {
if ($current_user
->hasPermission('use private messaging system') && $current_user
->id() != $author
->id()) {
$members = [
$current_user,
$author,
];
$thread_id = \Drupal::service('private_message.mapper')
->getThreadIdForMembers($members);
if ($thread_id) {
$url = Url::fromRoute('entity.private_message_thread.canonical', [
'private_message_thread' => $thread_id,
], [
'attributes' => [
'class' => [
'private_message_link',
],
],
]);
$build['private_message_link'] = [
'#type' => 'link',
'#url' => $url,
'#title' => t('Send private message to author'),
'#prefix' => '<div class="private_message_link_wrapper">',
'#suffix' => '</div>',
];
}
else {
$url = Url::fromRoute('private_message.private_message_create', [], [
'query' => [
'recipient' => $author
->id(),
],
]);
$build['private_message_link'] = [
'#type' => 'link',
'#url' => $url,
'#title' => t('Send private message to author'),
'#prefix' => '<div class="private_message_link_wrapper">',
'#suffix' => '</div>',
];
}
}
}
else {
$url = Url::fromRoute('user.login');
$build['private_message_link'] = [
'#type' => 'link',
'#url' => $url,
'#title' => t('Send private message'),
'#prefix' => '<div class="private_message_link_wrapper">',
'#suffix' => '</div>',
];
}
}
}