diff --git a/core/modules/image/src/Controller/QuickEditImageController.php b/core/modules/image/src/Controller/QuickEditImageController.php index e7e36647b2..c0199db086 100644 --- a/core/modules/image/src/Controller/QuickEditImageController.php +++ b/core/modules/image/src/Controller/QuickEditImageController.php @@ -147,7 +147,7 @@ public function upload(EntityInterface $entity, $field_name, $langcode, $view_mo $entity->$field_name->setValue($value); // Render the new image using the correct formatter settings. - $entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModes($entity->getEntityTypeId())); + $entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModesOptions($entity->getEntityTypeId())); if (in_array($view_mode_id, $entity_view_mode_ids, TRUE)) { $output = $entity->$field_name->view($view_mode_id); } diff --git a/core/modules/quickedit/src/QuickEditController.php b/core/modules/quickedit/src/QuickEditController.php index c2dac0f7e0..ef0571a8d4 100644 --- a/core/modules/quickedit/src/QuickEditController.php +++ b/core/modules/quickedit/src/QuickEditController.php @@ -288,7 +288,7 @@ public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view * @see hook_quickedit_render_field() */ protected function renderField(EntityInterface $entity, $field_name, $langcode, $view_mode_id) { - $entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModes($entity->getEntityTypeId())); + $entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModesOptions($entity->getEntityTypeId())); if (in_array($view_mode_id, $entity_view_mode_ids)) { $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); $output = $entity->get($field_name)->view($view_mode_id); diff --git a/core/modules/quickedit/tests/src/Functional/QuickEditDefaultViewMode.php b/core/modules/quickedit/tests/src/Functional/QuickEditDefaultViewMode.php new file mode 100644 index 0000000000..9022fd5a4e --- /dev/null +++ b/core/modules/quickedit/tests/src/Functional/QuickEditDefaultViewMode.php @@ -0,0 +1,113 @@ +drupalCreateContentType(['type' => 'article']); + $this->adminUser = $this->drupalCreateUser([ + 'administer views', + 'edit any article content', + 'access in-place editing', + ]); + $this->drupalLogin($this->adminUser); + } + + /** + * Test views does not use a non-existent view mode, to avoid Quick Edit to break + */ + public function testUseDefaultViewMode() { + // Create some content. + $node = $this->drupalCreateNode(['type' => 'article']); + + // Get the content editing form. + $url = $this->buildUrl('quickedit/form/node/' . $node->id() . '/body/en/default'); + + $client = $this->getHttpClient(); + $post = ['nocssjs' => 'true']; + $response = $client->post($url, [ + 'body' => http_build_query($post), + 'query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax'], + 'cookies' => $this->getSessionCookies(), + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + 'http_errors' => FALSE, + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + $ajax_commands = Json::decode($response->getBody()); + + // Prepare form values for submission. drupalPostAJAX() is not suitable for + // handling pages with JSON responses, so we need our own solution here. + $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) + && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match); + + $this->assertTrue($form_tokens_found, 'Form tokens found in output.'); + + $client = $this->getHttpClient(); + $url = $this->buildUrl('quickedit/form/node/' . $node->id() . '/body/en/default'); + + $post = [ + 'nocssjs' => 'true', + 'form_id' => 'quickedit_field_form', + 'form_token' => $token_match[1], + 'form_build_id' => $build_id_match[1], + 'body[0][summary]' => '', + 'body[0][value]' => '

A very quick edit

', + 'body[0][format]' => 'basic_html', + 'op' => t('Save'), + ]; + + $response = $client->post($url, [ + 'body' => http_build_query($post), + 'query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax'], + 'cookies' => $this->getSessionCookies(), + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + 'http_errors' => FALSE, + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + $ajax_commands = Json::decode($response->getBody()); + + $this->assertIdentical($ajax_commands[0]['command'], 'quickeditFieldFormSaved'); + $this->assertNotEmpty($ajax_commands[0]['data']); + $this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.'); + $this->assertTrue(strpos($ajax_commands[0]['data'], 'A very quick edit'), 'Form value saved and printed back.'); + } +}