Version 1
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / views / sort / EntityQueuePosition.php
1 <?php
2
3 namespace Drupal\entityqueue\Plugin\views\sort;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\entityqueue\Plugin\views\relationship\EntityQueueRelationship;
7 use Drupal\views\Plugin\views\sort\SortPluginBase;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Default implementation of the base sort plugin.
12  *
13  * @ingroup views_sort_handlers
14  *
15  * @ViewsSort("entity_queue_position")
16  */
17 class EntityQueuePosition extends SortPluginBase {
18
19   /**
20    * The current user.
21    *
22    * @var \Drupal\Core\Session\AccountInterface
23    */
24   protected $currentUser;
25
26   /**
27    * Constructor.
28    *
29    * @param array $configuration
30    *   A configuration array containing information about the plugin instance.
31    * @param string $plugin_id
32    *   The plugin_id for the plugin instance.
33    * @param array $plugin_definition
34    *   The plugin implementation definition.
35    * @param \Drupal\Core\Session\AccountInterface $current_user
36    *   The current user.
37    */
38   public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccountInterface $current_user) {
39     parent::__construct($configuration, $plugin_id, $plugin_definition);
40
41     $this->currentUser = $current_user;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48     return new static(
49       $configuration,
50       $plugin_id,
51       $plugin_definition,
52       $container->get('current_user')
53     );
54   }
55
56   /**
57    * {@inheritdoc}.
58    */
59   public function query() {
60     $this->ensureMyTable();
61
62     // Try to find an entity queue relationship in this view, and pick the first
63     // one available.
64     $entity_queue_relationship = NULL;
65     foreach ($this->view->relationship as $id => $relationship) {
66       if ($relationship instanceof EntityQueueRelationship) {
67         $entity_queue_relationship = $relationship;
68         $this->options['relationship'] = $id;
69         $this->setRelationship();
70
71         break;
72       }
73     }
74
75     if ($entity_queue_relationship) {
76       // Add the field.
77       $subqueue_items_table_alias = $entity_queue_relationship->first_alias;
78       $this->query->addOrderBy($subqueue_items_table_alias, $this->realField, $this->options['order']);
79     }
80     else {
81       if ($this->currentUser->hasPermission('administer views')) {
82         drupal_set_message($this->t('In order to sort by the queue position, you need to add the Entityqueue: Queue relationship on View: @view with display: @display', ['@view' => $this->view->storage->label(), '@display' => $this->view->current_display]), 'error');
83       }
84     }
85   }
86
87 }