Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • project/private_message
  • issue/private_message-3102091
  • issue/private_message-3184577
  • issue/private_message-3186067
  • issue/private_message-3095101
  • issue/private_message-3164669
  • issue/private_message-3203419
  • issue/private_message-2942602
  • issue/private_message-3095232
  • issue/private_message-3216927
  • issue/private_message-3217509
  • issue/private_message-3232483
  • issue/private_message-3226703
  • issue/private_message-3231433
  • issue/private_message-3137648
  • issue/private_message-3094213
  • issue/private_message-3156194
  • issue/private_message-3183628
  • issue/private_message-3231711
  • issue/private_message-3142633
  • issue/private_message-3042276
  • issue/private_message-3236702
  • issue/private_message-3239279
  • issue/private_message-3239575
  • issue/private_message-3281258
  • issue/private_message-3243139
  • issue/private_message-3195730
  • issue/private_message-3184550
  • issue/private_message-3041599
  • issue/private_message-3179135
  • issue/private_message-3029461
  • issue/private_message-3245298
  • issue/private_message-3250059
  • issue/private_message-3261287
  • issue/private_message-3221475
  • issue/private_message-3261292
  • issue/private_message-3050359
  • issue/private_message-3249348
  • issue/private_message-3248258
  • issue/private_message-3082478
  • issue/private_message-3265901
  • issue/private_message-3300046
  • issue/private_message-2947762
  • issue/private_message-3321802
  • issue/private_message-3051300
  • issue/private_message-3207855
  • issue/private_message-3099166
  • issue/private_message-3344346
  • issue/private_message-3297889
  • issue/private_message-3261083
  • issue/private_message-3151941
  • issue/private_message-3317985
  • issue/private_message-3285143
  • issue/private_message-3337185
  • issue/private_message-3337186
  • issue/private_message-3337189
  • issue/private_message-3322900
  • issue/private_message-2977313
  • issue/private_message-3333001
  • issue/private_message-3337216
  • issue/private_message-3088743
  • issue/private_message-3337221
  • issue/private_message-3337222
  • issue/private_message-3343726
  • issue/private_message-3337224
  • issue/private_message-3339976
  • issue/private_message-3297888
  • issue/private_message-3355273
  • issue/private_message-3356586
  • issue/private_message-3378251
  • issue/private_message-3391546
  • issue/private_message-3381016
  • issue/private_message-3412209
  • issue/private_message-3414396
  • issue/private_message-3204985
  • issue/private_message-3043898
  • issue/private_message-3384791
  • issue/private_message-3121606
  • issue/private_message-3035510
  • issue/private_message-3337240
  • issue/private_message-3417386
  • issue/private_message-3183523
  • issue/private_message-3386742
  • issue/private_message-3322629
  • issue/private_message-3322625
  • issue/private_message-3417433
  • issue/private_message-3241221
  • issue/private_message-3417449
  • issue/private_message-3417461
  • issue/private_message-3271454
  • issue/private_message-3417457
  • issue/private_message-3417644
  • issue/private_message-3417654
  • issue/private_message-3417705
  • issue/private_message-3417731
  • issue/private_message-3417742
  • issue/private_message-3417747
  • issue/private_message-3417751
  • issue/private_message-3417752
  • issue/private_message-3212104
  • issue/private_message-3427223
  • issue/private_message-3365542
  • issue/private_message-3424499
  • issue/private_message-3432140
  • issue/private_message-3439400
  • issue/private_message-3454771
  • issue/private_message-3456956
  • issue/private_message-3464363
  • issue/private_message-3464371
  • issue/private_message-3470070
  • issue/private_message-3470072
  • issue/private_message-3470928
  • issue/private_message-3475462
  • issue/private_message-2987189
  • issue/private_message-3477667
  • issue/private_message-3477873
  • issue/private_message-3478456
  • issue/private_message-3054106
  • issue/private_message-3479391
  • issue/private_message-3417748
  • issue/private_message-3480219
  • issue/private_message-3482103
  • issue/private_message-3482504
  • issue/private_message-3482630
  • issue/private_message-2971692
  • issue/private_message-3484561
  • issue/private_message-3484828
  • issue/private_message-3484854
  • issue/private_message-3485607
  • issue/private_message-3486915
  • issue/private_message-3487175
  • issue/private_message-3482416
  • issue/private_message-3487689
  • issue/private_message-3487705
  • issue/private_message-3487770
  • issue/private_message-3488097
  • issue/private_message-3484915
  • issue/private_message-3488247
  • issue/private_message-3488508
  • issue/private_message-3487692
  • issue/private_message-3484246
  • issue/private_message-3488863
  • issue/private_message-3486047
  • issue/private_message-3489091
  • issue/private_message-3489205
  • issue/private_message-3490037
  • issue/private_message-3490119
  • issue/private_message-3490456
  • issue/private_message-3489224
  • issue/private_message-3490346
  • issue/private_message-3490816
  • issue/private_message-3491252
  • issue/private_message-3491271
  • issue/private_message-3492177
  • issue/private_message-3492289
  • issue/private_message-3331865
  • issue/private_message-3497800
  • issue/private_message-3499154
  • issue/private_message-3499926
  • issue/private_message-3501083
  • issue/private_message-3501286
  • issue/private_message-3501694
  • issue/private_message-3502128
  • issue/private_message-3502140
  • issue/private_message-3502159
  • issue/private_message-3502162
  • issue/private_message-3502180
  • issue/private_message-3502186
  • issue/private_message-3502190
  • issue/private_message-3502496
  • issue/private_message-3504372
  • issue/private_message-3512770
  • issue/private_message-3513969
