OpignoMessageThreadsSorting.php in Opigno messaging 3.x
File
src/Plugin/views/sort/OpignoMessageThreadsSorting.php
View source
<?php
namespace Drupal\opigno_messaging\Plugin\views\sort;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\sort\SortPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class OpignoMessageThreadsSorting extends SortPluginBase {
protected $routeMatch;
protected $router;
protected $request;
public function __construct(RouteMatchInterface $route_match, AccessAwareRouterInterface $router, RequestStack $request_stack, ...$default) {
parent::__construct(...$default);
$this->routeMatch = $route_match;
$this->router = $router;
$this->request = $request_stack
->getCurrentRequest();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('current_route_match'), $container
->get('router'), $container
->get('request_stack'), $configuration, $plugin_id, $plugin_definition);
}
public function canExpose() {
return FALSE;
}
public function query() {
$this
->ensureMyTable();
$order = $this->options['order'];
if ($this->request instanceof Request && $this->request
->isXmlHttpRequest()) {
$route = $this->request->server
->get('HTTP_REFERER');
$route_info = $this->router
->match($route);
$params = $route_info['_raw_variables'] ?? NULL;
$thread = $params instanceof ParameterBag ? $params
->get('private_message_thread') : NULL;
}
else {
$thread = $this->routeMatch
->getRawParameter('private_message_thread');
}
if ($thread) {
$this->query
->addOrderBy(NULL, "FIELD({$this->tableAlias}.id, {$thread})", $order, 'current_thread');
}
$this->query
->addOrderBy(NULL, "{$this->tableAlias}.{$this->realField}", $order, 'by_date');
}
}