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 "":
/\(([^)]+)\)"?$/
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | select2boxes-brackets-in-label-3266030-2.patch | 507 bytes | davidhk |
Comments
Comment #2
davidhk commentedComment #4
matsbla commentedGreat, thank you!