Problem/Motivation

If an item's label contains ( ), that item cannot be saved.

Steps to reproduce

I'm using the widget with an entity-reference field, linking nodes. If I select a node that has ( ) in its title, eg:

Josephine ABAD (née BRACKEN, aka Josephine Rizal) [1876-1902]

Then when I save the page I get the error message:

The referenced entity (node: née BRACKEN, aka Josephine Rizal) does not exist.

Proposed resolution

The problem happens where we process the ajax results for the autocomplete.

            processResults: function (data) {
              var res = $.map(data, function (item) {
                // Prevent empty options being passed to the results.
                if (item.label) {
                  return {
                    id: /\(([^)]+)\)/.exec(item.value)[1],
                    text: decodeHtmlEntities(item.label)
                  };
                }
              });

A typical value for item.value is:

Thomas JACKSON [1844-1915] (5445)

The regex is:

/\(([^)]+)\)/

It looks for the contents of the first (), setting the id to 5445, which is correct. But if there is () in the title, eg

Thomas JACKSON (aka Tom) [1844-1915] (5445)

the regex sets the id to 'aka Tom', causing the error.

We could use $ to tell the regex we're looking for a () at the end of the string:

/\(([^)]+)\)$/

That solves the 'aka Tom' case, but still didn't work for the original problem. It arrives surrounded in "", perhaps because it contains an accented character. So item.value is:

"Josephine ABAD (née BRACKEN, aka Josephine Rizal) [1876-1902] (9137)"

To cope with that I add an optional " before the $, and now it works for labels without brackets, labels with brackets, and labels with brackets and "":

/\(([^)]+)\)"?$/

Comments

davidhk created an issue. See original summary.

davidhk’s picture

Status: Active » Needs review
StatusFileSize
new507 bytes

  • matsbla committed 0ffcc49 on 8.x-1.x authored by davidhk
    Issue #3266030 by davidhk: Brackets in label prevent save
    
matsbla’s picture

Status: Needs review » Fixed

Great, thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.