I have a display with 102 product references in a field type "Product reference" with "Autocomplete text field" widget and when I try to save it, I get this error: [PRODUCTS_REFERENCE] cannot be longer than 1024 characters but is currently XXXX characters long.

The problem is that this field type has a #maxlength of 1024 in commerce_product_reference.module

I understand that this may be a extreme use case and it may not make sense to make maxlength longer for everyone, but wanted to post the workaround just in case someone else's need it.

function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['instance']['widget']['type'] == 'commerce_product_reference_autocomplete') {
    $element['#maxlength'] = 3072;
  }
}
CommentFileSizeAuthor
#1 1336290.product_reference_maxlength.patch812 bytesrszrama

Comments

rszrama’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new812 bytes

You know, I saw that the other day when I was scrolling through the Product Reference module, and I couldn't really remember why we had a limitation on the field at all. As best as I can figure, we used that because that's what the Taxonomy module does for term reference autocomplete widgets.

As long as your database server can handle attempting to load so many hundreds of queries on a form submission, there's no reason to limit the maximum length of the field. Let's just remove the length limitation and if it turns out we need to, we can always change validation so it doesn't attempt to fully load products for the entered SKUs. I hesitate to change that right now because I don't know if someone might be storing products in alternate databases and how it would work if we were to try and just load the product ID directly from the database that matches a SKU.

Gonna let test bot tell me if I screwed something up. : )

rszrama’s picture

Status: Needs review » Fixed

Alrighty, committed. If this is a performance hit for anyone, that's why we have the "hidden widget". : )

facine’s picture

Ok, thanks!

Status: Fixed » Closed (fixed)

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

AJen’s picture

Version: 7.x-1.x-dev » 7.x-1.1

I am getting a similar error message. I've updated to 1.1 and I believe that patch is incorporated in that (as I can't find that line). The message I'm getting is 'Product Reference cannot be longer than 128 characters but is currently 211 characters long.'

I have imported the product nodes and it's only when I go in to update the image that I get this message and only on nodes where there many product references in the autocomplete widget.

rszrama’s picture

Version: 7.x-1.1 » 7.x-1.x-dev

Nope, the patch was committed after 1.1. If you update to the latest -dev, you'll have it.

EDIT: Sorry, didn't realize which issue this was. Yes, the patch here made it into 1.1, but Drupal's default setting is apparently 128 if you don't specify a limit. A follow-up patch now in 1.x-dev until we roll a new point release (maybe in a couple weeks) fixes this again.

AJen’s picture

Worked a treat. Many thanks.