3 namespace Drupal\entityqueue\Plugin\views\sort;
5 use Drupal\Core\Messenger\MessengerInterface;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\entityqueue\Plugin\views\relationship\EntityQueueRelationship;
8 use Drupal\views\Plugin\views\sort\SortPluginBase;
9 use Drupal\Core\Messenger\Messenger;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Sort handler for ordering the results based on their queue position.
15 * @ingroup views_sort_handlers
17 * @ViewsSort("entity_queue_in_queue")
19 class EntityQueueInQueue extends SortPluginBase {
24 * @var \Drupal\Core\Session\AccountInterface
26 protected $currentUser;
31 * @var \Drupal\Core\Messenger\MessengerInterface
36 * Constructs a new EntityQueueInQueue object.
38 * @param array $configuration
39 * A configuration array containing information about the plugin instance.
40 * @param string $plugin_id
41 * The plugin_id for the plugin instance.
42 * @param array $plugin_definition
43 * The plugin implementation definition.
44 * @param \Drupal\Core\Session\AccountInterface $current_user
46 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
49 public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccountInterface $current_user, MessengerInterface $messenger) {
50 parent::__construct($configuration, $plugin_id, $plugin_definition);
52 $this->currentUser = $current_user;
53 $this->messenger = $messenger;
59 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64 $container->get('current_user'),
65 $container->get('messenger')
72 public function query() {
73 $this->ensureMyTable();
75 // Try to find an entity queue relationship in this view, and pick the first
77 $entity_queue_relationship = NULL;
78 foreach ($this->view->relationship as $id => $relationship) {
79 if ($relationship instanceof EntityQueueRelationship) {
80 $entity_queue_relationship = $relationship;
81 $this->options['relationship'] = $id;
82 $this->setRelationship();
88 if ($entity_queue_relationship) {
90 $subqueue_items_table_alias = $entity_queue_relationship->first_alias;
91 $this->query->addOrderBy($subqueue_items_table_alias, 'bundle', $this->options['order']);
94 if ($this->currentUser->hasPermission('administer views')) {
95 $this->messenger->addMessage($this->t('In order to sort by in queue, you need to add the Entityqueue: Queue relationship on View: @view with display: @display',
96 ['@view' => $this->view->storage->label(),
97 '@display' => $this->view->current_display]
98 ), Messenger::TYPE_ERROR);