-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDraggableOverviewBuilderBase.php
More file actions
190 lines (155 loc) · 5.46 KB
/
DraggableOverviewBuilderBase.php
File metadata and controls
190 lines (155 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Drupal\wmentity_overview\OverviewBuilder;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\wmentity_overview\Annotation\OverviewBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @see DraggableListBuilder
*/
abstract class DraggableOverviewBuilderBase extends OverviewBuilderBase implements FormInterface
{
/**
* The key to use for the form element containing the entities.
* @var string
*/
protected $entitiesKey = 'entities';
/**
* The entities being listed.
*
* @var EntityInterface[]
*/
protected $entities = [];
/**
* Name of the entity's weight field
* @var string
*/
protected $weightKey;
/** @var FormBuilderInterface */
protected $formBuilder;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage)
{
parent::__construct($entity_type, $storage);
// Check if the entity type supports weighting.
if ($this->entityType->hasKey('weight')) {
$this->weightKey = $this->entityType->getKey('weight');
}
// Draggable overviews are ordered by weight
$this->sort = false;
// Draggable overviews cannot be paged
$this->limit = false;
}
public static function create(ContainerInterface $container, OverviewBuilder $definition)
{
$instance = parent::create($container, $definition);
$instance->formBuilder = $container->get('form_builder');
return $instance;
}
public function buildHeader()
{
$header = parent::buildHeader();
if (!empty($this->weightKey)) {
$header['weight'] = $this->t('Weight');
}
return $header;
}
public function buildRow(EntityInterface $entity)
{
$row = parent::buildRow($entity);
if (!empty($this->weightKey)) {
// Override default values to markup elements.
$row['#attributes']['class'][] = 'draggable';
$row['#weight'] = $entity->get($this->weightKey)->value;
// Add weight column.
$row['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for @title', ['@title' => $entity->label()]),
'#title_display' => 'invisible',
'#default_value' => $entity->get($this->weightKey)->value,
'#attributes' => ['class' => ['weight']],
];
}
return $row;
}
public function render()
{
if (!empty($this->weightKey)) {
return $this->formBuilder->getForm($this);
}
return parent::render();
}
public function getFormId()
{
return sprintf('wmoverview_builder_draggable_%s', $this->getDefinition()->getId());
}
public function buildForm(array $form, FormStateInterface $form_state)
{
$form[$this->entitiesKey] = [
'#type' => 'table',
'#header' => $this->buildHeader(),
'#empty' => $this->t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
],
],
];
$this->entities = $this->load();
$delta = 10;
// Change the delta of the weight field if have more than 20 entities.
if (!empty($this->weightKey)) {
$count = count($this->entities);
if ($count > 20) {
$delta = ceil($count / 2);
}
}
foreach ($this->entities as $entity) {
$row = $this->buildRow($entity);
foreach (array_keys($row) as $i) {
if (isset($row[$i]['data'])) {
$row[$i] = $row[$i]['data'];
}
}
if (isset($row['label'])) {
$row['label'] = ['#markup' => $row['label']];
}
if (isset($row['weight'])) {
$row['weight']['#delta'] = $delta;
}
$form[$this->entitiesKey][$this->getRowKeyByEntity($entity)] = $row;
}
uasort($form[$this->entitiesKey], [SortArray::class, 'sortByWeightProperty']);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state)
{
}
public function submitForm(array &$form, FormStateInterface $form_state)
{
foreach ($form_state->getValue($this->entitiesKey) as $id => $value) {
if (!isset($this->entities[$id])) {
continue;
}
$originalWeight = $this->entities[$id]->get($this->weightKey)->value;
if ($originalWeight === $value['weight']) {
continue;
}
$this->entities[$id]->set($this->weightKey, $value['weight']);
$this->entities[$id]->save();
}
}
}