You are here

function image_field_caption_form_element_after_build in Image Field Caption 7

An #after_build callback for image fields. Attaches the caption textarea to the image field form element.

1 string reference to 'image_field_caption_form_element_after_build'
image_field_caption_form_alter in ./image_field_caption.module
Implements hook_form_alter().

File

./image_field_caption.module, line 156
Provides a caption textarea for image fields.

Code

function image_field_caption_form_element_after_build($element, &$form_state) {
  $delta = 0;
  while (isset($element['und'][$delta])) {
    $field_name = $element['und']['#field_name'];
    if ($form_state['values'][$field_name]['und'][$delta]['fid'] != 0) {
      $default_value = '';
      if (isset($element['und'][$delta]['#entity']->{$field_name})) {
        $field = $element['und'][$delta]['#entity']->{$field_name};
        if (isset($field['und'][$delta]['caption'])) {
          $default_value = $field['und'][$delta]['caption'];
        }
      }
      $element['und'][$delta]['caption'] = array(
        '#type' => 'textarea',
        '#title' => t('Caption'),
        '#description' => t('Enter caption text or html for this image.') . '<br />' . t('Allowed html tags: ') . image_field_captions_allowed_tags(),
        '#value' => $default_value,
        /* http://drupal.org/node/1189584 */
        '#attributes' => array(
          'id' => "{$element['#id']}-und-{$delta}-caption",
          'name' => "{$element['#array_parents'][0]}[und][{$delta}][caption]",
        ),
      );
    }
    $delta++;
  }
  return $element;
}