173 results
Show changes
Commits on Source (3)
Showing
with 364 additions and 278 deletions
......@@ -17,10 +17,10 @@
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"php": ">=7.4",
"drupal/message": "^1.0",
"drupal/message_notify": "^1.0",
"drupal/core": "^8.8.0 || ^9.0"
"drupal/core": "^8.8.0 || ^9.0 || ^10.0"
},
"suggest": {
"drush/drush": "^9 || ^10"
......
......@@ -2,8 +2,7 @@ name: Private Message Notify
type: module
description: Implements new message notifications.
package: Private Message
core: 8.x
core_version_requirement: ^8 || ^9
core_version_requirement: ^8.8 || ^9 || ^10
dependencies:
- message_notify:message_notify
- private_message:private_message
......@@ -60,6 +60,13 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
*/
protected $messageNotifier;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new PrivateMessageNotifier object.
*
......
name: Private Message
description: A private message system for users to send messages to each other
core_version_requirement: ^8.8 || ^9
core_version_requirement: ^8.8 || ^9 || ^10
type: module
package: Private Message
......
......@@ -160,7 +160,7 @@ function private_message_private_message_thread_view(array &$build, EntityInterf
->getFormObject('private_message', 'add')
->setEntity($private_message);
$build['private_message_form'] = Drupal::formBuilder()
$build['private_message_form'] = \Drupal::formBuilder()
->getForm($form_object, $entity);
}
}
......
parameters:
private_message.mapper.class: 'Drupal\private_message\Mapper\PrivateMessageMapper'
private_message.service.class: 'Drupal\private_message\Service\PrivateMessageService'
private_message.event_subscriber.class: 'Drupal\private_message\Event\PrivateMessageEventSubscriber'
cache_context.private_message_thread.class: 'Drupal\private_message\Cache\Context\PrivateMessageThreadCacheContext'
private_message.thread_manager.class: 'Drupal\private_message\Service\PrivateMessageThreadManager'
private_message.private_message_config_form_manager.class: 'Drupal\private_message\PluginManager\PrivateMessageConfigFormManager'
......@@ -29,7 +28,7 @@ services:
arguments:
- '@current_route_match'
tags:
- { name: cache.context}
- { name: cache.context }
private_message.thread_manager:
class: '%private_message.thread_manager.class%'
......
......@@ -12,7 +12,7 @@ use Drupal\Core\Url;
* Implements hook_preprocess_private_message_notification_block().
*/
function template_preprocess_private_message_notification_block(&$vars) {
$vars['notification_image_path'] = base_path() . drupal_get_path('module', 'private_message') . '/images/private-message-notification-icon.png';
$vars['notification_image_path'] = base_path() . \Drupal::service('extension.list.module')->getPath('private_message') . '/images/private-message-notification-icon.png';
$link_title = $vars['new_message_count'] ? \Drupal::service('string_translation')->formatPlural($vars['new_message_count'], 'You have 1 unread private message', 'You have @count unread private messages') : t('You have no unread private messages');
$url = Url::fromRoute('private_message.private_message_page', [], ['attributes' => ['title' => $link_title, 'class' => ['private-message-page-link']]]);
$count = $vars['new_message_count'] > 99 ? '99+' : $vars['new_message_count'];
......
......@@ -231,7 +231,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Creates an Ajax Command containing new private message.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function getNewPrivateMessages(AjaxResponse $response) {
......@@ -268,7 +268,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Create an Ajax Command containing old private messages.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function getOldPrivateMessages(AjaxResponse $response) {
......@@ -302,7 +302,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Creates and Ajax Command containing old threads for the inbox.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function getOldInboxThreads(AjaxResponse $response) {
......@@ -331,7 +331,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Creates an Ajax Command with new threads for the private message inbox.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function getNewInboxThreads(AjaxResponse $response) {
......@@ -370,7 +370,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Create Ajax Command determining whether a given username is valid.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function validatePrivateMessageMemberUsername(AjaxResponse $response) {
......@@ -386,7 +386,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
* Only messages created since the current user last visited the private
* message page are shown.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function getNewUnreadThreadCount(AjaxResponse $response) {
......@@ -398,7 +398,7 @@ class AjaxController extends ControllerBase implements AjaxControllerInterface {
/**
* Load a private message thread to be dynamically inserted into the page.
*
* @param Drupal\Core\Ajax\AjaxResponse $response
* @param \Drupal\Core\Ajax\AjaxResponse $response
* The response to which any commands should be attached.
*/
protected function loadThread(AjaxResponse $response) {
......
......@@ -6,7 +6,6 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -25,83 +24,54 @@ class PrivateMessageThreadViewBuilder extends EntityViewBuilder {
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The class resolver service.
*
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface
*/
protected $classResolver;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The form builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
protected AccountProxyInterface $currentUser;
/**
* Constructs a PrivateMessageThreadViewBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entityType
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Theme\Registry $themeRegistry
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme register.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
*/
public function __construct(
EntityTypeInterface $entityType,
EntityRepositoryInterface $entityRepository,
LanguageManagerInterface $languageManager,
Registry $themeRegistry,
AccountProxyInterface $currentUser,
EntityDisplayRepositoryInterface $entity_display_repository
EntityTypeInterface $entity_type,
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
Registry $theme_registry,
EntityDisplayRepositoryInterface $entity_display_repository,
AccountProxyInterface $current_user
) {
parent::__construct($entityType, $entityRepository, $languageManager, $themeRegistry, $entity_display_repository);
$this->currentUser = $currentUser;
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entityType) {
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): PrivateMessageThreadViewBuilder {
return new static(
$entityType,
$entity_type,
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('theme.registry'),
$container->get('current_user'),
$container->get('entity_display.repository')
$container->get('entity_display.repository'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL): array {
$build = parent::view($entity, $view_mode, $langcode);
$classes = ['private-message-thread'];
......
......@@ -5,7 +5,6 @@ namespace Drupal\private_message\Entity\Builder;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -14,7 +13,7 @@ use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Build handler for rpivate messages.
* Build handler for private messages.
*/
class PrivateMessageViewBuilder extends EntityViewBuilder {
......@@ -23,55 +22,52 @@ class PrivateMessageViewBuilder extends EntityViewBuilder {
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* Constructs a PrivateMessageViewBuilder object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Theme\Registry $themeRegistry
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme registry.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
*/
public function __construct(
EntityTypeInterface $entityType,
EntityRepositoryInterface $entityRepository,
LanguageManagerInterface $languageManager,
AccountProxyInterface $currentUser,
Registry $themeRegistry = NULL,
EntityDisplayRepositoryInterface $entity_display_repository
EntityTypeInterface $entity_type,
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
Registry $theme_registry,
EntityDisplayRepositoryInterface $entity_display_repository,
AccountProxyInterface $currentUser
) {
parent::__construct($entityType, $entityRepository, $languageManager, $themeRegistry, $entity_display_repository);
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->currentUser = $currentUser;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): PrivateMessageViewBuilder {
return new static(
$entity_type,
$container->get('entity.repository'),
$container->get('language_manager'),
$container->get('current_user'),
$container->get('theme.registry'),
$container->get('entity_display.repository')
$container->get('entity_display.repository'),
$container->get('current_user'),
);
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $viewMode = 'default', $langcode = NULL) {
public function view(EntityInterface $entity, $viewMode = 'default', $langcode = NULL): array {
$message = parent::view($entity, $viewMode, $langcode);
$classes = ['private-message'];
......
......@@ -147,7 +147,7 @@ class PrivateMessageThread extends ContentEntityBase implements PrivateMessageTh
$last_timestamp = 0;
foreach ($messages as $message) {
$creation_date = $message->get('created')->value;
$last_timestamp = $creation_date > $last_timestamp ? $creation_date : $last_timestamp;
$last_timestamp = max($creation_date, $last_timestamp);
}
return $last_timestamp;
......@@ -231,7 +231,7 @@ class PrivateMessageThread extends ContentEntityBase implements PrivateMessageTh
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function clearAccountHistory(AccountInterface $account = NULL) {
if (!$account) {
......
......@@ -60,10 +60,10 @@ interface PrivateMessageThreadInterface extends ContentEntityInterface {
/**
* Add a private message to the list of messages in this thread.
*
* @param Drupal\private_message\Entity\PrivateMessageInterface $privateMessage
* @param \Drupal\private_message\Entity\PrivateMessageInterface $privateMessage
* The private message to be added to the thread.
*
* @return Drupal\private_message\Entity\PrivateMessageThread
* @return \Drupal\private_message\Entity\PrivateMessageThread
* The private message thread.
*/
public function addMessage(PrivateMessageInterface $privateMessage);
......
......@@ -13,11 +13,14 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\private_message\Ajax\PrivateMessageInboxTriggerUpdateCommand;
use Drupal\private_message\Ajax\PrivateMessageLoadNewMessagesCommand;
use Drupal\private_message\Entity\PrivateMessage;
use Drupal\private_message\Entity\PrivateMessageThread;
use Drupal\private_message\Entity\PrivateMessageThreadInterface;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
......@@ -93,6 +96,13 @@ class PrivateMessageForm extends ContentEntityForm {
*/
protected $userManager;
/**
* The Form Builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs a PrivateMessageForm object.
*
......@@ -116,8 +126,22 @@ class PrivateMessageForm extends ContentEntityForm {
* The private message service.
* @param \Drupal\private_message\Service\PrivateMessageThreadManagerInterface $privateMessageThreadManager
* The private message thread manager service.
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The Form Builder service.
*/
public function __construct(EntityRepositoryInterface $entityRepository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, TypedDataManagerInterface $typedDataManager, UserDataInterface $userData, ConfigFactoryInterface $configFactory, PrivateMessageServiceInterface $privateMessageService, PrivateMessageThreadManagerInterface $privateMessageThreadManager) {
public function __construct(
EntityRepositoryInterface $entityRepository,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time,
EntityTypeManagerInterface $entityTypeManager,
AccountProxyInterface $currentUser,
TypedDataManagerInterface $typedDataManager,
UserDataInterface $userData,
ConfigFactoryInterface $configFactory,
PrivateMessageServiceInterface $privateMessageService,
PrivateMessageThreadManagerInterface $privateMessageThreadManager,
FormBuilderInterface $formBuilder,
) {
parent::__construct($entityRepository, $entity_type_bundle_info, $time);
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
......@@ -127,6 +151,7 @@ class PrivateMessageForm extends ContentEntityForm {
$this->privateMessageService = $privateMessageService;
$this->privateMessageThreadManager = $privateMessageThreadManager;
$this->userManager = $entityTypeManager->getStorage('user');
$this->formBuilder = $formBuilder;
}
/**
......@@ -143,7 +168,8 @@ class PrivateMessageForm extends ContentEntityForm {
$container->get('user.data'),
$container->get('config.factory'),
$container->get('private_message.service'),
$container->get('private_message.thread_manager')
$container->get('private_message.thread_manager'),
$container->get('form_builder'),
);
}
......@@ -178,7 +204,6 @@ class PrivateMessageForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state, PrivateMessageThreadInterface $privateMessageThread = NULL) {
$form = parent::buildForm($form, $form_state);
if ($privateMessageThread) {
$form_state->set('thread', $privateMessageThread);
$form['actions']['submit']['#ajax'] = [
......@@ -295,11 +320,40 @@ class PrivateMessageForm extends ContentEntityForm {
/**
* Ajax callback for the PrivateMessageForm.
*
* Re-render form after submission, so user could write new message.
*/
public function ajaxCallback(array $form, FormStateInterface $formState) {
$response = new AjaxResponse();
$form['message']['widget'][0]['value']['#value'] = '';
$response->addCommand(new ReplaceCommand('.private-message-add-form', $form));
/** @var \Drupal\private_message\Entity\PrivateMessageInterface $message */
$message = $formState->getFormObject()->getEntity();
// @todo move to message method
$threads = $this->entityTypeManager
->getStorage('private_message_thread')
->loadByProperties(['private_messages' => $message->id()]);
$thread = reset($threads);
$private_message = PrivateMessage::create();
/** @var \Drupal\private_message\Form\PrivateMessageForm $form_object */
$form_object = $this->entityTypeManager
->getFormObject('private_message', 'add')
->setEntity($private_message);
// We create form State manually so that Drupal won't fill it and
// submit automatically.
$new_form_state = new FormState();
$new_form_state->addBuildInfo('args', [$thread]);
$new_form_state->setUserInput([]);
$new_form = $this->formBuilder
->buildForm($form_object, $new_form_state);
$response->setAttachments($new_form['#attached']);
$response->addCommand(new ReplaceCommand('.private-message-add-form', $new_form));
$response->addCommand(new PrivateMessageLoadNewMessagesCommand());
$response->addCommand(new PrivateMessageInboxTriggerUpdateCommand());
......
......@@ -22,7 +22,7 @@ class PrivateMessageMapper implements PrivateMessageMapperInterface {
/**
* The current user.
*
* @var \Drupal\Session\AccountProxyInterface
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
......@@ -31,7 +31,7 @@ class PrivateMessageMapper implements PrivateMessageMapperInterface {
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Session\AccountProxyInterface $currentUser
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
*/
public function __construct(Connection $database, AccountProxyInterface $currentUser) {
......
......@@ -142,7 +142,7 @@ interface PrivateMessageMapperInterface {
/**
* Load the thread id of the thread that a private message belongs to.
*
* @param Drupal\private_message\Entity\PrivateMessageInterface $privateMessage
* @param \Drupal\private_message\Entity\PrivateMessageInterface $privateMessage
* The private message for which the thread ID of the thread it belongs to
* should be returned.
*
......
......@@ -2,10 +2,13 @@
namespace Drupal\private_message\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -28,14 +31,14 @@ class PrivateMessageActionsBlock extends BlockBase implements BlockPluginInterfa
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* Configuration Factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
protected ConfigFactory $configFactory;
/**
* Constructs a PrivateMessageForm object.
......@@ -67,7 +70,7 @@ class PrivateMessageActionsBlock extends BlockBase implements BlockPluginInterfa
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PrivateMessageActionsBlock {
return new static(
$configuration,
$plugin_id,
......@@ -80,24 +83,30 @@ class PrivateMessageActionsBlock extends BlockBase implements BlockPluginInterfa
/**
* {@inheritdoc}
*/
public function build() {
if ($this->currentUser->hasPermission('use private messaging system')) {
$config = $config = $this->configFactory->get('private_message.settings');
$url = Url::fromRoute('private_message.private_message_create');
$block['links'] = [
'#type' => 'link',
'#title' => $config->get("create_message_label"),
'#url' => $url,
];
protected function blockAccess(AccountInterface $account): AccessResultInterface {
return AccessResult::allowedIf($account->hasPermission('use private messaging system'))
->cachePerPermissions();
}
/**
* {@inheritdoc}
*/
public function build(): array {
$config = $this->configFactory->get('private_message.settings');
$url = Url::fromRoute('private_message.private_message_create');
$block['links'] = [
'#type' => 'link',
'#title' => $config->get("create_message_label"),
'#url' => $url,
];
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-actions-block';
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-actions-block';
return $block;
}
return $block;
}
}
......@@ -2,13 +2,17 @@
namespace Drupal\private_message\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
......@@ -31,35 +35,35 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
*
* @var \Drupal\private_message\Service\PrivateMessageServiceInterface
*/
protected $privateMessageService;
protected PrivateMessageServiceInterface $privateMessageService;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The CSRF token generator service.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator
*/
protected $csrfToken;
protected CsrfTokenGenerator $csrfToken;
/**
* The private message configuration.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
* @var \Drupal\Core\Config\Config
*/
protected $privateMessageConfig;
protected Config $privateMessageConfig;
/**
* Constructs a PrivateMessageForm object.
......@@ -81,7 +85,16 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The config service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser, PrivateMessageServiceInterface $privateMessageService, EntityTypeManagerInterface $entityTypeManager, CsrfTokenGenerator $csrfToken, ConfigFactoryInterface $config) {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
AccountProxyInterface $currentUser,
PrivateMessageServiceInterface $privateMessageService,
EntityTypeManagerInterface $entityTypeManager,
CsrfTokenGenerator $csrfToken,
ConfigFactoryInterface $config
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $currentUser;
......@@ -94,7 +107,7 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PrivateMessageInboxBlock {
return new static(
$configuration,
$plugin_id,
......@@ -110,90 +123,98 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
/**
* {@inheritdoc}
*/
public function build() {
if ($this->currentUser->isAuthenticated() && $this->currentUser->hasPermission('use private messaging system')) {
$config = $this->getConfiguration();
$thread_info = $this->privateMessageService->getThreadsForUser($config['thread_count']);
$total_thread = $this->privateMessageService->getCountThreadsForUser();
if (count($thread_info['threads'])) {
$view_builder = $this->entityTypeManager->getViewBuilder('private_message_thread');
$threads = $thread_info['threads'];
foreach ($threads as $thread) {
$block[$thread->id()] = $view_builder->view($thread, 'inbox');
}
$block['#attached']['library'][] = 'private_message/inbox_block_script';
$style_disabled = $this->privateMessageConfig->get('remove_css');
if (!$style_disabled) {
$block['#attached']['library'][] = 'private_message/inbox_block_style';
}
if (count($threads) && $thread_info['next_exists']) {
$prev_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_old_inbox_threads']);
$prev_token = $this->csrfToken->get($prev_url->getInternalPath());
$prev_url->setOptions(['query' => ['token' => $prev_token]]);
$new_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_inbox_threads']);
$new_token = $this->csrfToken->get($new_url->getInternalPath());
$new_url->setOptions(['query' => ['token' => $new_token]]);
$last_thread = array_pop($threads);
$block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
'oldestTimestamp' => $last_thread->get('updated')->value,
'loadPrevUrl' => $prev_url->toString(),
'loadNewUrl' => $new_url->toString(),
'threadCount' => $config['ajax_load_count'],
];
}
else {
$block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
'oldestTimestamp' => FALSE,
];
}
protected function blockAccess(AccountInterface $account): AccessResultInterface {
return AccessResult::allowedIf(
$account->isAuthenticated()
&& $account->hasPermission('use private messaging system')
)->cachePerPermissions();
}
/**
* {@inheritdoc}
*/
public function build(): array {
$config = $this->getConfiguration();
$thread_info = $this->privateMessageService->getThreadsForUser($config['thread_count']);
$total_thread = $this->privateMessageService->getCountThreadsForUser();
if (count($thread_info['threads'])) {
$view_builder = $this->entityTypeManager->getViewBuilder('private_message_thread');
$threads = $thread_info['threads'];
foreach ($threads as $thread) {
$block[$thread->id()] = $view_builder->view($thread, 'inbox');
}
$block['#attached']['library'][] = 'private_message/inbox_block_script';
$style_disabled = $this->privateMessageConfig->get('remove_css');
if (!$style_disabled) {
$block['#attached']['library'][] = 'private_message/inbox_block_style';
}
if (count($threads) && $thread_info['next_exists']) {
$prev_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_old_inbox_threads']);
$prev_token = $this->csrfToken->get($prev_url->getInternalPath());
$prev_url->setOptions(['query' => ['token' => $prev_token]]);
$new_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_inbox_threads']);
$new_token = $this->csrfToken->get($new_url->getInternalPath());
$new_url->setOptions(['query' => ['token' => $new_token]]);
$last_thread = array_pop($threads);
$block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
'oldestTimestamp' => $last_thread->get('updated')->value,
'loadPrevUrl' => $prev_url->toString(),
'loadNewUrl' => $new_url->toString(),
'threadCount' => $config['ajax_load_count'],
];
}
else {
$block['no_threads'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this->t('You do not have any private messages'),
$block['#attached']['drupalSettings']['privateMessageInboxBlock'] = [
'oldestTimestamp' => FALSE,
];
}
}
else {
$block['no_threads'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this->t('You do not have any private messages'),
];
}
$new_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_inbox_threads']);
$new_token = $this->csrfToken->get($new_url->getInternalPath());
$new_url->setOptions(['query' => ['token' => $new_token]]);
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['loadNewUrl'] = $new_url->toString();
$config = $this->getConfiguration();
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['totalThreads'] = $total_thread;
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['itemsToShow'] = $config['thread_count'];
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-inbox-block';
$new_url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_inbox_threads']);
$new_token = $this->csrfToken->get($new_url->getInternalPath());
$new_url->setOptions(['query' => ['token' => $new_token]]);
// Wrapper using in js to place Load Previous button for multiple threads.
$block['#prefix'] = '<div class="private-message-thread--full-container">';
$block['#suffix'] = '</div>';
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['loadNewUrl'] = $new_url->toString();
return $block;
}
$config = $this->getConfiguration();
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['totalThreads'] = $total_thread;
$block['#attached']['drupalSettings']['privateMessageInboxBlock']['itemsToShow'] = $config['thread_count'];
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-inbox-block';
// Wrapper using in js to place Load Previous button for multiple threads.
$block['#prefix'] = '<div class="private-message-thread--full-container">';
$block['#suffix'] = '</div>';
return $block;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
public function getCacheTags(): array {
return Cache::mergeTags(parent::getCacheTags(), ['private_message_inbox_block:uid:' . $this->currentUser->id()]);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
public function getCacheContexts(): array {
// Vary caching of this block per user.
return Cache::mergeContexts(parent::getCacheContexts(), ['user']);
}
......@@ -201,7 +222,7 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
public function defaultConfiguration(): array {
return [
'thread_count' => 5,
'ajax_load_count' => 5,
......@@ -212,7 +233,7 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
public function blockForm($form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
......@@ -247,7 +268,7 @@ class PrivateMessageInboxBlock extends BlockBase implements BlockPluginInterface
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
public function blockSubmit($form, FormStateInterface $form_state): void {
$this->configuration['thread_count'] = $form_state->getValue('thread_count');
$this->configuration['ajax_load_count'] = $form_state->getValue('ajax_load_count');
$this->configuration['ajax_refresh_rate'] = $form_state->getValue('ajax_refresh_rate');
......
......@@ -2,12 +2,16 @@
namespace Drupal\private_message\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Config;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
......@@ -30,28 +34,28 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* The CSRF token generator service.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator
*/
protected $csrfToken;
protected CsrfTokenGenerator $csrfToken;
/**
* The private message service.
*
* @var \Drupal\private_message\Service\PrivateMessageServiceInterface
*/
protected $privateMessageService;
protected PrivateMessageServiceInterface $privateMessageService;
/**
* The private message configuration.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
* @var \Drupal\Core\Config\Config
*/
protected $privateMessageConfig;
protected Config $privateMessageConfig;
/**
* Constructs a PrivateMessageForm object.
......@@ -71,7 +75,15 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The config service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser, CsrfTokenGenerator $csrfToken, PrivateMessageServiceInterface $privateMessageService, ConfigFactoryInterface $config) {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
AccountProxyInterface $currentUser,
CsrfTokenGenerator $csrfToken,
PrivateMessageServiceInterface $privateMessageService,
ConfigFactoryInterface $config
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $currentUser;
......@@ -83,7 +95,7 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PrivateMessageNotificationBlock {
return new static(
$configuration,
$plugin_id,
......@@ -98,48 +110,57 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
/**
* {@inheritdoc}
*/
public function build() {
if ($this->currentUser->isAuthenticated() && $this->currentUser->hasPermission('use private messaging system')) {
$block = [
'#theme' => 'private_message_notification_block',
'#new_message_count' => $this->privateMessageService->getUnreadThreadCount(),
];
$url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_unread_thread_count']);
$token = $this->csrfToken->get($url->getInternalPath());
$url->setOptions(['query' => ['token' => $token]]);
$block['#attached']['drupalSettings']['privateMessageNotificationBlock']['newMessageCountCallback'] = $url->toString();
$config = $this->getConfiguration();
$block['#attached']['drupalSettings']['privateMessageNotificationBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
$block['#attached']['library'][] = 'private_message/notification_block_script';
$style_disabled = $this->privateMessageConfig->get('remove_css');
if (!$style_disabled) {
$block['#attached']['library'][] = 'private_message/notification_block_style';
}
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-notification-block';
return $block;
protected function blockAccess(AccountInterface $account): AccessResultInterface {
return AccessResult::allowedIf(
$account->isAuthenticated() &&
$account->hasPermission('use private messaging system')
)->cachePerPermissions();
}
/**
* {@inheritdoc}
*/
public function build(): array {
$block = [
'#theme' => 'private_message_notification_block',
'#new_message_count' => $this->privateMessageService->getUnreadThreadCount(),
];
$url = Url::fromRoute('private_message.ajax_callback', ['op' => 'get_new_unread_thread_count']);
$token = $this->csrfToken->get($url->getInternalPath());
$url->setOptions(['query' => ['token' => $token]]);
$block['#attached']['drupalSettings']['privateMessageNotificationBlock']['newMessageCountCallback'] = $url->toString();
$config = $this->getConfiguration();
$block['#attached']['drupalSettings']['privateMessageNotificationBlock']['ajaxRefreshRate'] = $config['ajax_refresh_rate'];
$block['#attached']['library'][] = 'private_message/notification_block_script';
$style_disabled = $this->privateMessageConfig->get('remove_css');
if (!$style_disabled) {
$block['#attached']['library'][] = 'private_message/notification_block_style';
}
// Add the default classes, as these are not added when the block output
// is overridden with a template.
$block['#attributes']['class'][] = 'block';
$block['#attributes']['class'][] = 'block-private-message';
$block['#attributes']['class'][] = 'block-private-message-notification-block';
return $block;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
public function getCacheTags(): array {
return Cache::mergeTags(parent::getCacheTags(), ['private_message_notification_block:uid:' . $this->currentUser->id()]);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
public function getCacheContexts(): array {
// Vary caching of this block per user.
return Cache::mergeContexts(parent::getCacheContexts(), ['user']);
}
......@@ -147,7 +168,7 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
public function defaultConfiguration(): array {
return [
'ajax_refresh_rate' => 15,
];
......@@ -156,7 +177,7 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
public function blockForm($form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
......@@ -175,7 +196,7 @@ class PrivateMessageNotificationBlock extends BlockBase implements BlockPluginIn
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
public function blockSubmit($form, FormStateInterface $form_state): void {
$this->configuration['ajax_refresh_rate'] = $form_state->getValue('ajax_refresh_rate');
}
......
......@@ -10,6 +10,7 @@ use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -32,21 +33,28 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
protected EntityDisplayRepositoryInterface $entityDisplayRepository;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected RendererInterface $renderer;
/**
* Construct a PrivateMessageThreadFormatter object.
......@@ -65,9 +73,9 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
* The current view mode.
* @param array $third_party_settings
* The third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager service.
* @param |Drupal\Core\Session\AccountProxyInterface $currentUser
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
......@@ -80,20 +88,23 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
$label,
$view_mode,
array $third_party_settings,
EntityTypeManagerInterface $entityTypeManager,
AccountProxyInterface $currentUser,
EntityDisplayRepositoryInterface $entity_display_repository) {
EntityTypeManagerInterface $entity_type_manager,
AccountProxyInterface $current_user,
EntityDisplayRepositoryInterface $entity_display_repository,
RendererInterface $renderer
) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->entityDisplayRepository = $entity_display_repository;
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PrivateMessageThreadMemberFormatter {
return new static(
$plugin_id,
$plugin_definition,
......@@ -104,21 +115,22 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
$configuration['third_party_settings'],
$container->get('entity_type.manager'),
$container->get('current_user'),
$container->get('entity_display.repository')
$container->get('entity_display.repository'),
$container->get('renderer')
);
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
public static function isApplicable(FieldDefinitionInterface $field_definition): bool {
return ($field_definition->getFieldStorageDefinition()->getTargetEntityTypeId() == 'private_message_thread' && $field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'user');
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
public function settingsSummary(): array {
$summary = [];
if ($this->getSetting('display_type') == 'label') {
......@@ -160,7 +172,7 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
public static function defaultSettings(): array {
return [
'display_type' => 'label',
'entity_display_mode' => 'private_message_author',
......@@ -174,7 +186,7 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
public function settingsForm(array $form, FormStateInterface $form_state): array {
$element['display_type'] = [
'#title' => $this->t('Display Type'),
'#type' => 'select',
......@@ -255,16 +267,14 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
/**
* Ajax callback for settings form.
*/
public function ajaxCallback(array $form, FormStateInterface $form_state) {
public function ajaxCallback(array $form, FormStateInterface $form_state): array {
return $form['fields'][$this->getFieldName()]['plugin']['settings_edit_form']['settings']['entity_display_mode'];
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
public function viewElements(FieldItemListInterface $items, $langcode): array {
$access_profiles = $this->currentUser->hasPermission('access user profiles');
$users = [];
$display_current_user = $this->getSetting('display_current_user');
......@@ -275,8 +285,8 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
if ($user) {
if ($user->id() != $this->currentUser->id() || ($user->id() == $this->currentUser->id() && $display_current_user)) {
if ($this->getSetting('display_type') == 'label') {
if ($access_profiles) {
$url = Url::fromRoute('entity.user.canonical', ['user' => $user->id()]);
$url = Url::fromRoute('entity.user.canonical', ['user' => $user->id()]);
if ($access_profiles && $url->access()) {
$users[$user->id()] = new FormattableMarkup('<a href=":link">@username</a>', [':link' => $url->toString(), '@username' => $user->getDisplayName()]);
}
else {
......@@ -285,7 +295,7 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
}
elseif ($this->getSetting('display_type') == 'entity') {
$renderable = $view_builder->view($user, $this->getSetting('entity_display_mode'));
$users[$user->id()] = render($renderable);
$users[$user->id()] = $this->renderer->render($renderable);
}
}
}
......@@ -317,7 +327,7 @@ class PrivateMessageThreadMemberFormatter extends FormatterBase implements Conta
/**
* Retrieve the name of the field.
*/
protected function getFieldName() {
protected function getFieldName(): string {
return $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName();
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\private_message\Plugin\Field\FieldFormatter;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
......@@ -12,6 +13,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
......@@ -33,42 +35,42 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected AccountProxyInterface $currentUser;
/**
* The CSRF token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator
*/
protected $csrfTokenGenerator;
protected CsrfTokenGenerator $csrfTokenGenerator;
/**
* The user manager service.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userManager;
protected UserStorageInterface $userManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ImmutableConfig
* @var \Drupal\Core\Config\Config
*/
protected $config;
protected Config $config;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
protected EntityDisplayRepositoryInterface $entityDisplayRepository;
/**
* Construct a PrivateMessageThreadFormatter object.
......@@ -89,7 +91,7 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
* The third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity manager service.
* @param |Drupal\Core\Session\AccountProxyInterface $currentUser
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrfTokenGenerator
* The CSRF token generator.
......@@ -116,15 +118,15 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->csrfTokenGenerator = $csrfTokenGenerator;
$this->userManager = $entityTypeManager->getStorage('user');
$this->config = $configFactory->get('private_message.settings');
$this->userManager = $entityTypeManager->getStorage('user');
$this->entityDisplayRepository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PrivateMessageThreadMessageFormatter {
return new static(
$plugin_id,
$plugin_definition,
......@@ -144,14 +146,14 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
public static function isApplicable(FieldDefinitionInterface $field_definition): bool {
return ($field_definition->getFieldStorageDefinition()->getTargetEntityTypeId() == 'private_message_thread' && $field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'private_message');
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
public static function defaultSettings(): array {
return [
'message_count' => 5,
'ajax_previous_load_count' => 5,
......@@ -164,7 +166,7 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
/**
* {@inheritdoc}
*/
public function settingsSummary() {
public function settingsSummary(): array {
$summary = [];
$settings = $this->getSettings();
......@@ -186,7 +188,7 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
public function settingsForm(array $form, FormStateInterface $form_state): array {
$element['message_count'] = [
'#title' => $this->t('Message Count'),
'#type' => 'number',
......@@ -232,9 +234,7 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
public function viewElements(FieldItemListInterface $items, $langcode): array {
$private_message_thread = $items->getEntity();
$element = [
......@@ -313,7 +313,7 @@ class PrivateMessageThreadMessageFormatter extends FormatterBase implements Cont
'desc' => $this->t('Descending'),
];
return isset($keys[$value]) ? $keys[$value] : $value;
return $keys[$value] ?? $value;
}
return $value;
......