Skip to content
Snippets Groups Projects
Commit 044e5de6 authored by Claudiu Cristea's avatar Claudiu Cristea
Browse files

Issue #2987189 by loze, claudiu.cristea, nishruu, adam clarey, cosolom, phjou,...

Issue #2987189 by loze, claudiu.cristea, nishruu, adam clarey, cosolom, phjou, ekulkisnek, authintmedia: Realname support
parent ce64f7d6
No related branches found
No related tags found
1 merge request!175Use default:selection handler
Pipeline #409136 canceled
......@@ -10,12 +10,15 @@ declare(strict_types=1);
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\private_message\Entity\PrivateMessage;
use Drupal\user\Entity\User;
use Drupal\user\Plugin\EntityReferenceSelection\UserSelection;
use Drupal\user\RoleInterface;
/**
* Implements hook_views_data().
......@@ -691,3 +694,59 @@ function private_message_theme_suggestions_private_message_thread_alter(array &$
function private_message_theme_suggestions_private_message_alter(array &$suggestions, array &$vars): void {
$suggestions[] = 'private_message__' . $vars['elements']['#view_mode'];
}
/**
* Implements hook_entity_query_ENTITY_TYPE_alter().
*/
function private_message_entity_query_user_alter(QueryInterface $query): void {
if (!$handler = $query->getMetadata('entity_reference_selection_handler')) {
return;
}
if (!$handler instanceof UserSelection) {
return;
}
// Filter out blocked users.
$config = $handler->getConfiguration();
if (empty($config['private_message']['active_users_selection'])) {
return;
}
$roles = array_filter(
\Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple(),
fn(RoleInterface $role): bool => $role->hasPermission('use private messaging system'),
);
if (!$rids = array_keys($roles)) {
// Provide condition which will not return any results.
$query->condition('uid');
return;
}
$current_user_id = \Drupal::currentUser()->id();
$subquery = \Drupal::database()->select('private_message_ban', 'pmb')
->fields('pmb', ['target']);
$subquery->condition('owner', $current_user_id);
$query->condition('uid', $subquery, 'NOT IN');
if (!in_array('authenticated', $rids)) {
$query->condition('roles', $rids, 'IN');
}
$query->condition('uid', $current_user_id, '<>');
}
/**
* Implements hook_config_schema_info_alter().
*/
function private_message_config_schema_info_alter(array &$definitions): void {
if (isset($definitions['entity_reference_selection.default:user'])) {
// Required to pass config_inspector validation.
$definitions['entity_reference_selection.default:user']['mapping']['private_message'] = [
'active_users_selection' => [
'type' => 'boolean',
'label' => 'Whether to filter out blocked user on member selection widget',
],
];
}
}
......@@ -41,9 +41,13 @@ class BanUserForm extends FormBase {
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#tags' => FALSE,
'#selection_handler' => 'private_message:not_blocked_user',
'#selection_handler' => 'default:user',
'#selection_settings' => [
'include_anonymous' => FALSE,
// @see \private_message_entity_query_user_alter()
'private_message' => [
'active_users_selection' => TRUE,
],
],
];
......
<?php
declare(strict_types=1);
namespace Drupal\private_message\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\Plugin\EntityReferenceSelection\UserSelection;
use Drupal\user\RoleInterface;
/**
* Provides reference selection for not blocked users.
*/
#[EntityReferenceSelection(
id: "private_message:not_blocked_user",
label: new TranslatableMarkup("Not blocked user selection"),
group: "private_message",
weight: 3,
entity_types: ["user"]
)]
class NotBlockedUserSelection extends UserSelection {
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS'): QueryInterface {
$query = parent::buildEntityQuery($match, $match_operator);
$rids = $this->getCanUseRids();
// Provide condition which will not return any results.
if (empty($rids)) {
$query->condition('uid');
return $query;
}
$subquery = $this->connection->select('private_message_ban', 'pmb')
->fields('pmb', ['target']);
$subquery->condition('owner', $this->currentUser->id());
$query->condition('uid', $subquery, 'NOT IN');
if (!in_array('authenticated', $rids)) {
$query->condition('roles', $rids, 'IN');
}
$query->condition('uid', $this->currentUser->id(), '<>');
return $query;
}
/**
* Returns role ids with permission to use PM system.
*
* @return int[]|string[]
* Array of role IDs.
*/
protected function getCanUseRids(): array {
$use_pm_permission = 'use private messaging system';
$roles = array_filter(
$this->entityTypeManager->getStorage('user_role')->loadMultiple(),
fn(RoleInterface $role): bool => $role->hasPermission($use_pm_permission),
);
return array_keys($roles);
}
}
......@@ -124,9 +124,13 @@ class PrivateMessageThreadMemberWidget extends EntityReferenceAutocompleteWidget
$element['target_id']['#title'] = $this->t('To');
$element['target_id']['#required'] = TRUE;
$element['target_id']['#default_value'] = $items->referencedEntities();
$element['target_id']['#selection_handler'] = 'private_message:not_blocked_user';
$element['target_id']['#selection_handler'] = 'default:user';
$element['target_id']['#selection_settings'] = [
'include_anonymous' => FALSE,
// @see \private_message_entity_query_user_alter()
'private_message' => [
'active_users_selection' => TRUE,
],
];
$element['target_id']['#validate_reference'] = TRUE;
......
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