Commit 5d24ec7c authored by catch's avatar catch

Issue #2169835 by damiankloip, dawehner: Cannot change display ID on a Views display.

parent 667ec432
......@@ -2138,7 +2138,7 @@ public function validateOptionsForm(&$form, &$form_state) {
form_error($form['display_id'], $form_state, t('Display name must be letters, numbers, or underscores only.'));
}
foreach ($this->view->display as $id => $display) {
foreach ($this->view->displayHandlers as $id => $display) {
if ($id != $this->view->current_display && ($form_state['values']['display_id'] == $id || (isset($display->new_id) && $form_state['values']['display_id'] == $display->new_id))) {
form_error($form['display_id'], $form_state, t('Display id should be unique.'));
}
......
......@@ -45,6 +45,49 @@ public function testDeleteLink() {
$this->assertFalse($view instanceof View);
}
/**
* Tests the machine name form.
*/
public function testMachineNameOption() {
$this->drupalGet('admin/structure/views/view/test_view');
// Add a new attachment display.
$this->drupalPostForm(NULL, array(), 'Add Attachment');
// Change the machine name for the display from page_1 to test_1.
$edit = array('display_id' => 'test_1');
$this->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_id', $edit, 'Apply');
$this->assertLink(t('test_1'));
// Save the view, and test the new id has been saved.
$this->drupalPostForm(NULL, array(), 'Save');
$view = \Drupal::entityManager()->getStorageController('view')->load('test_view');
$displays = $view->get('display');
$this->assertTrue(!empty($displays['test_1']), 'Display data found for new display ID key.');
$this->assertIdentical($displays['test_1']['id'], 'test_1', 'New display ID matches the display ID key.');
$this->assertFalse(array_key_exists('attachment_1', $displays), 'Old display ID not found.');
// Test the form validation with invalid IDs.
$machine_name_edit_url = 'admin/structure/views/nojs/display/test_view/test_1/display_id';
$error_text = t('Display name must be letters, numbers, or underscores only.');
$edit = array('display_id' => 'test 1');
$this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this->assertText($error_text);
$edit = array('display_id' => 'test_1#');
$this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this->assertText($error_text);
// Test using an existing display ID.
$edit = array('display_id' => 'default');
$this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this->assertText(t('Display id should be unique.'));
// Test that the display ID has not been changed.
$this->drupalGet('admin/structure/views/view/test_view/edit/test_1');
$this->assertLink(t('test_1'));
}
/**
* Tests the 'Other' options category on the views edit form.
*/
......
......@@ -248,27 +248,32 @@ public function submit(array $form, array &$form_state) {
parent::submit($form, $form_state);
$view = $this->entity;
$executable = $view->getExecutable();
// Go through and remove displayed scheduled for removal.
$displays = $view->get('display');
foreach ($displays as $id => $display) {
if (!empty($display['deleted'])) {
$view->getExecutable()->displayHandlers->remove($id);
$executable->displayHandlers->remove($id);
unset($displays[$id]);
}
}
// Rename display ids if needed.
foreach ($view->getExecutable()->displayHandlers as $id => $display) {
foreach ($executable->displayHandlers as $id => $display) {
if (!empty($display->display['new_id'])) {
$new_id = $display->display['new_id'];
$view->getExecutable()->displayHandlers->set($new_id, $view->getExecutable()->displayHandlers->get($id));
$view->getExecutable()->displayHandlers->get($new_id)->display['id'] = $new_id;
$display->display['id'] = $new_id;
unset($display->display['new_id']);
$executable->displayHandlers->set($new_id, $display);
$displays[$new_id] = $displays[$id];
unset($displays[$id]);
// Redirect the user to the renamed display to be sure that the page itself exists and doesn't throw errors.
$form_state['redirect_route'] = array(
'route_name' => 'views_ui.edit_display',
'route_parameters' => array('view' => $view->id(), 'display_id' => $id),
'route_parameters' => array('view' => $view->id(), 'display_id' => $new_id),
);
}
}
......
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