Summary

The term merge autocomplete widget response correctly quotes term names containing commas or quotes, however the autocomplete validation function does not unwrap the quotes causing the following error and preventing term merging.

There are no terms matching %value in the %vocabulary vocabulary.

Test Cases

Case 1

In any taxonomy, create a term named Test, and a term named "Quoted Test" (note the quotes). On the "Merge terms" page, select Test for "Terms to Merge" and "Merge Into" (using the "Autocomplete" widget), "Quoted Test". Upon selection the autocomplete widget will display """Quoted Test"" (2)". Attempt to submit the form.

Expected behavior: merge confirmation screen.
Actual behavior: There are no terms matching """Quoted Test"" (2)" in the Author vocabulary.

Case 2

In any taxonomy, create a term named Test, and a term named Test, with comma. On the "Merge terms" page, select Test for "Terms to Merge" and "Merge Into" (using the "Autocomplete" widget), Test, with comma. Upon selection the autocomplete widget will display "Test, with comma (3)". Attempt to submit the form.

Expected behavior: merge confirmation screen.
Actual behavior: There are no terms matching "Test, with comma (3)" in the Author vocabulary.

Cause

The two functions in question are term_merge_form_term_trunk_widget_autocomplete_autocomplete() and term_merge_form_trunk_term_widget_autocomplete_validate(). The autocomplete menu callback (based on taxonomy_autocomplete(), per comments) quotes term names containing commas or quotes:

...
  $term_matches = array();
  foreach ($tags_return as $tid => $name) {
    // Add both term name and tid to array key in order to allow multiple terms
    // with same name to be displayed.
    $key = "$name ($tid)";
    // Names containing commas or quotes must be wrapped in quotes.
    if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
      $key = '"' . str_replace('"', '""', $key) . '"';
    }
    $term_matches[$key] = check_plain($name . ' [tid: ' . $tid . ']');
  }
...

Upon validation, the quotes are not stripped/matched causing quoted values to be ignored:

function term_merge_form_trunk_term_widget_autocomplete_validate($element, &$form_state, $form) {
  // Field value is "name (tid)", match the tid from parenthesis.
  if (preg_match("/.+\((\d+)\)$/", $element['#value'], $matches)) {
    $term = taxonomy_term_load($matches[1]);
  }
...

The preg_match does not account for a trailing quote ("). The validation logic was most recently updated in #2660790: Autocomplete term trunk widget issues.

CommentFileSizeAuthor
#3 term_merge_quoted_terms-2894288.patch604 bytesheydemo

Comments

daxelrod created an issue. See original summary.

daxelrod’s picture

Title: Autocomplete term with comma » Autocomplete term name with comma or quote
Status: Active » Needs review

Following the taxonomy autocomplete validation function as an example, the proper fix for this would be to unwrap the quotes:

function term_merge_form_trunk_term_widget_autocomplete_validate($element, &$form_state, $form) {
  // Names containing commas or quotes must be unwrapped.
  $value = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $element['#value'])));
  // Field value is "name (tid)", match the tid from parenthesis.
  if (preg_match("/.+\((\d+)\)$/", $value, $matches)) {
    $term = taxonomy_term_load($matches[1]);
  }
...

But since we only care about the tid, we can cheat this a bit by adding an optional trailing quote (\"?) to the preg_match:

  if (preg_match("/.+\((\d+)\)\"?$/", $element['#value'], $matches)) {

@bucefal91 (or other maintainer), would you like a patch for this?

heydemo’s picture

StatusFileSize
new604 bytes

Adding patch with regex solution above

astonvictor’s picture

Status: Needs review » Closed (outdated)

D7 reached its EOL back in January 2025, and there is no active release for D7 for this module anymore.
Development or support is not planned for D7. All D7-related issues are marked as outdated in a bunch.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.