diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php index 7c643cf03d5da87923eb4c090da8814b9d6008f1..847da687ca6933f404282f9bcbaa8e0e856df9ef 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php @@ -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.')); } diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Tests/ViewEditTest.php b/core/modules/views_ui/lib/Drupal/views_ui/Tests/ViewEditTest.php index 27655a8e7e8b4d2c8ba604f3940bdf128bf74f52..ae713b3ca56db27fa1758cab97d3d230105c0918 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Tests/ViewEditTest.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Tests/ViewEditTest.php @@ -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. */ diff --git a/core/modules/views_ui/lib/Drupal/views_ui/ViewEditFormController.php b/core/modules/views_ui/lib/Drupal/views_ui/ViewEditFormController.php index 84df38ea5c23e8d762b4d2aff20ea9fbf6035c17..01786bbcfacda341dbf23ab21bf981864a10a008 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/ViewEditFormController.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/ViewEditFormController.php @@ -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), ); } }