.../Core/Config/Entity/DraggableListController.php | 47 ++++++++++------------ 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/core/lib/Drupal/Core/Config/Entity/DraggableListController.php b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php index 5370be5..5f3dd13 100644 --- a/core/lib/Drupal/Core/Config/Entity/DraggableListController.php +++ b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php @@ -14,6 +14,9 @@ /** * Provides a list controller for draggable configuration entities. + * + * Using this class as a list controller requires the entity type to define a + * weight key. */ abstract class DraggableListController extends ConfigEntityListController implements FormInterface { @@ -44,21 +47,20 @@ public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler) { parent::__construct($entity_type, $entity_info, $storage, $module_handler); - // Check if the entity type supports weighting. - if (!empty($this->entityInfo['entity_keys']['weight'])) { - $this->weightKey = $this->entityInfo['entity_keys']['weight']; + // An entity type that does not support weighting should not use this list + // controller. + if (empty($this->entityInfo['entity_keys']['weight'])) { + throw new \Exception("Entity type '$entity_type' uses DraggableListController but does not define a weight key."); } + + $this->weightKey = $this->entityInfo['entity_keys']['weight']; } /** * {@inheritdoc} */ public function buildHeader() { - $header = array(); - if (!empty($this->weightKey)) { - $header['weight'] = t('Weight'); - } - return $header + parent::buildHeader(); + return array('weight' => t('Weight')) + parent::buildHeader(); } /** @@ -66,19 +68,17 @@ public function buildHeader() { */ public function buildRow(EntityInterface $entity) { $row = array(); - if (!empty($this->weightKey)) { - // Override default values to markup elements. - $row['#attributes']['class'][] = 'draggable'; - $row['#weight'] = $entity->get($this->weightKey); - // Add weight column. - $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')), - ); - } + // Override default values to markup elements. + $row['#attributes']['class'][] = 'draggable'; + $row['#weight'] = $entity->get($this->weightKey); + // Add weight column. + $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); } @@ -86,10 +86,7 @@ public function buildRow(EntityInterface $entity) { * {@inheritdoc} */ public function render() { - if (!empty($this->weightKey)) { - return drupal_get_form($this); - } - return parent::render(); + return drupal_get_form($this); } /**