You are here

function imagefield_extended_widget_process in ImageField Extended 6.4

Same name and namespace in other branches
  1. 6.3 imagefield_extended.module \imagefield_extended_widget_process()

Element #process callback function.

1 string reference to 'imagefield_extended_widget_process'
imagefield_extended_elements in ./imagefield_extended.module
Implementation of hook_elements().

File

./imagefield_extended.module, line 219
Insert additional fields into a FileField / ImageField data array.

Code

function imagefield_extended_widget_process($element, $edit, &$form_state, $form) {
  $file = $element['#value'];
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $widget = $field['widget'];
  $extra_values = isset($element['#value']['data']) ? $element['#value']['data'] : array();

  // Load the internal fields we present
  $extended_fields = _imagefield_extended_fields();
  foreach ($extended_fields['textfields'] as $key => $title) {
    $title = t('!ife:' . $key, array(
      '!ife:' . $key => check_plain(drupal_ucfirst($title)),
    ));
    if (!empty($widget['custom_' . $key])) {
      $element['data'][$key]['body'] = array(
        '#type' => $widget['custom_' . $key . '_style'] != 'textfield' ? 'textarea' : 'textfield',
        '#required' => $widget['custom_' . $key . '_required'] ? TRUE : FALSE,
        '#title' => $title,
        '#default_value' => isset($extra_values[$key]['body']) ? $extra_values[$key]['body'] : $widget[$key],
        '#attributes' => array(
          'class' => 'imagefield-text',
        ),
      );

      // The AHAH load is not using the default value.
      if ($form_state['submitted']) {
        $element['data'][$key]['body']['#default_value'] = isset($extra_values[$key]['body']) ? $extra_values[$key]['body'] : $widget[$key];
      }
      else {
        $element['data'][$key]['body']['#value'] = isset($extra_values[$key]['body']) ? $extra_values[$key]['body'] : $widget[$key];
      }
      if (!empty($widget[$key . '_help'])) {
        $help = t('!ife-help:' . $key, array(
          '!ife-help:' . $key => check_plain($widget[$key . '_help']),
        ));
        $element['data'][$key]['body']['#description'] = $help;
      }
      $format = empty($extra_values[$key]['format']) ? FILTER_FORMAT_DEFAULT : $extra_values[$key]['format'];
      if ($widget['custom_' . $key . '_style'] == 'formatted') {
        $element['data'][$key]['format'] = filter_form($format, NULL, array_merge($element['#array_parents'], array(
          'data',
          $key,
          'format',
        )));
      }
      else {
        $element['data'][$key]['format'] = array(
          '#type' => 'hidden',
          '#value' => $format,
        );
      }
      $element['data'][$key]['style'] = array(
        '#type' => 'hidden',
        '#value' => $widget['custom_' . $key . '_style'],
      );
    }
  }
  foreach ($extended_fields['checkboxes'] as $key => $title) {
    $title = t('!ife:workflow_' . $key, array(
      '!ife:workflow_' . $key => check_plain(drupal_ucfirst($title)),
    ));
    if (!empty($widget['workflow_' . $key])) {
      $element['data']['workflow_' . $key] = array(
        '#type' => 'checkbox',
        '#title' => drupal_ucfirst($title),
        '#default_value' => isset($extra_values['workflow_' . $key]) ? $extra_values['workflow_' . $key] : 0,
        '#attributes' => array(
          'class' => 'imagefield-checkbox',
        ),
        '#suffix' => '<div class="clear"></div>',
      );
    }
  }
  $extra_fields = module_invoke_all('imagefield_extended_widget', $element, $extra_values);
  foreach ($extra_fields as $key => $field) {
    $element['data'][$key] = $field;
  }
  return $element;
}