3

I wonder how I can alter the description of the custom image field. I can't use hook_form_alter. I couldn't find the variable. Please advise. Thanks!

enter image description heree.g

0
6

The alternate text field is added by image_field_widget_process(), which is the process element function added by image_field_widget_form() (an implementation of hook_field_widget_form()). To alter it, you can add your element process function with 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']['alt_field'])) {
    foreach (element_children($element) as $delta) {
      $element[$delta]['#process'][] = 'mymodule_field_widget_process';
    }
  }
}

function mymodule_field_widget_process($element, &$form_state, $form) {
  // Verify the element is accessible to the currently logged-in user.
  // Differently, it will not be visible to the user, and it is useless to alter the description, or the title.
  if ($element['alt']['#access']) {
    // Use $element['alt']['#title'] for the title.
    // Use $element['alt']['#description'] for the description.
  }

  return $element;
}

The reason I would use hook_field_widget_form_alter() instead of hook_form_alter() is that the first has direct access to some information the latter doesn't get, such as the settings for the field instance, and the field settings. In this case, I can access $context['instance']['settings']['alt_field'] to check if the field requires the alternate text field.

If you are going to use hook_form_alter(), remember that:

  • A field could have more than one widget, if the user is allowed to enter multiple values.
  • When its #access property is set to FALSE, a form element is not accessible to the currently logged-in user; in that case, it is useless to change any other properties for the form element, as it will not be shown to the user.
7
  • Thanks, yes hook_field_widget_form_alter is the best form_alter hook to use to add the process callback there. Answer updated for clarification. Jan 5 '13 at 21:54
  • Actually, you didn't update your answer: You changed it to suggest the same hook I am suggesting.
    – apaderno
    Jan 5 '13 at 22:07
  • I initially wrote "form_alter", then changed to hook_form_alter. The actual form_alter hook could be field_widget_form_alter (best) hook_form_alter hook_FORM_ID_alter etc.. apologies for any offense there. I've changed it back to the origin "form_alter" Jan 5 '13 at 22:10
  • No offense taken. Just to better state what I was saying, hook_form_alter(), and hook_field_widget_form_alter() are two different hooks which take different arguments; the latter is not the hook_form_FORM_ID_alter() variant either.
    – apaderno
    Jan 5 '13 at 22:49
  • 1
    Great thanks! @kiamlaluno. You are right, a field can use more than one widget. I have tested and your updated code works perfectly now. Besides, I use $context['form']['type']['#value'] == 'gallery' to narrow down only to my gallery content type without affecting others. Thanks for your help and guidance really. Thank you!
    – Adamtan
    Jan 6 '13 at 10:50
3

That text is added by

image_field_widget_process

You can alter it using form_alter to add your own process callback to the element.

See also

image_field_widget_form for some additional info, e.g the process callback added by $elements[$delta]['#process'][] = 'image_field_widget_process';

Alternatively, a module solution is to use

stringoverrides

and translate the text via the admin user interface.

1
  • The String Overrides module changes every instance of the string. It cannot be used to change a string only in specific cases.
    – apaderno
    Jan 5 '13 at 11:50
0

Although @kiamlaluno's answer comes close, it's returning a blank image field for me. Here's a modified solution that worked.

// Alter image field Alt Title & Description
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  // Replace field_MYFIELD with your field name
  if ($context['field']['field_name'] == 'field_MYFIELD') {
    // Loop through the element children
    foreach (element_children($element) as $key => $child) {
      // Add the new process function to the element
      $element[$key]['#process'][] = 'MYMODULE_image_field_widget_process';
    }
  }
}

function MYMODULE_image_field_widget_process($element, &$form_state, $form) {
  // Change the title field label and description
  $element['alt']['#title'] = 'Effect';
  $element['alt']['#description'] = '{effect options}';

  // Return the altered element
  return $element;
}

Answer is inspired by a duplicate question asked on StackOverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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