3 namespace Drupal\draggableviews\Plugin\views\field;
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;
15 * Defines a draggableviews form element.
17 * @ViewsField("draggable_views_field")
19 class DraggableViewsField extends BulkForm {
24 * @var \Drupal\Core\Entity\EntityManagerInterface
26 protected $entityManager;
31 * @var \Drupal\Core\Entity\EntityStorageInterface
33 protected $actionStorage;
36 * The language manager.
38 * @var \Drupal\Core\Language\LanguageManagerInterface
40 protected $languageManager;
44 * @var \Drupal\Core\Session\AccountInterface
46 protected $currentUser;
49 * Sets the current_user service.
51 * @param \Drupal\Core\Session\AccountInterface $current_user
56 public function setCurrentUser(AccountInterface $current_user) {
57 $this->currentUser = $current_user;
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'));
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."),
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']);
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 . '-->');
109 public function viewsForm(&$form, FormStateInterface $form_state) {
110 $form[$this->options['id']] = [
114 $draggableviews = new DraggableViews($this->view);
116 foreach ($this->view->result as $row_index => $row) {
117 $form[$this->options['id']][$row_index] = [
121 // Item to keep id of the entity.
122 $form[$this->options['id']][$row_index]['id'] = [
124 '#value' => $this->getEntity($row)->id(),
125 '#attributes' => ['class' => ['draggableviews-id']],
129 $form[$this->options['id']][$row_index]['parent'] = [
131 '#default_value' => $draggableviews->getParent($row_index),
132 '#attributes' => ['class' => ['draggableviews-parent']],
136 if ($this->currentUser->hasPermission('access draggableviews')) {
138 'table_id' => $draggableviews->getHtmlId(),
140 'relationship' => 'group',
141 'group' => 'draggableviews-parent',
142 'subgroup' => 'draggableviews-parent',
143 'source' => 'draggableviews-id',
145 drupal_attach_tabledrag($form, $options);