Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / draggableviews / src / Plugin / views / field / DraggableViewsField.php
1 <?php
2
3 namespace Drupal\draggableviews\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\draggableviews\DraggableViews;
7 use Drupal\system\Plugin\views\field\BulkForm;
8 use Drupal\Core\Render\Markup;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a draggableviews form element.
16  *
17  * @ViewsField("draggable_views_field")
18  */
19 class DraggableViewsField extends BulkForm {
20
21   /**
22    * The entity manager.
23    *
24    * @var \Drupal\Core\Entity\EntityManagerInterface
25    */
26   protected $entityManager;
27
28   /**
29    * The action storage.
30    *
31    * @var \Drupal\Core\Entity\EntityStorageInterface
32    */
33   protected $actionStorage;
34
35   /**
36    * The language manager.
37    *
38    * @var \Drupal\Core\Language\LanguageManagerInterface
39    */
40   protected $languageManager;
41   /**
42    * The Current user.
43    *
44    * @var \Drupal\Core\Session\AccountInterface
45    */
46   protected $currentUser;
47
48   /**
49    * Sets the current_user service.
50    *
51    * @param \Drupal\Core\Session\AccountInterface $current_user
52    *   Current user.
53    *
54    * @return $this
55    */
56   public function setCurrentUser(AccountInterface $current_user) {
57     $this->currentUser = $current_user;
58     return $this;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65     /** @var static $datasource */
66     $bulk_form = parent::create($container, $configuration, $plugin_id, $plugin_definition);
67     $bulk_form->setCurrentUser($container->get('current_user'));
68     return $bulk_form;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
75     $form['draggableview_help'] = [
76       '#markup' => $this->t("A draggable element will be added to the first table column. You do not have to set this field as the first column in your View."),
77     ];
78     parent::buildOptionsForm($form, $form_state);
79     // Remove all the fields that would break this or are completely ignored
80     // when rendering the drag interface.
81     unset($form['custom_label']);
82     unset($form['label']);
83     unset($form['element_label_colon']);
84     unset($form['action_title']);
85     unset($form['include_exclude']);
86     unset($form['selected_actions']);
87     unset($form['exclude']);
88     unset($form['alter']);
89     unset($form['empty_field_behavior']);
90     unset($form['empty']);
91     unset($form['empty_zero']);
92     unset($form['hide_empty']);
93     unset($form['hide_alter_empty']);
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   // @codingStandardsIgnoreStart
100   public function render_item($count, $item) {
101     // @codingStandardsIgnoreEnd
102     // Using internal method. @todo Reckeck after drupal stable release.
103     return Markup::create('<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->');
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function viewsForm(&$form, FormStateInterface $form_state) {
110     $form[$this->options['id']] = [
111       '#tree' => TRUE,
112     ];
113
114     $draggableviews = new DraggableViews($this->view);
115
116     foreach ($this->view->result as $row_index => $row) {
117       $form[$this->options['id']][$row_index] = [
118         '#tree' => TRUE,
119       ];
120
121       // Item to keep id of the entity.
122       $form[$this->options['id']][$row_index]['id'] = [
123         '#type' => 'hidden',
124         '#value' => $this->getEntity($row)->id(),
125         '#attributes' => ['class' => ['draggableviews-id']],
126       ];
127
128       // Add parent.
129       $form[$this->options['id']][$row_index]['parent'] = [
130         '#type' => 'hidden',
131         '#default_value' => $draggableviews->getParent($row_index),
132         '#attributes' => ['class' => ['draggableviews-parent']],
133       ];
134     }
135
136     if ($this->currentUser->hasPermission('access draggableviews')) {
137       $options = [
138         'table_id' => $draggableviews->getHtmlId(),
139         'action' => 'match',
140         'relationship' => 'group',
141         'group' => 'draggableviews-parent',
142         'subgroup' => 'draggableviews-parent',
143         'source' => 'draggableviews-id',
144       ];
145       drupal_attach_tabledrag($form, $options);
146     }
147   }
148
149 }