Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Config / Entity / DraggableListBuilder.php
1 <?php
2
3 namespace Drupal\Core\Config\Entity;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormInterface;
9 use Drupal\Core\Form\FormStateInterface;
10
11 /**
12  * Defines a class to build a draggable listing of configuration entities.
13  */
14 abstract class DraggableListBuilder extends ConfigEntityListBuilder implements FormInterface {
15
16   /**
17    * The key to use for the form element containing the entities.
18    *
19    * @var string
20    */
21   protected $entitiesKey = 'entities';
22
23   /**
24    * The entities being listed.
25    *
26    * @var \Drupal\Core\Entity\EntityInterface[]
27    */
28   protected $entities = [];
29
30   /**
31    * Name of the entity's weight field or FALSE if no field is provided.
32    *
33    * @var string|bool
34    */
35   protected $weightKey = FALSE;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected $limit = FALSE;
41
42   /**
43    * The form builder.
44    *
45    * @var \Drupal\Core\Form\FormBuilderInterface
46    */
47   protected $formBuilder;
48
49   /**
50    * {@inheritdoc}
51    */
52   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
53     parent::__construct($entity_type, $storage);
54
55     // Check if the entity type supports weighting.
56     if ($this->entityType->hasKey('weight')) {
57       $this->weightKey = $this->entityType->getKey('weight');
58     }
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function buildHeader() {
65     $header = [];
66     if (!empty($this->weightKey)) {
67       $header['weight'] = t('Weight');
68     }
69     return $header + parent::buildHeader();
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function buildRow(EntityInterface $entity) {
76     $row = [];
77     if (!empty($this->weightKey)) {
78       // Override default values to markup elements.
79       $row['#attributes']['class'][] = 'draggable';
80       $row['#weight'] = $entity->get($this->weightKey);
81       // Add weight column.
82       $row['weight'] = [
83         '#type' => 'weight',
84         '#title' => t('Weight for @title', ['@title' => $entity->label()]),
85         '#title_display' => 'invisible',
86         '#default_value' => $entity->get($this->weightKey),
87         '#attributes' => ['class' => ['weight']],
88       ];
89     }
90     return $row + parent::buildRow($entity);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function render() {
97     if (!empty($this->weightKey)) {
98       return $this->formBuilder()->getForm($this);
99     }
100     return parent::render();
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function buildForm(array $form, FormStateInterface $form_state) {
107     $form[$this->entitiesKey] = [
108       '#type' => 'table',
109       '#header' => $this->buildHeader(),
110       '#empty' => t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]),
111       '#tabledrag' => [
112         [
113           'action' => 'order',
114           'relationship' => 'sibling',
115           'group' => 'weight',
116         ],
117       ],
118     ];
119
120     $this->entities = $this->load();
121     $delta = 10;
122     // Change the delta of the weight field if have more than 20 entities.
123     if (!empty($this->weightKey)) {
124       $count = count($this->entities);
125       if ($count > 20) {
126         $delta = ceil($count / 2);
127       }
128     }
129     foreach ($this->entities as $entity) {
130       $row = $this->buildRow($entity);
131       if (isset($row['label'])) {
132         $row['label'] = ['#markup' => $row['label']];
133       }
134       if (isset($row['weight'])) {
135         $row['weight']['#delta'] = $delta;
136       }
137       $form[$this->entitiesKey][$entity->id()] = $row;
138     }
139
140     $form['actions']['#type'] = 'actions';
141     $form['actions']['submit'] = [
142       '#type' => 'submit',
143       '#value' => t('Save'),
144       '#button_type' => 'primary',
145     ];
146
147     return $form;
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function validateForm(array &$form, FormStateInterface $form_state) {
154     // No validation.
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function submitForm(array &$form, FormStateInterface $form_state) {
161     foreach ($form_state->getValue($this->entitiesKey) as $id => $value) {
162       if (isset($this->entities[$id]) && $this->entities[$id]->get($this->weightKey) != $value['weight']) {
163         // Save entity only when its weight was changed.
164         $this->entities[$id]->set($this->weightKey, $value['weight']);
165         $this->entities[$id]->save();
166       }
167     }
168   }
169
170   /**
171    * Returns the form builder.
172    *
173    * @return \Drupal\Core\Form\FormBuilderInterface
174    *   The form builder.
175    */
176   protected function formBuilder() {
177     if (!$this->formBuilder) {
178       $this->formBuilder = \Drupal::formBuilder();
179     }
180     return $this->formBuilder;
181   }
182
183 }