If a user attempts to add a taxonomy term with a large name (> 255 characters), taxonomy manager generates an exception and crashes.

taxonomy_manager.admin.inc :: taxonomy_manager_form_add_submit(...) ->
taxonomy_manager.admin.inc :: taxonomy_manager_mass_add_terms(...) ->
taxonomy.module :: taxonomy_term_save($term) ->
drupal_write_record('taxonomy_term_data', $term) ->
generates a PDOException, message: "SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'name' at row 1"

The resulting display to the user is an unthemed display of "ERROR The website encountered an unexpected error. Please try again later. "

Instead, the user the should receive an error message that doesn't break the page, maintains the form data they've submitted, and notifies them of which term(s) need to be fixed.

Alternatively, it would be nice to have an option to simply truncate the term and accept it (maybe with warning). For my use case, most of these long term names are being copied & pasted from another document. I'm okay with them being truncated, and I'd rather have the system automatically truncate them for me, rather than having to figure out exactly how many characters I need to remove from each long term name.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chirhotec’s picture

For what its worth, here's the code to simply truncate the term on submission, with a warning to the user.

Index: sites/all/modules/taxonomy_manager/taxonomy_manager.admin.inc
===================================================================
--- sites/all/modules/taxonomy_manager/taxonomy_manager.admin.inc	(revision 2e9e2a8b387aab3da9d0000cf93520e89c73b957)
+++ sites/all/modules/taxonomy_manager/taxonomy_manager.admin.inc	(revision 51f3c82d87f9474bbd9ce12680a43af83640dbd8)
@@ -1269,6 +1269,17 @@
       $current_parents = $parents;
     }
+    
     $term = new stdClass;
+    
+    // truncate long string names that will cause database exceptions
+    if (strlen($name) > 255)
+    {
+      drupal_set_message(
+        "The term name '<em>" . $name . "</em>' was too long and was truncated.",
+        'warning');
+      $name = substr($name, 0, 255);
+    }
     $term->name = $name;
+    
     $term->vid = $vid;
     $term->parent = $current_parents;
randyhook’s picture

Here is a patch to address this issue. I started with chirhotec's truncate code. The configuration form at admin/config/user-interface/taxonomy-manager-settings now includes a textfield for the user to enter the max number of characters they would like their terms truncated to.

Taxonomy manager config

Adding taxonomy terms at admin/structure/taxonomy_manager/voc/vocab now automatically truncates the terms based on the value entered in the config. If no value is entered, the default 255 characters is used.

Note: Taxonomy Manager uses the Drupal core Taxonomy module to save the terms. The core module does not truncate terms. Rather, it sets it's term textfield to a max length of 255, prohibiting the user from entering a longer term. Therefore, we cannot send a string that is too long to the core module and expect some kind of exception handling. Thus I have set up the patch to truncate terms automatically.

Upon adding the terms, the user is notified which terms, if any, were truncated.

Taxonomy manager notification

Note: I would like to eliminate the use of the hardcoded "255" characters if possible.

mh86’s picture

Status: Needs review » Fixed

Committed a different version to fix this problem, see http://drupalcode.org/project/taxonomy_manager.git/blobdiff/5a238943c396...

@randyhook: at the moment I don't see a use case to not have it hardcoded to 255 characters, as this value directly corresponds to the database schema. But feel free to convince me and re-open this (or create a new) issue (feature request) :-)

Thanks for the report and patches!

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

typo