Commit d556b325 authored by alexpott's avatar alexpott
Browse files

Issue #1968020 by damiankloip, dawehner: Convert ReorderDisplays to use table...

Issue #1968020 by damiankloip, dawehner: Convert ReorderDisplays to use table rendering and remove theme function.
parent b4eaea27
......@@ -35,57 +35,103 @@ public function buildForm(array $form, array &$form_state) {
$view = $form_state['view'];
$display_id = $form_state['display_id'];
$form['view'] = array('#type' => 'value', '#value' => $view);
$form['#title'] = t('Reorder displays');
$form['#section'] = 'reorder';
$form['#action'] = url('admin/structure/views/nojs/reorder-displays/' . $view->id() . '/' . $display_id);
$form['view'] = array(
'#type' => 'value',
'#value' => $view
);
$form['#tree'] = TRUE;
$displays = $view->get('display');
$count = count($displays);
$count = count($view->get('display'));
// Sort the displays.
uasort($displays, function ($display1, $display2) {
if ($display1['position'] != $display2['position']) {
return $display1['position'] < $display2['position'] ? -1 : 1;
}
return 0;
});
$form['displays'] = array(
'#type' => 'table',
'#id' => 'reorder-displays',
'#header' => array(t('Display'), t('Weight'), t('Remove')),
'#empty' => t('No displays available.'),
'#tabledrag' => array(
array('order', 'sibling', 'weight'),
),
'#tree' => TRUE,
'#prefix' => '<div class="scroll">',
'#suffix' => '</div>',
);
$displays = $view->get('display');
foreach ($displays as $display) {
$form[$display['id']] = array(
'title' => array('#markup' => $display['display_title']),
'weight' => array(
'#type' => 'weight',
'#value' => $display['position'],
'#delta' => $count,
'#title' => t('Weight for @display', array('@display' => $display['display_title'])),
'#title_display' => 'invisible',
),
'#tree' => TRUE,
foreach ($displays as $id => $display) {
$form['displays'][$id] = array(
'#display' => $display,
'removed' => array(
'#type' => 'checkbox',
'#id' => 'display-removed-' . $display['id'],
'#attributes' => array('class' => array('views-remove-checkbox')),
'#default_value' => isset($display['deleted']),
'#attributes' => array(
'id' => 'display-row-' . $id,
),
'#weight' => $display['position'],
);
if (isset($display['deleted']) && $display['deleted']) {
$form[$display['id']]['deleted'] = array('#type' => 'value', '#value' => TRUE);
}
if ($display['id'] === 'default') {
unset($form[$display['id']]['weight']);
unset($form[$display['id']]['removed']);
// Only make row draggable if it's not the default display.
if ($id !== 'default') {
$form['displays'][$id]['#attributes']['class'][] = 'draggable';
}
}
$form['displays'][$id]['title'] = array(
'#markup' => $display['display_title'],
);
$form['#title'] = t('Displays Reorder');
$form['#section'] = 'reorder';
$form['displays'][$id]['weight'] = array(
'#type' => 'weight',
'#value' => $display['position'],
'#delta' => $count,
'#title' => t('Weight for @display', array('@display' => $display['display_title'])),
'#title_display' => 'invisible',
'#attributes' => array(
'class' => array('weight'),
),
);
// Add javascript settings that will be added via $.extend for tabledragging
$form['#js']['tableDrag']['reorder-displays']['weight'][0] = array(
'target' => 'weight',
'source' => NULL,
'relationship' => 'sibling',
'action' => 'order',
'hidden' => TRUE,
'limit' => 0,
);
$form['displays'][$id]['removed'] = array(
'checkbox' => array(
'#type' => 'checkbox',
'#id' => 'display-removed-' . $id,
'#attributes' => array(
'class' => array('views-remove-checkbox'),
),
'#default_value' => !empty($display['deleted']),
),
'link' => array(
'#type' => 'link',
'#title' => '<span>' . t('Remove') . '</span>',
'#href' => 'javascript:void()',
'#options' => array(
'html' => TRUE,
),
'#attributes' => array(
'id' => 'display-remove-link-' . $id,
'class' => array('views-button-remove', 'display-remove-link'),
'alt' => t('Remove this display'),
'title' => t('Remove this display'),
),
),
'#access' => ($id !== 'default'),
);
$form['#action'] = url('admin/structure/views/nojs/reorder-displays/' . $view->id() . '/' . $display_id);
if (!empty($display['deleted'])) {
$form['displays'][$id]['deleted'] = array(
'#type' => 'value',
'#value' => TRUE,
);
$form['displays'][$id]['#attributes']['class'][] = 'element-hidden';
}
}
$view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
......@@ -97,42 +143,46 @@ public function buildForm(array $form, array &$form_state) {
*/
public function submitForm(array &$form, array &$form_state) {
$view = $form_state['view'];
foreach ($form_state['input'] as $display => $info) {
// add each value that is a field with a weight to our list, but only if
$order = array();
foreach ($form_state['input']['displays'] as $display => $info) {
// Add each value that is a field with a weight to our list, but only if
// it has had its 'removed' checkbox checked.
if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
$order[$display] = $info['weight'];
}
}
// Sort the order array
// Sort the order array.
asort($order);
// Fixing up positions
// Remove the default display from ordering.
unset($order['default']);
// Increment up positions.
$position = 1;
foreach (array_keys($order) as $display) {
$order[$display] = $position++;
}
// Setting up position and removing deleted displays
// Setting up position and removing deleted displays.
$displays = $view->get('display');
foreach ($displays as $display_id => $display) {
// Don't touch the default !!!
foreach ($displays as $display_id => &$display) {
// Don't touch the default.
if ($display_id === 'default') {
$displays[$display_id]['position'] = 0;
$display['position'] = 0;
continue;
}
if (isset($order[$display_id])) {
$displays[$display_id]['position'] = $order[$display_id];
$display['position'] = $order[$display_id];
}
else {
$displays[$display_id]['deleted'] = TRUE;
$display['deleted'] = TRUE;
}
}
$view->set('display', $displays);
// Store in cache
// Store in cache.
$view->cacheSet();
$form_state['redirect'] = array('admin/structure/views/view/' . $view->id() . '/edit', array('fragment' => 'views-tab-default'));
}
......
......@@ -127,8 +127,8 @@ public function testReorderDisplay() {
// Put the block display in front of the page display.
$edit = array(
'page_1[weight]' => 2,
'block_1[weight]' => 1
'displays[page_1][weight]' => 2,
'displays[block_1][weight]' => 1
);
$this->drupalPost(NULL, $edit, t('Apply'));
$this->drupalPost(NULL, array(), t('Save'));
......
......@@ -150,12 +150,6 @@ function views_ui_theme() {
'file' => 'views_ui.theme.inc',
),
// Reordering displays.
'views_ui_reorder_displays_form' => array(
'render element' => 'form',
'file' => 'views_ui.theme.inc',
),
// On behalf of a plugin
'views_ui_style_plugin_table' => array(
'render element' => 'form',
......
......@@ -218,64 +218,6 @@ function theme_views_ui_build_group_filter_form($variables) {
return $output . $render_form . $table . $more;
}
/**
* Turn the reorder form into a proper table
*/
function theme_views_ui_reorder_displays_form($vars) {
$form = $vars['form'];
$rows = array();
foreach (element_children($form) as $key) {
if (isset($form[$key]['#display'])) {
$display = &$form[$key];
$row = array();
$row[] = drupal_render($display['title']);
$form[$key]['weight']['#attributes']['class'] = array('weight');
$row[] = drupal_render($form[$key]['weight']);
if (isset($display['removed'])) {
$row[] = drupal_render($form[$key]['removed']) .
l('<span>' . t('Remove') . '</span>',
'javascript:void()',
array(
'attributes' => array(
'id' => 'display-remove-link-' . $key,
'class' => array('views-button-remove display-remove-link'),
'alt' => t('Remove this display'),
'title' => t('Remove this display')),
'html' => TRUE));
}
else {
$row[] = '';
}
$class = array();
$styles = array();
if (isset($form[$key]['weight']['#type'])) {
$class[] = 'draggable';
}
if (isset($form[$key]['deleted']['#value']) && $form[$key]['deleted']['#value']) {
$styles[] = 'display: none;';
}
$rows[] = array('data' => $row, 'class' => $class, 'id' => 'display-row-' . $key, 'style' => $styles);
}
}
$header = array(t('Display'), t('Weight'), t('Remove'));
$output = '';
drupal_add_tabledrag('reorder-displays', 'order', 'sibling', 'weight');
$output = drupal_render($form['override']);
$output .= '<div class="scroll">';
$output .= theme('table',
array('header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'reorder-displays'),
));
$output .= '</div>';
$output .= drupal_render_children($form);
return $output;
}
/**
* Turn the rearrange form into a proper table
*/
......
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