Skip to content
Snippets Groups Projects
Commit 60c1994a authored by Daniil Boiko's avatar Daniil Boiko Committed by Jay Friendly
Browse files

Issue #2976000 by 5n00py, mike82, Jaypan: Add class to the private message to...

Issue #2976000 by 5n00py, mike82, Jaypan: Add class to the private message to allow easier chat like styling
parent e61cb3f8
No related branches found
Tags 1.0.2
No related merge requests found
<?php
/**
* Alter the private message render array before it is rendered.
*
* @param array $build
* The render array representing the private message.
* @param \Drupal\Core\Entity\EntityInterface $privateMessage
* The private message entity being rendered.
* @param string $viewMode
* The view mode being rendered on the private message.
*/
function hook_private_message_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $privateMessage, $viewMode) {
// Create a new class specific to the author of the message.
$class = 'private-message-author-' . $privateMessage->getOwnerId();
// Add the class to the wrapper.
$build['wrapper']['#attributes']['class'][] = $class;
}
......@@ -3,24 +3,87 @@
namespace Drupal\private_message\Entity\Builder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Build handler for rpivate messages.
*/
class PrivateMessageViewBuilder extends EntityViewBuilder {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs a PrivateMessageViewBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* The entity type definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Theme\Registry $themeRegistry
* The theme registry.
*/
public function __construct(
EntityTypeInterface $entityType,
EntityManagerInterface $entityManager,
LanguageManagerInterface $languageManager,
AccountProxyInterface $currentUser,
Registry $themeRegistry = NULL
) {
parent::__construct($entityType, $entityManager, $languageManager, $themeRegistry);
$this->currentUser = $currentUser;
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
$build = parent::view($entity, $view_mode, $langcode);
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity.manager'),
$container->get('language_manager'),
$container->get('current_user'),
$container->get('theme.registry')
);
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $viewmode = 'full', $langcode = NULL) {
$message = parent::view($entity, $viewMode, $langcode);
$classes = ['private-message'];
$classes[] = 'private-message-' . $view_mode;
$classes[] = 'private-message-' . $viewMode;
$classes[] = 'private-message-' . ($this->currentUser->id() == $entity->getOwnerId() ? 'author-self' : 'author-other');
$id = 'private-message-' . $entity->id();
$build['wrapper'] = [
'#type' => 'container',
'#attributes' => [
'id' => $id,
'data-message-id' => $entity->id(),
'class' => $classes,
],
];
$build['wrapper']['message'] = $message;
$build['#prefix'] = '<div id="private-message-' . $entity->id() . '" data-message-id="' . $entity->id() . '" class="' . implode(' ', $classes) . '">';
$build['#suffix'] = '</div>';
$this->moduleHandler()->alter('private_message_view', $build, $entity, $viewMode);
return $build;
}
......
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