Commit 8469e98b authored by alexpott's avatar alexpott

Issue #1978956 by michaellenahan, naxoc, dawehner, vijaycs85, tim.plunkett:...

Issue #1978956 by michaellenahan, naxoc, dawehner, vijaycs85, tim.plunkett: Convert shortcut_set_customize() to a Controller.
parent d625f2c3
<?php
/**
* @file
* Contains \Drupal\shortcut\Form\SetCustomize.
*/
namespace Drupal\shortcut\Form;
use Drupal\Core\Entity\EntityFormController;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Builds the shortcut set customize form.
*/
class SetCustomize extends EntityFormController {
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form = parent::form($form, $form_state);
$form['shortcuts'] = array(
'#tree' => TRUE,
'#weight' => -20,
);
$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',
'#default_value' => $link->weight,
'#attributes' => array('class' => array('shortcut-weight')),
);
$links['edit'] = array(
'title' => t('Edit'),
'href' => "admin/config/user-interface/shortcut/link/$mlid",
);
$links['delete'] = array(
'title' => t('Delete'),
'href' => "admin/config/user-interface/shortcut/link/$mlid/delete",
);
$form['shortcuts']['links'][$mlid]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
// Sort the list so the output is ordered by weight.
uasort($form['shortcuts']['links'], 'element_sort');
return $form;
}
/**
* {@inheritdoc}
*/
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 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.'));
}
}
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
* "form" = { * "form" = {
* "default" = "Drupal\shortcut\ShortcutFormController", * "default" = "Drupal\shortcut\ShortcutFormController",
* "edit" = "Drupal\shortcut\ShortcutFormController", * "edit" = "Drupal\shortcut\ShortcutFormController",
* "customize" = "Drupal\shortcut\Form\SetCustomize",
* "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm" * "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm"
* } * }
* }, * },
......
...@@ -179,130 +179,6 @@ function shortcut_set_add() { ...@@ -179,130 +179,6 @@ function shortcut_set_add() {
return Drupal::entityManager()->getForm($entity); return Drupal::entityManager()->getForm($entity);
} }
/**
* Form callback: builds the form for customizing shortcut sets.
*
* @param $form
* An associative array containing the structure of the form.
* @param $form_state
* An associative array containing the current state of the form.
* @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut
* An object representing the shortcut set which is being edited.
*
* @return
* An array representing the form definition.
*
* @ingroup forms
* @see shortcut_set_customize_submit()
*/
function shortcut_set_customize($form, &$form_state, $shortcut_set) {
$form['#shortcut_set_name'] = $shortcut_set->id();
$form['shortcuts'] = array(
'#tree' => TRUE,
'#weight' => -20,
'links' => array(),
);
foreach ($shortcut_set->links as $uuid => $link) {
$mlid = $link['mlid'];
$form['shortcuts']['links'][$mlid]['name']['#markup'] = l($link['link_title'], $link['link_path']);
$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'],
'#attributes' => array('class' => array('shortcut-weight')),
);
$links['edit'] = array(
'title' => t('Edit'),
'href' => "admin/config/user-interface/shortcut/link/$mlid",
);
$links['delete'] = array(
'title' => t('Delete'),
'href' => "admin/config/user-interface/shortcut/link/$mlid/delete",
);
$form['shortcuts']['links'][$mlid]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
$form['actions'] = array(
'#type' => 'actions',
'#access' => !empty($shortcut_set->links),
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);
return $form;
}
/**
* Submit handler for shortcut_set_customize().
*/
function shortcut_set_customize_submit($form, &$form_state) {
foreach ($form_state['values']['shortcuts']['links'] as $mlid => $data) {
$link = menu_link_load($mlid);
$link['weight'] = $data['weight'];
menu_link_save($link);
}
drupal_set_message(t('The shortcut set has been updated.'));
}
/**
* 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. <a href="@link">Add a shortcut</a>.', 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. * Form callback: builds the form for adding a new shortcut link.
* *
......
...@@ -98,13 +98,9 @@ function shortcut_menu() { ...@@ -98,13 +98,9 @@ function shortcut_menu() {
); );
$items['admin/config/user-interface/shortcut/manage/%shortcut_set'] = array( $items['admin/config/user-interface/shortcut/manage/%shortcut_set'] = array(
'title' => 'Edit shortcuts', 'title' => 'Edit shortcuts',
'page callback' => 'drupal_get_form', 'route_name' => 'shortcut_set_customize',
'page arguments' => array('shortcut_set_customize', 5),
'title callback' => 'entity_page_label', 'title callback' => 'entity_page_label',
'title arguments' => array(5), 'title arguments' => array(5),
'access callback' => 'shortcut_set_edit_access',
'access arguments' => array(5),
'file' => 'shortcut.admin.inc',
); );
$items['admin/config/user-interface/shortcut/manage/%shortcut_set/links'] = array( $items['admin/config/user-interface/shortcut/manage/%shortcut_set/links'] = array(
'title' => 'List links', 'title' => 'List links',
...@@ -164,18 +160,6 @@ function shortcut_admin_paths() { ...@@ -164,18 +160,6 @@ function shortcut_admin_paths() {
return $paths; return $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. * Access callback for editing a shortcut set.
* *
......
...@@ -32,3 +32,10 @@ shortcut_link_add_inline: ...@@ -32,3 +32,10 @@ shortcut_link_add_inline:
_controller: 'Drupal\shortcut\Controller\ShortcutController::addShortcutLinkInline' _controller: 'Drupal\shortcut\Controller\ShortcutController::addShortcutLinkInline'
requirements: requirements:
_entity_access: 'shortcut.update' _entity_access: 'shortcut.update'
shortcut_set_customize:
pattern: '/admin/config/user-interface/shortcut/manage/{shortcut}'
defaults:
_entity_form: 'shortcut.customize'
requirements:
_entity_access: 'shortcut.update'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment