Maxlength doesn't appear to work with https://drupal.org/project/inline_entity_form

I'm digging into inline_entity_form and maxlength to see how this all works, although, I'm not sure I'll be able to figure this out. I'm wondering if someone has figured this out or it's currently not possible. :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

thefoo’s picture

Fixed, although I'm not sure how hackery this is or what... but it works:

function maxlength_pre_render($element) {
if (((isset($element['#maxlength']) && $element['#maxlength'] > 0) ||(isset($element['#attributes']['maxlength']) && $element['#attributes']['maxlength'] > 0)) &&
isset($element['#maxlength_js']) && $element['#maxlength_js'] === TRUE) {

isset($element['#maxlength_js']) is returning false because $element['#maxlength_js'] doesn't exist. (Not sure why?)

My fix:

function custom_mods_element_info_alter(&$cache) {
// Add prerender functions to textareas and text fields
$cache['textfield']['#pre_render'][] = 'custom_mods_pre_render';
$cache['textarea']['#pre_render'][] = 'custom_mods_pre_render';
$cache['text_format']['#pre_render'][] = 'custom_mods_pre_render';
}

function custom_mods_pre_render($element) { if (((isset($element['#maxlength']) && $element['#maxlength'] > 0) ||(isset($element['#attributes']['maxlength']) && $element['#attributes']['maxlength'] > 0)) && $element['#id'] == 'edit-field-step-und-form-title') {
$element['#maxlength'] = 100;
$element['#attributes']['maxlength_js_label'][]='@remaining characters remaining';
if ($element['#type'] == 'textarea' && !isset($element['#attributes']['maxlength'])) {
$element['#attributes']['maxlength'] = $element['#maxlength'];
}
$element['#attributes']['class'][] = 'maxlength';
$element['#attached']['js'][] = drupal_get_path('module', 'maxlength') . '/js/maxlength.js';
}
return $element;
}

I had to manually define maxlength and the maxlength label... I know this is not ideal. How can I do this better? Hey, at least I didn't hack the original module. :)

r-mo’s picture

Version: 7.x-3.0-beta1 » 7.x-3.2
Issue summary: View changes
FileSize
1.35 KB

Have this issue applying maxlength to node titles with inline entity forms. Patch attached so it does the same logic as for node forms. Other fields seem to behave ok with hook_field_attach_form.

r-mo’s picture

Status: Active » Needs review
cedewey’s picture

Status: Needs review » Closed (won't fix)

We are only maintaining the Drupal 7 version of the module for critical security fixes, so I'm marking this Closed (won't fix). Thank you everyone for working on this issue. If you do want to maintain the Drupal 7 version, do reach out. We'd be happy to bring you on board as a maintainer.

I also encourage you, if you haven't already, to upgrade your site to Drupal 8/9. We are actively maintaining that version and you would enjoy all of the other features of the latest version of Drupal.