diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetCustomize.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetCustomize.php index e8808ae..299e9f8 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetCustomize.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetCustomize.php @@ -7,70 +7,44 @@ namespace Drupal\shortcut\Form; -use Drupal\Core\Controller\ControllerInterface; -use Drupal\Core\Form\FormInterface; -use Drupal\menu_link\MenuLinkStorageController; -use Drupal\shortcut\Plugin\Core\Entity\Shortcut; +use Drupal\Core\Entity\EntityFormController; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Builds the shortcut set customize form. */ -class SetCustomize implements FormInterface, ControllerInterface { - - /** - * The entity storage controller for menu links. - * - * @var \Drupal\menu_link\MenuLinkStorageController - */ - protected $storageController; - - /** - * Constructs a SetCustomize object. - * - * @param \Drupal\menu_link\MenuLinkStorageController $storage_controller - * The entity manager. - */ - public function __construct(MenuLinkStorageController $storage_controller) { - $this->storageController = $storage_controller; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('plugin.manager.entity')->getStorageController('menu_link') - ); - } +class SetCustomize extends EntityFormController { /** * {@inheritdoc} */ - public function getFormID() { - return 'shortcut_set_customize'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, array &$form_state, Shortcut $shortcut = NULL) { - $form['#shortcut_set_name'] = $shortcut->id(); + public function form(array $form, array &$form_state) { + $form = parent::form($form, $form_state); $form['shortcuts'] = array( '#tree' => TRUE, '#weight' => -20, - 'links' => array(), ); - foreach ($shortcut->links as $link) { + $form['shortcuts']['links'] = array( + '#type' => 'table', + '#header' => array(t('Name'), t('Weight'), t('Operations')), + '#empty' => t('No shortcuts available. @link', array('@link' => l(t('Add a shortcut'), 'admin/config/user-interface/shortcut/' . $this->entity->id() . '/add-link'))), + '#attributes' => array('id' => 'shortcuts'), + '#tabledrag' => array( + array('order', 'sibling', 'shortcut-weight'), + ), + ); + + foreach ($this->entity->links as $link) { $mlid = $link->id(); + $form['shortcuts']['links'][$mlid]['#attributes']['class'][] = 'draggable'; $form['shortcuts']['links'][$mlid]['name']['#markup'] = l($link->link_title, $link->link_path); + $form['shortcuts']['links'][$mlid]['#weight'] = $link->weight; $form['shortcuts']['links'][$mlid]['weight'] = array( '#type' => 'weight', '#title' => t('Weight for @title', array('@title' => $link->link_title)), '#title_display' => 'invisible', - '#delta' => 50, - '#default_value' => $link['weight'], + '#default_value' => $link->weight, '#attributes' => array('class' => array('shortcut-weight')), ); @@ -87,37 +61,35 @@ public function buildForm(array $form, array &$form_state, Shortcut $shortcut = '#links' => $links, ); } - - $form['actions'] = array( - '#type' => 'actions', - '#access' => !empty($shortcut->links), - ); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save changes'), - ); - + // Sort the list so the output is ordered by weight. + uasort($form['shortcuts']['links'], 'element_sort'); return $form; } /** - * {@inheritdoc} + * Overrides Drupal\Core\Entity\EntityFormController::actions(). */ - public function validateForm(array &$form, array &$form_state) { + protected function actions(array $form, array &$form_state) { + // Only includes a Save action for the entity, no direct Delete button. + return array( + 'submit' => array( + '#value' => t('Save changes'), + '#access' => !empty($this->entity->links), + '#submit' => array( + array($this, 'submit'), + array($this, 'save'), + ), + ), + ); } /** * {@inheritdoc} */ - public function submitForm(array &$form, array &$form_state) { - $mlids = array_keys($form_state['values']['shortcuts']['links']); - $menu_links = $this->storageController->load($mlids); - foreach ($form_state['values']['shortcuts']['links'] as $mlid => $data) { - $link = $menu_links[$mlid]; - debug($mlid); - debug($data['weight']); - $link->weight = $data['weight']; - $this->storageController->save($link); + public function save(array $form, array &$form_state) { + foreach ($this->entity->links as $link) { + $link->weight = $form_state['values']['shortcuts']['links'][$link->mlid]['weight']; + $link->save(); } drupal_set_message(t('The shortcut set has been updated.')); } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/Shortcut.php b/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/Shortcut.php index 5b3f17a..3c9bd1c 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/Shortcut.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/Shortcut.php @@ -27,6 +27,7 @@ * "form" = { * "default" = "Drupal\shortcut\ShortcutFormController", * "edit" = "Drupal\shortcut\ShortcutFormController", + * "customize" = "Drupal\shortcut\Form\SetCustomize", * "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm" * } * }, diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutAccessController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutAccessController.php index 2451da3..7aa488e 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutAccessController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutAccessController.php @@ -22,7 +22,6 @@ class ShortcutAccessController extends EntityAccessController { protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) { switch ($operation) { case 'edit': - case 'customize': if (user_access('administer shortcuts', $account)) { return TRUE; } diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index 5976f53..a9f255c 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -180,56 +180,6 @@ function shortcut_set_add() { } /** - * Returns HTML for a shortcut set customization form. - * - * @param $variables - * An associative array containing: - * - form: A render element representing the form. - * - * @see shortcut_set_customize() - * @ingroup themeable - */ -function theme_shortcut_set_customize($variables) { - $form = $variables['form']; - - // Do not add any rows to the table if there are no shortcuts to display. - $statuses = empty($shortcuts_by_status['enabled']) && empty($shortcuts_by_status['disabled']) ? array() : array_keys($shortcuts_by_status); - - $rows = array(); - drupal_add_tabledrag('shortcuts', 'order', 'sibling', 'shortcut-weight'); - foreach (element_children($form['shortcuts']['links']) as $key) { - $shortcut = &$form['shortcuts']['links'][$key]; - $row = array(); - $row[] = drupal_render($shortcut['name']); - $row[] = drupal_render($shortcut['weight']); - $row[] = drupal_render($shortcut['operations']); - $rows[] = array( - 'data' => $row, - 'class' => array('draggable'), - ); - } - - $header = array(t('Name'), t('Weight'), t('Operations')); - - $table = array( - '#theme' => 'table', - '#header' => $header, - '#rows' => $rows, - '#empty' => t('No shortcuts available. Add a shortcut.', array( - '@link' => url('admin/config/user-interface/shortcut/manage/' . $form['#shortcut_set_name'] . '/add-link'), - )), - '#attributes' => array( - 'id' => 'shortcuts', - ), - ); - - $output = drupal_render($table); - $output .= drupal_render($form['actions']); - $output = drupal_render_children($form) . $output; - return $output; -} - -/** * Form callback: builds the form for adding a new shortcut link. * * @param $form diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 7cba21b..f658b24 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -168,18 +168,6 @@ function shortcut_admin_paths() { } /** - * Implements hook_theme(). - */ -function shortcut_theme() { - return array( - 'shortcut_set_customize' => array( - 'render element' => 'form', - 'file' => 'shortcut.admin.inc', - ), - ); -} - -/** * Access callback for editing a shortcut set. * * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index cb0ad1f..107beb3 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -29,6 +29,6 @@ shortcut_set_edit: shortcut_set_customize: pattern: '/admin/config/user-interface/shortcut/manage/{shortcut}' defaults: - _form: 'Drupal\shortcut\Form\SetCustomize' + _entity_form: 'shortcut.customize' requirements: - _entity_access: 'shortcut.customize' + _entity_access: 'shortcut.edit'