View source
<?php
namespace Drupal\Core\Config\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
abstract class DraggableListBuilder extends ConfigEntityListBuilder implements FormInterface {
protected $entitiesKey = 'entities';
protected $entities = array();
protected $weightKey = FALSE;
protected $formBuilder;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
parent::__construct($entity_type, $storage);
if ($this->entityType
->hasKey('weight')) {
$this->weightKey = $this->entityType
->getKey('weight');
}
}
public function buildHeader() {
$header = array();
if (!empty($this->weightKey)) {
$header['weight'] = t('Weight');
}
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row = array();
if (!empty($this->weightKey)) {
$row['#attributes']['class'][] = 'draggable';
$row['#weight'] = $entity
->get($this->weightKey);
$row['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array(
'@title' => $entity
->label(),
)),
'#title_display' => 'invisible',
'#default_value' => $entity
->get($this->weightKey),
'#attributes' => array(
'class' => array(
'weight',
),
),
);
}
return $row + parent::buildRow($entity);
}
public function render() {
if (!empty($this->weightKey)) {
return $this
->formBuilder()
->getForm($this);
}
return parent::render();
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form[$this->entitiesKey] = array(
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#empty' => t('There is no @label yet.', array(
'@label' => $this->entityType
->getLabel(),
)),
'#tabledrag' => array(
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
),
),
);
$this->entities = $this
->load();
$delta = 10;
if (!empty($this->weightKey)) {
$count = count($this->entities);
if ($count > 20) {
$delta = ceil($count / 2);
}
}
foreach ($this->entities as $entity) {
$row = $this
->buildRow($entity);
if (isset($row['label'])) {
$row['label'] = array(
'#markup' => $row['label'],
);
}
if (isset($row['weight'])) {
$row['weight']['#delta'] = $delta;
}
$form[$this->entitiesKey][$entity
->id()] = $row;
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save order'),
'#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]) && $this->entities[$id]
->get($this->weightKey) != $value['weight']) {
$this->entities[$id]
->set($this->weightKey, $value['weight']);
$this->entities[$id]
->save();
}
}
}
protected function formBuilder() {
if (!$this->formBuilder) {
$this->formBuilder = \Drupal::formBuilder();
}
return $this->formBuilder;
}
}