diff --git a/core/lib/Drupal/Core/Config/Entity/DraggableListController.php b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php new file mode 100644 index 0000000000000000000000000000000000000000..5370be5d4b6a960ed1dd2cd9f7efef3f7c756be5 --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php @@ -0,0 +1,147 @@ +<?php + +/** + * @file + * Contains Drupal\Core\Config\Entity\DraggableListController. + */ + +namespace Drupal\Core\Config\Entity; + +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageControllerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Form\FormInterface; + +/** + * Provides a list controller for draggable configuration entities. + */ +abstract class DraggableListController extends ConfigEntityListController implements FormInterface { + + /** + * The key to use for the form element containing the entities. + * + * @var string + */ + protected $entitiesKey = 'entities'; + + /** + * The entities being listed. + * + * @var \Drupal\Core\Entity\EntityInterface[] + */ + protected $entities = array(); + + /** + * Name of the entity's weight field or FALSE if no field is provided. + * + * @var string|bool + */ + protected $weightKey = FALSE; + + /** + * {@inheritdoc} + */ + 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']; + } + } + + /** + * {@inheritdoc} + */ + public function buildHeader() { + $header = array(); + if (!empty($this->weightKey)) { + $header['weight'] = t('Weight'); + } + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + 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')), + ); + } + return $row + parent::buildRow($entity); + } + + /** + * {@inheritdoc} + */ + public function render() { + if (!empty($this->weightKey)) { + return drupal_get_form($this); + } + return parent::render(); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { + $form[$this->entitiesKey] = array( + '#type' => 'table', + '#header' => $this->buildHeader(), + '#empty' => t('There is no @label yet.', array('@label' => $this->entityInfo['label'])), + '#tabledrag' => array( + array('order', 'sibling', 'weight'), + ), + ); + + $this->entities = $this->load(); + foreach ($this->entities as $entity) { + $row = $this->buildRow($entity); + if (isset($row['label'])) { + $row['label'] = array('#markup' => $row['label']); + } + $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; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + // No validation. + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + foreach ($form_state['values'][$this->entitiesKey] as $id => $value) { + if (isset($this->entities[$id]) && $this->entities[$id]->get($this->weightKey) != $value['weight']) { + // Save entity only when its weight was changed. + $this->entities[$id]->set($this->weightKey, $value['weight']); + $this->entities[$id]->save(); + } + } + } + +} diff --git a/core/modules/filter/lib/Drupal/filter/Entity/FilterFormat.php b/core/modules/filter/lib/Drupal/filter/Entity/FilterFormat.php index cc7a3c7db7c1ab80ff0d558fe38f2d22b249f586..7bc107d54970a219bfd96c20443ffbaf38c1496a 100644 --- a/core/modules/filter/lib/Drupal/filter/Entity/FilterFormat.php +++ b/core/modules/filter/lib/Drupal/filter/Entity/FilterFormat.php @@ -36,6 +36,7 @@ * "id" = "format", * "label" = "name", * "uuid" = "uuid", + * "weight" = "weight", * "status" = "status" * } * ) diff --git a/core/modules/filter/lib/Drupal/filter/FilterFormatListController.php b/core/modules/filter/lib/Drupal/filter/FilterFormatListController.php index af08b9ec8f3f121a3796be497741c02003c63f31..098aa7ddf378d142e68872011b28b0d0a2d401ae 100644 --- a/core/modules/filter/lib/Drupal/filter/FilterFormatListController.php +++ b/core/modules/filter/lib/Drupal/filter/FilterFormatListController.php @@ -9,17 +9,22 @@ use Drupal\Component\Utility\String; use Drupal\Core\Config\ConfigFactory; -use Drupal\Core\Config\Entity\ConfigEntityListController; +use Drupal\Core\Config\Entity\DraggableListController; +use Drupal\Core\Entity\EntityControllerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Form\FormInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines the filter format list controller. */ -class FilterFormatListController extends ConfigEntityListController implements FormInterface { +class FilterFormatListController extends DraggableListController implements EntityControllerInterface { + + /** + * {@inheritdoc} + */ + protected $entitiesKey = 'formats'; /** * The config factory service. @@ -68,13 +73,6 @@ public function getFormID() { return 'filter_admin_overview'; } - /** - * {@inheritdoc} - */ - public function render() { - return drupal_get_form($this); - } - /** * {@inheritdoc} */ @@ -89,9 +87,8 @@ public function load() { * {@inheritdoc} */ public function buildHeader() { - $header['name'] = t('Name'); + $header['label'] = t('Name'); $header['roles'] = t('Roles'); - $header['weight'] = t('Weight'); return $header + parent::buildHeader(); } @@ -99,14 +96,10 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['#attributes']['class'][] = 'draggable'; - $row['#weight'] = $entity->get('weight'); - // Check whether this is the fallback text format. This format is available // to all roles and cannot be disabled via the admin interface. - $row['#is_fallback'] = $entity->isFallbackFormat(); - if ($row['#is_fallback']) { - $row['name'] = array('#markup' => String::placeholder($entity->label())); + if ($entity->isFallbackFormat()) { + $row['label'] = String::placeholder($entity->label()); $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice'); if ($fallback_choice) { @@ -117,20 +110,12 @@ public function buildRow(EntityInterface $entity) { } } else { - $row['name'] = array('#markup' => $this->getLabel($entity)); + $row['label'] = $this->getLabel($entity); $roles = array_map('\Drupal\Component\Utility\String::checkPlain', filter_get_roles_by_format($entity)); $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format'); } - $row['roles'] = array('#markup' => $roles_markup); - - $row['weight'] = array( - '#type' => 'weight', - '#title' => t('Weight for @title', array('@title' => $entity->label())), - '#title_display' => 'invisible', - '#default_value' => $entity->get('weight'), - '#attributes' => array('class' => array('text-format-order-weight')), - ); + $row['roles'] = !empty($this->weightKey) ? array('#markup' => $roles_markup) : $roles_markup; return $row + parent::buildRow($entity); } @@ -159,47 +144,15 @@ public function getOperations(EntityInterface $entity) { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { - $form['#tree'] = TRUE; - $form['formats'] = array( - '#type' => 'table', - '#header' => $this->buildHeader(), - '#empty' => t('There is no @label yet.', array('@label' => $this->entityInfo['label'])), - '#tabledrag' => array( - array('order', 'sibling', 'text-format-order-weight'), - ), - ); - foreach ($this->load() as $entity) { - $form['formats'][$entity->id()] = $this->buildRow($entity); - } - $form['actions']['#type'] = 'actions'; - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save changes'), - '#button_type' => 'primary', - ); + $form = parent::buildForm($form, $form_state); + $form['actions']['submit']['#value'] = t('Save changes'); return $form; } - - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, array &$form_state) { - } - /** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - $values = $form_state['values']['formats']; - - $entities = $this->storage->loadMultiple(array_keys($values)); - foreach ($values as $id => $value) { - if (isset($entities[$id]) && $value['weight'] != $entities[$id]->get('weight')) { - // Update changed weight. - $entities[$id]->set('weight', $value['weight']); - $entities[$id]->save(); - } - } + parent::submitForm($form, $form_state); filter_formats_reset(); drupal_set_message(t('The text format ordering has been saved.')); diff --git a/core/modules/language/lib/Drupal/language/Entity/Language.php b/core/modules/language/lib/Drupal/language/Entity/Language.php index 15c6a67f4f44b3649b91758b197072b63c3bc572..eb958c4415c17fefd87080cd42a87ddf90c47762 100644 --- a/core/modules/language/lib/Drupal/language/Entity/Language.php +++ b/core/modules/language/lib/Drupal/language/Entity/Language.php @@ -30,6 +30,7 @@ * entity_keys = { * "id" = "id", * "label" = "label", + * "weight" = "weight", * "uuid" = "uuid" * } * ) diff --git a/core/modules/language/lib/Drupal/language/LanguageListController.php b/core/modules/language/lib/Drupal/language/LanguageListController.php index 8abeb8b37e582e5f08c6d5e981e12a99eb875374..d33b9f4b72c342552bba1d5d73f4788d7e0500af 100644 --- a/core/modules/language/lib/Drupal/language/LanguageListController.php +++ b/core/modules/language/lib/Drupal/language/LanguageListController.php @@ -6,14 +6,18 @@ namespace Drupal\language; -use Drupal\Core\Config\Entity\ConfigEntityListController; +use Drupal\Core\Config\Entity\DraggableListController; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Form\FormInterface; /** * User interface for the language overview screen. */ -class LanguageListController extends ConfigEntityListController implements FormInterface { +class LanguageListController extends DraggableListController { + + /** + * {@inheritdoc} + */ + protected $entitiesKey = 'languages'; /** * {@inheritdoc} @@ -61,7 +65,6 @@ public function getOperations(EntityInterface $entity) { */ public function buildHeader() { $header['label'] = t('Name'); - $header['weight'] = t('Weight'); return $header + parent::buildHeader(); } @@ -69,23 +72,7 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['#attributes']['class'][] = 'draggable'; - - $row['label'] = array( - '#markup' => check_plain($entity->get('label')), - ); - - $row['#weight'] = $entity->get('weight'); - // Add weight column. - $row['weight'] = array( - '#type' => 'weight', - '#title' => t('Weight for @title', array('@title' => $entity->label())), - '#title_display' => 'invisible', - '#default_value' => $entity->get('weight'), - '#attributes' => array('class' => array('weight')), - '#delta' => 30, - ); - + $row['label'] = $this->getLabel($entity); return $row + parent::buildRow($entity); } @@ -93,52 +80,17 @@ public function buildRow(EntityInterface $entity) { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { - $languages = $this->load(); - - $form['languages'] = array( - '#languages' => $languages, - '#type' => 'table', - '#header' => $this->buildHeader(), - '#empty' => t('There are no languages', array('@label' => $this->entityInfo['label'])), - '#tabledrag' => array( - array('order', 'sibling', 'weight'), - ), - ); - - foreach ($languages as $entity) { - $form['languages'][$entity->id()] = $this->buildRow($entity); - } - - $form['actions']['#type'] = 'actions'; - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save configuration'), - ); - + $form = parent::buildForm($form, $form_state); + $form[$this->entitiesKey]['#languages'] = $this->entities; + $form['actions']['submit']['#value'] = t('Save configuration'); return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, array &$form_state) { - // No validation. - } - /** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - $languages = $form_state['values']['languages']; - - $language_entities = $this->load(); - foreach ($languages as $langcode => $language) { - if (isset($language_entities[$langcode]) && $language['weight'] != $language_entities[$langcode]->get('weight')) { - // Update changed weight. - $language_entities[$langcode]->set('weight', $language['weight']); - $language_entities[$langcode]->save(); - } - } + parent::submitForm($form, $form_state); // Kill the static cache in language_list(). drupal_static_reset('language_list'); @@ -149,10 +101,4 @@ public function submitForm(array &$form, array &$form_state) { drupal_set_message(t('Configuration saved.')); } - /** - * {@inheritdoc} - */ - public function render() { - return drupal_get_form($this); - } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php index 005703622550e83a7e11a644c495a53263a0efc5..8c20176214d9747a3744e3a0e3f9d9aac2ccb5f3 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php @@ -35,6 +35,7 @@ * entity_keys = { * "id" = "vid", * "label" = "name", + * "weight" = "weight", * "uuid" = "uuid" * } * ) diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php index a5480c5f3bc57b608ca6cc1fd231809ca0fc76ee..3ce091b0d09664ebed4d08370fadd55a57acb030 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php @@ -7,14 +7,18 @@ namespace Drupal\taxonomy; -use Drupal\Core\Config\Entity\ConfigEntityListController; +use Drupal\Core\Config\Entity\DraggableListController; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Form\FormInterface; /** * Provides a listing of vocabularies. */ -class VocabularyListController extends ConfigEntityListController implements FormInterface { +class VocabularyListController extends DraggableListController { + + /** + * {@inheritdoc} + */ + protected $entitiesKey = 'vocabularies'; /** * {@inheritdoc} @@ -65,21 +69,7 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - // Override default values to markup elements. - $row['#attributes']['class'][] = 'draggable'; - - $row['label'] = array( - '#markup' => $this->getLabel($entity), - ); - $row['#weight'] = $entity->get('weight'); - // Add weight column. - $row['weight'] = array( - '#type' => 'weight', - '#title' => t('Weight for @title', array('@title' => $entity->label())), - '#title_display' => 'invisible', - '#default_value' => $entity->get('weight'), - '#attributes' => array('class' => array('weight')), - ); + $row['label'] = $this->getLabel($entity); return $row + parent::buildRow($entity); } @@ -88,22 +78,13 @@ public function buildRow(EntityInterface $entity) { */ public function render() { $entities = $this->load(); - if (count($entities) > 1) { - // Creates a form for manipulating vocabulary weights if more then one - // vocabulary exists. - return drupal_get_form($this); - } - $build = array( - '#theme' => 'table', - '#header' => $this->buildHeader(), - '#rows' => array(), - '#empty' => t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))), - ); - unset($build['#header']['weight']); - foreach ($entities as $entity) { - $row['label'] = $this->getLabel($entity); - $build['#rows'][$entity->id()] = $row + parent::buildRow($entity); + // If there are not multiple vocabularies, disable dragging by unsetting the + // weight key. + if (count($entities) <= 1) { + unset($this->weightKey); } + $build = parent::render(); + $build['#empty'] = t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))); return $build; } @@ -111,52 +92,18 @@ public function render() { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { - $form['vocabularies'] = array( - '#type' => 'table', - '#header' => $this->buildHeader(), - '#tabledrag' => array( - array('order', 'sibling', 'weight'), - ), - '#attributes' => array( - 'id' => 'taxonomy', - ), - ); - - foreach ($this->load() as $entity) { - $form['vocabularies'][$entity->id()] = $this->buildRow($entity); - } - - $form['actions']['#type'] = 'actions'; - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - '#button_type' => 'primary', - ); + $form = parent::buildForm($form, $form_state); + $form['vocabularies']['#attributes'] = array('id' => 'taxonomy'); + $form['actions']['submit']['#value'] = t('Save'); return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, array &$form_state) { - // No validation. - } - /** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - $vocabularies = $form_state['values']['vocabularies']; - - $entities = entity_load_multiple($this->entityType, array_keys($vocabularies)); - foreach ($vocabularies as $id => $value) { - if (isset($entities[$id]) && $value['weight'] != $entities[$id]->get('weight')) { - // Update changed weight. - $entities[$id]->set('weight', $value['weight']); - $entities[$id]->save(); - } - } + parent::submitForm($form, $form_state); drupal_set_message(t('The configuration options have been saved.')); } diff --git a/core/modules/user/lib/Drupal/user/Entity/Role.php b/core/modules/user/lib/Drupal/user/Entity/Role.php index 2703b8b26d42b354695ee6cc400b73ab78103503..8529e87769a9ad47810078bc2e7c3e3c7e90df5a 100644 --- a/core/modules/user/lib/Drupal/user/Entity/Role.php +++ b/core/modules/user/lib/Drupal/user/Entity/Role.php @@ -33,6 +33,7 @@ * entity_keys = { * "id" = "id", * "uuid" = "uuid", + * "weight" = "weight", * "label" = "label" * } * ) diff --git a/core/modules/user/lib/Drupal/user/RoleListController.php b/core/modules/user/lib/Drupal/user/RoleListController.php index ce9594f5e0373273f45b0fff4854ace69aa1f39e..3e95aae56c1d4a9955c15dd5b90d2044e422627b 100644 --- a/core/modules/user/lib/Drupal/user/RoleListController.php +++ b/core/modules/user/lib/Drupal/user/RoleListController.php @@ -7,14 +7,13 @@ namespace Drupal\user; -use Drupal\Core\Config\Entity\ConfigEntityListController; +use Drupal\Core\Config\Entity\DraggableListController; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Form\FormInterface; /** * Provides a listing of user roles. */ -class RoleListController extends ConfigEntityListController implements FormInterface { +class RoleListController extends DraggableListController { /** * {@inheritdoc} @@ -28,10 +27,17 @@ public function getFormID() { */ public function buildHeader() { $header['label'] = t('Name'); - $header['weight'] = t('Weight'); return $header + parent::buildHeader(); } + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $this->getLabel($entity); + return $row + parent::buildRow($entity); + } + /** * {@inheritdoc} */ @@ -50,84 +56,13 @@ public function getOperations(EntityInterface $entity) { return $operations; } - /** - * {@inheritdoc} - */ - public function buildRow(EntityInterface $entity) { - // Override default values to markup elements. - $row['#attributes']['class'][] = 'draggable'; - - $row['label'] = array( - '#markup' => $this->getLabel($entity), - ); - $row['#weight'] = $entity->get('weight'); - // Add weight column. - $row['weight'] = array( - '#type' => 'weight', - '#title' => t('Weight for @title', array('@title' => $entity->label())), - '#title_display' => 'invisible', - '#default_value' => $entity->get('weight'), - '#attributes' => array('class' => array('weight')), - ); - return $row + parent::buildRow($entity); - } - - /** - * {@inheritdoc} - */ - public function render() { - return drupal_get_form($this); - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, array &$form_state) { - $form['entities'] = array( - '#type' => 'table', - '#header' => $this->buildHeader(), - '#empty' => t('There is no @label yet.', array('@label' => $this->entityInfo['label'])), - '#tabledrag' => array( - array('order', 'sibling', 'weight'), - ), - ); - - foreach ($this->load() as $entity) { - $form['entities'][$entity->id()] = $this->buildRow($entity); - } - - $form['actions']['#type'] = 'actions'; - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save order'), - '#button_type' => 'primary', - ); - - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, array &$form_state) { - // No validation. - } - /** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - $values = $form_state['values']['entities']; - - $entities = entity_load_multiple($this->entityType, array_keys($values)); - foreach ($values as $id => $value) { - if (isset($entities[$id]) && $value['weight'] != $entities[$id]->get('weight')) { - // Update changed weight. - $entities[$id]->set('weight', $value['weight']); - $entities[$id]->save(); - } - } + parent::submitForm($form, $form_state); drupal_set_message(t('The role settings have been updated.')); } + }