Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / views / sort / EntityQueueInQueue.php
1 <?php
2
3 namespace Drupal\entityqueue\Plugin\views\sort;
4
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;
11
12 /**
13  * Sort handler for ordering the results based on their queue position.
14  *
15  * @ingroup views_sort_handlers
16  *
17  * @ViewsSort("entity_queue_in_queue")
18  */
19 class EntityQueueInQueue extends SortPluginBase {
20
21   /**
22    * The current user.
23    *
24    * @var \Drupal\Core\Session\AccountInterface
25    */
26   protected $currentUser;
27
28   /**
29    * The messenger.
30    *
31    * @var \Drupal\Core\Messenger\MessengerInterface
32    */
33   protected $messenger;
34
35   /**
36    * Constructs a new EntityQueueInQueue object.
37    *
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
45    *   The current user.
46    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
47    *   The messenger.
48    */
49   public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccountInterface $current_user, MessengerInterface $messenger) {
50     parent::__construct($configuration, $plugin_id, $plugin_definition);
51
52     $this->currentUser = $current_user;
53     $this->messenger = $messenger;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60     return new static(
61       $configuration,
62       $plugin_id,
63       $plugin_definition,
64       $container->get('current_user'),
65       $container->get('messenger')
66     );
67   }
68
69   /**
70    * {@inheritdoc}.
71    */
72   public function query() {
73     $this->ensureMyTable();
74
75     // Try to find an entity queue relationship in this view, and pick the first
76     // one available.
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();
83
84         break;
85       }
86     }
87
88     if ($entity_queue_relationship) {
89       // Add the field.
90       $subqueue_items_table_alias = $entity_queue_relationship->first_alias;
91       $this->query->addOrderBy($subqueue_items_table_alias, 'bundle', $this->options['order']);
92     }
93     else {
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);
99       }
100     }
101   }
102
103 }