Problem/Motivation

If someone tries to insert a long term via autocomplete on a page, then it will result in an exception because no validation is fired that the term isn't too long.

This is also an issue on D7, which is where I originally uncovered the situation. I had expected it to be fixed in D8 with typed data, but that isn't the case.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'name' at row 1: INSERT INTO {taxonomy_term_field_data} (tid, vid, langcode, name, description__value, description__format, weight, changed, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => 2 [:db_insert_placeholder_1] => tags [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => Donec mi odio; faucibus at; scelerisque quis; convallis in; nisi; Proin sapien ipsum;porta a; auctor quis; euismod ut; mi; Sed libero; Praesent vestibulum dapibus nibh; Pellentesque auctor neque nec urna; Aliquam lorem ante; dapibus in; viverra quis; feugiat a; tellus; Vivamus aliquet elit ac nisl; Donec vitae orci sed dolor rutrum auctor; Donec pede justo; fringilla vel; aliquet nec; vulputate eget; arcu; Donec venenatis vulputate lorem [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => 0 [:db_insert_placeholder_7] => 1444326833 [:db_insert_placeholder_8] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 757 of /home/dfv2i/www/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Proposed resolution

Remaining tasks

Fix it. I think a good starting point would be EntityAutocomplete->validateEntityAutocomplete().

User interface changes

API changes

Data model changes

Comments

heddn created an issue. See original summary.

heddn’s picture

Title: Taxonomy autocomplete does not validate for length » Taxonomy autocomplete does not validate for term name length
Version: 8.0.x-dev » 7.x-dev
Issue tags: +Needs tests

Temporarily switching this to D7 to see how it fairs. It still needs some tests though.

heddn’s picture

Status: Active » Needs review
StatusFileSize
new998 bytes
heddn’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Needs review » Needs work

Patch in #3 is against D7.

azinck’s picture

StatusFileSize
new993 bytes
new689 bytes

Refined the error message in the D7 patch a bit to make it less technical ("strings" is probably less understandable than "terms"). Other than that, this looks great.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

imre.horjan’s picture

Version: 8.1.x-dev » 7.x-dev

This patch is not for D8, so I've changed version.

heddn’s picture

Version: 7.x-dev » 8.2.x-dev

Even though the patches are for D7, this needs to be fixed in D8 first, because of the backport policy. Switching back to D8.

imre.horjan’s picture

Yeah, I forget to mention there's an other issue for D8, which's a general fix for all kind of entity references, not only taxonomy terms:
https://www.drupal.org/node/2438017
So, I still think this is a D7-only issue.

imre.horjan’s picture

Version: 8.2.x-dev » 7.x-dev

Check this, this is closed as duplicate for D8:
https://www.drupal.org/node/2569549

heddn’s picture

Title: Taxonomy autocomplete does not validate for term name length » [PP-1] Taxonomy autocomplete does not validate for term name length
Status: Needs work » Postponed
Related issues: +#2438017: Entity reference does not validate auto-created entities
sepgil’s picture

StatusFileSize
new1000 bytes

Fixed the path from #2584225-5: [PP-1] Taxonomy autocomplete does not validate for term name length to use drupal_strlen instead strlen, so that unicode characters are supported.

klausi’s picture

Status: Postponed » Needs work
  1. +++ b/modules/taxonomy/taxonomy.module
    @@ -1767,6 +1767,12 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
    +        form_set_error($element['#name'], t('%term_name is too long. Individual terms in %title may not exceed 256 characters.', array(
    

    This should use form_error() on $element instead. And the message should be 255, not 256.

  2. +++ b/modules/taxonomy/taxonomy.module
    @@ -1767,6 +1767,12 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
    +          '%title' => $element['#title'],
    

    In my testing with other taxonomy autocomplete module $element['#title'] was not even set. So I have this workaround:

        if (!empty($element['textfield']['#title'])) {
          $title = $element['textfield']['#title'];
        }
        else {
          $title = $element['#title'];
        }
    

    Because the textfield property is there for me.

klausi’s picture

StatusFileSize
new1.13 KB

Patch. This still needs tests, so leaving at "needs work".

puneetsharma’s picture

Patch with replacing !empty with isset.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.