While creating discounts programmatically I checked the column max length so I knew exactly how long I could make the machine name. The current length is 64 and this properly saves. If i then edit that discount through the admin ui i get this error message

Machine-readable name cannot be longer than 23 characters but is currently 25 characters long.

Looking at commerce_discount.admin.inc the maxlength is being restricted to 32 characters rather than 64. Is there a reason for this I'm not seeing? Simply updating #maxlength to 55 resolved the issue for me.

// Machine-readable type name.
  $form['name'] = array(
    '#type' => 'machine_name',
    // Strip the 'discount_' prefix from the beginning of the string
    '#default_value' => isset($commerce_discount->name) ? substr($commerce_discount->name, 9) : '',
    '#disabled' => $commerce_discount->hasStatus(ENTITY_IN_CODE),
    '#machine_name' => array(
      'exists' => '_commerce_discount_name_exists',
      'source' => array('label'),
    ),
    '#description' => t('A unique machine-readable name for this message type. It must only contain lowercase letters, numbers, and underscores.'),
    // 32 characters minus the 'discount_' prefix.
    '#maxlength' => 23,
    // This field should stay LTR even for RTL languages.
    '#field_prefix' => '<span dir="ltr">discount_',
    '#field_suffix' => '</span>&lrm;',
  );

Another potential problem is the maxlength offset is hardcoded. Would this cause an issue with a different discount type being created with the api having a different length prefix?

CommentFileSizeAuthor
#3 discount_name_property-2455069-3.patch820 bytesjoelpittet

Comments

agn507’s picture

Issue summary: View changes
agn507’s picture

Issue summary: View changes
joelpittet’s picture

StatusFileSize
new820 bytes

I can't see why not. Here's a patch to make it quite evident.

joelpittet’s picture

Status: Active » Needs review
joelpittet’s picture

Status: Needs review » Fixed

Fix in dev, thanks @agn507

  • joelpittet committed a1241d2 on 7.x-1.x authored by agn507
    Issue #2455069 by joelpittet, agn507: Discount name property restricted...

Status: Fixed » Closed (fixed)

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