1


I'm trying to figure out how I can limit the maximum length of an image title to 40. I have enabled title from the image field in my content and it works fine except that it seems to take unlimited number of letters. I want to reduce the maximum length of image title to 40.
Does anyone have any idea how to do it?
Thanks

1 Answer 1

2

Something like this may work:

/**
 * Implements hook_field_widget_form_alter
 */
function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['type'] == 'image' && $context['instance']['widget']['type'] == 'image_image' && !empty($context['instance']['settings']['title_field'])) {
    foreach (element_children($element) as $delta) {
      $element[$delta]['#process'][] = 'mymodule_image_field_widget_process';
    }
  }
}

/**
 * Image field process callback
 */
function mymodule_image_field_widget_process($element, &$form_state, $form) {
  // Set title maxlength, if accessible
  $maxlength = variable_get('imagefield_maxlength', 40);
  if ($element['title']['#access']) {
    $element['title']['#maxlength'] = $maxlength;
  }

  return $element;
}
2
  • I got this working by editing the image module's image.field.inc file. Changed to '#maxlength' => 100
    – misamisa
    Commented Feb 6, 2013 at 3:50
  • Hacking core is bad practice Commented Feb 6, 2013 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.