From f5ec82532c70431d010cbecceb7247f163d3fba8 Mon Sep 17 00:00:00 2001 From: Dries <dries@buytaert.net> Date: Fri, 10 May 2013 13:35:23 -0400 Subject: [PATCH] =?UTF-8?q?Issue=20#1985138=20by=20Berdir,=20G=C3=A1bor=20?= =?UTF-8?q?Hojtsy:=20Fixed=20New=20autocomplete=20taxonomy=20terms=20not?= =?UTF-8?q?=20previewable.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/Drupal/node/Tests/PagePreviewTest.php | 53 +++++++++++++++++-- ...ntityReferenceTaxonomyTermRssFormatter.php | 4 +- .../Plugin/field/formatter/LinkFormatter.php | 7 ++- .../Plugin/field/formatter/PlainFormatter.php | 5 +- .../field/formatter/RSSCategoryFormatter.php | 4 +- .../field/formatter/TaxonomyFormatterBase.php | 4 +- .../widget/TaxonomyAutocompleteWidget.php | 2 +- 7 files changed, 61 insertions(+), 18 deletions(-) diff --git a/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php b/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php index 28b04cde58a9..ed93b253f90a 100644 --- a/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php @@ -68,7 +68,8 @@ function setUp() { 'parent' => '0', ), ), - ) + ), + 'cardinality' => '-1', ); field_create_field($this->field); @@ -77,7 +78,7 @@ function setUp() { 'entity_type' => 'node', 'bundle' => 'page', 'widget' => array( - 'type' => 'options_select', + 'type' => 'taxonomy_autocomplete', ), // Hide on full display but render on teaser. 'display' => array( @@ -90,6 +91,9 @@ function setUp() { ), ); field_create_instance($this->instance); + entity_get_display('node', 'page', 'default') + ->setComponent($this->field_name) + ->save(); } /** @@ -105,7 +109,7 @@ function testPagePreview() { $edit = array(); $edit[$title_key] = $this->randomName(8); $edit[$body_key] = $this->randomName(16); - $edit[$term_key] = $this->term->id(); + $edit[$term_key] = $this->term->label(); $this->drupalPost('node/add/page', $edit, t('Preview')); // Check that the preview is displaying the title, body and term. @@ -119,6 +123,49 @@ function testPagePreview() { $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); + + // Save the node. + $this->drupalPost('node/add/page', $edit, t('Save')); + $node = $this->drupalGetNodeByTitle($edit[$title_key]); + + // Check the term was displayed on the saved node. + $this->drupalGet('node/' . $node->nid); + $this->assertText($edit[$term_key], 'Term displayed.'); + + // Check the term appears again on the edit form. + $this->drupalGet('node/' . $node->nid . '/edit'); + $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); + + // Check with two new terms on the edit form, additionally to the existing + // one. + $edit = array(); + $newterm1 = $this->randomName(8); + $newterm2 = $this->randomName(8); + $edit[$term_key] = $this->term->label() . ', ' . $newterm1 . ', ' . $newterm2; + $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview')); + $this->assertRaw('>' . $newterm1 . '<', 'First new term displayed.'); + $this->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.'); + // The first term should be displayed as link, the others not. + $this->assertLink($this->term->label()); + $this->assertNoLink($newterm1); + $this->assertNoLink($newterm2); + + $this->drupalPost('node/add/page', $edit, t('Save')); + + // Check with one more new term, keeping old terms, removing the existing + // one. + $edit = array(); + $newterm3 = $this->randomName(8); + $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2; + $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview')); + $this->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.'); + $this->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.'); + $this->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.'); + $this->assertNoText($this->term->label()); + $this->assertNoLink($newterm1); + $this->assertNoLink($newterm2); + $this->assertNoLink($newterm3); + $this->drupalPost('node/add/page', $edit, t('Save')); } /** diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/EntityReferenceTaxonomyTermRssFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/EntityReferenceTaxonomyTermRssFormatter.php index a21a7ce22c33..c4cb3cf30404 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/EntityReferenceTaxonomyTermRssFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/EntityReferenceTaxonomyTermRssFormatter.php @@ -38,9 +38,9 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { foreach ($items as $delta => $item) { $entity->rss_elements[] = array( 'key' => 'category', - 'value' => $item['target_id'] != 'autocreate' ? $item['entity']->label() : $item['label'], + 'value' => $item['entity']->label(), 'attributes' => array( - 'domain' => $item['target_id'] != 'autocreate' ? url('taxonomy/term/' . $item['target_id'], array('absolute' => TRUE)) : '', + 'domain' => $item['target_id'] ? url('taxonomy/term/' . $item['target_id'], array('absolute' => TRUE)) : '', ), ); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php index 10d566ea8a1d..7f92a88f53e6 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php @@ -33,12 +33,11 @@ class LinkFormatter extends TaxonomyFormatterBase { public function viewElements(EntityInterface $entity, $langcode, array $items) { $elements = array(); - // Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is - // not set. Theme such terms as just their name. + // Terms without tid do not exist yet, theme such terms as just their name. foreach ($items as $delta => $item) { - if ($item['tid'] == 'autocreate') { + if (!$item['tid']) { $elements[$delta] = array( - '#markup' => check_plain($item['name']), + '#markup' => check_plain($item['entity']->label()), ); } else { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php index 33eeffcc585f..7e61c9cee4d9 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php @@ -33,12 +33,9 @@ class PlainFormatter extends TaxonomyFormatterBase { public function viewElements(EntityInterface $entity, $langcode, array $items) { $elements = array(); - // Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is - // not set. Theme such terms as just their name. foreach ($items as $delta => $item) { - $name = ($item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name']); $elements[$delta] = array( - '#markup' => check_plain($name), + '#markup' => check_plain($item['entity']->label()), ); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php index 7b7e6e44b17b..5a96d537e2e5 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php @@ -34,7 +34,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { // Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is // not set. Theme such terms as just their name. foreach ($items as $item) { - if ($item['tid'] != 'autocreate') { + if ($item['tid']) { $value = $item['entity']->label(); $uri = $item['entity']->uri(); @@ -42,7 +42,7 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { $domain = url($uri['path'], $uri['options']); } else { - $value = $item['name']; + $value = $item['entity']->label(); $domain = ''; } $entity->rss_elements[] = array( diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php index e743350b5e32..f5071375618c 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php @@ -30,7 +30,7 @@ public function prepareView(array $entities, $langcode, array &$items) { foreach ($entities as $id => $entity) { foreach ($items[$id] as $delta => $item) { // Force the array key to prevent duplicates. - if ($item['tid'] != 'autocreate' && $item['tid'] !== FALSE) { + if ($item['tid'] !== FALSE) { $tids[$item['tid']] = $item['tid']; } } @@ -51,7 +51,7 @@ public function prepareView(array $entities, $langcode, array &$items) { $items[$id][$delta]['entity'] = $terms[$item['tid']]; } // Terms to be created are not in $terms, but are still legitimate. - elseif ($item['tid'] == 'autocreate') { + elseif ($item['tid'] === FALSE && isset($item['entity'])) { // Leave the item in place. } // Otherwise, unset the instance value, since the term does not exist. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php index 9624d43361b5..7ceb35095366 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php @@ -86,7 +86,7 @@ public function massageFormValues(array $values, array $form, array &$form_state // Translate term names into actual terms. foreach($values as $value) { // See if the term exists in the chosen vocabulary and return the tid; - // otherwise, create a new 'autocreate' term for insert/update. + // otherwise, create a new term. if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) { $term = array_pop($possibilities); $item = array('tid' => $term->id()); -- GitLab