Skip to content
Snippets Groups Projects
Commit 4309231b authored by Jay Friendly's avatar Jay Friendly Committed by Jay Friendly
Browse files

Issue #2976490 by zenimagine, Jaypan: Integrate with the "Profile" module

parent e5a35cfd
No related branches found
No related tags found
No related merge requests found
......@@ -97,6 +97,16 @@ function private_message_entity_extra_field_info() {
];
}
$profile_bundles = \Drupal::entityManager()->getBundleInfo('profile');
foreach (array_keys($profile_bundles) as $bundle) {
$fields['profile'][$bundle]['display']['private_message_link'] = [
'label' => t('Private message thread link'),
'description' => t('Displays a link to send a private message to the profile owner'),
'weight' => 0,
'visible' => FALSE,
];
}
return $fields;
}
......@@ -165,46 +175,7 @@ function private_message_user_view(array &$build, EntityInterface $entity, Entit
}
}
if ($display->getComponent('private_message_link')) {
$current_user = \Drupal::currentUser();
if ($current_user->isAuthenticated()) {
if ($current_user->hasPermission('use private messaging system') && $current_user->id() != $entity->id()) {
$members = [$current_user, $entity];
$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'),
'#prefix' => '<div class="private_message_link_wrapper">',
'#suffix' => '</div>',
];
}
else {
$url = Url::fromRoute('private_message.private_message_create', [], ['query' => ['recipient' => $entity->id()]]);
$build['private_message_link'] = [
'#type' => 'link',
'#url' => $url,
'#title' => t('Send private message'),
'#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>',
];
}
}
\Drupal::service('private_message.service')->createRenderablePrivateMessageThreadLink($build, $entity, $display, $view_mode);
}
/**
......@@ -215,46 +186,7 @@ function private_message_user_view(array &$build, EntityInterface $entity, Entit
* @see hook_entity_view()
*/
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>',
];
}
}
\Drupal::service('private_message.service')->createRenderablePrivateMessageThreadLink($build, $entity, $display, $view_mode);
}
/**
......@@ -265,46 +197,18 @@ function private_message_node_view(array &$build, EntityInterface $entity, Entit
* @see hook_entity_view()
*/
function private_message_comment_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'),
'#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>',
];
}
}
\Drupal::service('private_message.service')->createRenderablePrivateMessageThreadLink($build, $entity, $display, $view_mode);
}
/**
* Implements hook_ENTITY_TYPYE_view().
*
* Adds new elements to the Profile entity.
*
* @see hook_entity_view()
*/
function private_message_profile_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
\Drupal::service('private_message.service')->createRenderablePrivateMessageThreadLink($build, $entity, $display, $view_mode);
}
/**
......
......@@ -5,8 +5,11 @@ namespace Drupal\private_message\Service;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\private_message\Entity\PrivateMessageInterface;
use Drupal\private_message\Entity\PrivateMessageThreadInterface;
use Drupal\private_message\Mapper\PrivateMessageMapperInterface;
......@@ -312,6 +315,52 @@ class PrivateMessageService implements PrivateMessageServiceInterface {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function createRenderablePrivateMessageThreadLink(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 = $this->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'),
'#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'),
'#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>',
];
}
}
}
/**
* Create a new private message thread for the given users.
*
......
......@@ -2,6 +2,8 @@
namespace Drupal\private_message\Service;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\private_message\Entity\PrivateMessageInterface;
use Drupal\private_message\Entity\PrivateMessageThreadInterface;
use Drupal\user\UserInterface;
......@@ -185,4 +187,19 @@ interface PrivateMessageServiceInterface {
*/
public function getThreadFromMessage(PrivateMessageInterface $privateMessage);
/**
* Add a link to send a private message to the owner of the given entity.
*
* @param array $build
* A render array representing the given entity. Chances should be made to
* this render array.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being rendered.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display being used to render the entity.
* @param string $view_mode
* The view mode being used to render the entity.
*/
public function createRenderablePrivateMessageThreadLink(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment