Problem/Motivation

A confusing error occurs if a content creator includes parentheses when creating an entity via an entity reference field. Some actions generate no error. The lack of message with some actions, the error message that appears on node save, and the general behavior, cause confusion for content creators who understand they can add new nodes using entity reference fields.

In some cases, e.g. adding parentheses when creating an entity via a multi-value entity reference field then clicking "add another item", the field turns red but provides no explanation.

In other cases, e.g. clicking "save" when creating an entity via an entity reference field, the error message does not explain that the parentheses are the cause of the failure.

Proposed resolution

System can try finding a reference with that ID, but when match is not found, it should create it even if name/value has parentheses.

Ideally parentheses should be allowed while creating new entities via the entity reference field. Alternatively the error message should provide instructions appropriate to the intended (non-technical) audience.

To reproduce the issue

On a clean, standard install of Drupal:

  1. create an Article
  2. in the default Tags field, add a taxonomy term containing parentheses
  3. click save
  4. note the error message
  5. visit the list of Tags and note that only terms without parentheses are present

To see the alternatives that work as intended

  1. follow the steps above but remove the parentheses and click save
  2. try other special characters (e.g. curly brackets), then click save
  3. visit the list of Tags and note that only terms without parentheses are present
  4. click 'Add term' while on the taxonomy list page and add a term with parentheses

Original issue report

Content type with an Entity Reference field allowed to create new entities to the taxonomy term will not accept values with parentheses.

Fields' REFERENCE TYPE has a Reference method: Default
and checkbox "Create referenced entities if they don't already exist" is checked.

It works and new term is created if I enter the field value:
Test

It does not work when I insert
Test (some other text)

Last one gives an error:
The referenced entity (taxonomy_term: some other text) does not exist.

It probably tries to find the term with an ID 'some other text' and when match is not found, it does not try to create it but blindly believes that entered is ID and it needs to find a match.

But how can one enter a value which has parentheses without making system to think that it's the ID what user entered between parentheses?

Proposed resolution

System can try finding a reference with that ID, but when match is not found, it should create it even if name/value has parentheses.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

adroid created an issue. See original summary.

adroid’s picture

Tested if these kinds of terms can be added directly from the Add new term form - it is. Tested by adding:
Test (some other text)

So seems to be more of a widget case.

dorficus’s picture

@adroid I am having the same issue. Did you find a workaround or patch?

dorficus’s picture

https://www.drupal.org/node/2520416 is possibly a related issue. It's referenced in EntityAutocomplete.php

  public static function extractEntityIdFromAutocompleteInput($input) {
    $match = NULL;

    // Take "label (entity id)', match the ID from inside the parentheses.
    // @todo Add support for entities containing parentheses in their ID.
    // @see https://www.drupal.org/node/2520416
    if (preg_match("/.+\s\(([^\)]+)\)/", $input, $matches)) {
      $match = $matches[1];
    }

    return $match;
  }
kay_v’s picture

Title: The referenced entity (taxonomy_term: some other text) does not exist. » error creating new referenced entity when parentheses appear in title
Version: 8.4.2 » 8.7.x-dev
Issue summary: View changes
kay_v’s picture

Title: error creating new referenced entity when parentheses appear in title » confusing error & behavior when creating a new referenced entity containing parentheses
Issue summary: View changes

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

MrPaulDriver’s picture

It would be good to see this annoying bug fixed.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

schnydszch’s picture

This just came up when trying out in my Drupal install. I hope this bug will be solved soon. Thanks!

Skymen’s picture

Status: Needs review » Needs work

The last submitted patch, 13: 2924783-13.patch, failed testing. View results

Skymen’s picture

Status: Needs work » Needs review
FileSize
1011 bytes

Updated patch. Fixed cases for anonymous users.

Status: Needs review » Needs work

The last submitted patch, 15: 2924783-14.patch, failed testing. View results

Skymen’s picture

Status: Needs work » Needs review
FileSize
1.07 KB

Another fix after tests fail.

Skymen’s picture

Improved version.

Status: Needs review » Needs work

The last submitted patch, 18: 2924783-18.patch, failed testing. View results

Skymen’s picture

Status: Needs work » Needs review
Skymen’s picture

Skymen’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

daveiano’s picture

#18 solves this issue for me.

Skymen’s picture

Status: Needs review » Reviewed & tested by the community
quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Correct me if I am wrong but a test is needed.

Leo Pitt’s picture

This latest patch does not work for me.

After adding the patch and then trying to create a term "taxonomy_term (yo)" I receive the following message:

The referenced entity (taxonomy_term: yo) does not exist.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Proteo’s picture

Patch from #18 worked like a charm, thanks!

Spokje’s picture

Component: entity system » entity_reference.module
Status: Needs work » Needs review
Issue tags: -Needs tests +Bug Smash Initiative
FileSize
1.79 KB
3.38 KB
Spokje’s picture

Component: entity_reference.module » entity system
FileSize
2.47 KB

The last submitted patch, 30: 2924783_test_only_should_fail.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 30: 2924783-30.patch, failed testing. View results

Spokje’s picture

Spokje’s picture

I think the fail_test patch shows that the current patch doesn't solve _all_ cases of parentheses being used in the title of the referenced entity. (@Leo Pitt in Leo Pitt #27 already found out that text in parentheses didn't work).

mkindred’s picture

Thus far, #18 is working for me in 9.2.9.

gambry’s picture

Have you thought about the implications?
Currently - without the patch - using the scenario Article+Tags in the IS, if you type "Foo (2)", and a term with ID 2 exists, that term will be used.
It doesn't matter what label you put before the "(2)", the label of term with ID 2 will be used.

The work on this issue, and its patch, only adds to this confusion since the behavior of the widget will be different depending what is within the parentheses, and according to the entities already existing in the database.

Talking about the widget... this is how the entity autocomplete form element is meant to work, with expected input being "Label (ID)". Allowing a different pattern is changing the way entity autocomplete works.

My suggestion: close this issue with "work as designed". Then we can open a different feature request issue proposing a widget extending entity autocomplete but allowing parentheses. This can also be a contrib module initially, to speed up adoption.

RE the misleading error, we can create another issue providing meaningful errors when parenthesis are used within the entity autocomplete element.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ainsofs’s picture

# edit - pls ignore or delete this message if you are admin. thnx

--
#18 not working on 9.4.5

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

szato’s picture

Using Drupal core 9.4.8 + term reference with views reference display.
Patch #18 fixed my issue.

VasyOK’s picture

Version: 10.1.x-dev » 9.5.x-dev

Drupal 9.5.
[For me] Today pathces are not needed for use with Autocomplite (Tags style) widget. Even if linked entites contain brackets.
But with popular_tags module (mb with some others) patch #18 is necessary.

lubwn’s picture

Sadly this does not work in Drupal 9.5.9 at all. Putting any text in parentheses triggers an error so entities containing text in parentheses can not be referenced at all (nor created). Applying patch from #18 does not solve the issue.

lubwn’s picture

Sadly this is an error which seems to be too deep to be fixed in short term so I developed custom solution.

I have basicly only added new custom fields with same names as taxonomy fields I already had but with formatter "Text (plain)". Then I used hook_entity_presave() to match text inputted by user into those custom fields and check whether taxonomies exists by name or not, setting them to previously created taxonomy reference fields as target_id. In this custom solution you also have full control over what you want to use as a delimited between each taxonomy entity (for example ";" instead of "," or so)

I guess proper "Drupal" way would be either somehow fix the error or create custom taxonomy field formatter with the logic needed for that but to be fair that would take so much more time and effort as everything with Drupal that those custom hacks works better and are faster to implement.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.