You are here

function _imagefield_widget_form in ImageField 5

Same name and namespace in other branches
  1. 5.2 imagefield.module \_imagefield_widget_form()
1 call to _imagefield_widget_form()
imagefield_widget in ./imagefield.module
Implementation of hook_widget().

File

./imagefield.module, line 434
Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.

Code

function _imagefield_widget_form($node, $field, &$node_field) {
  $fieldname = $field['field_name'];
  drupal_add_css(drupal_get_path('module', 'imagefield') . '/imagefield.css');
  $form = array();
  $form[$fieldname] = array(
    '#type' => 'fieldset',
    '#title' => t($field['widget']['label']),
    '#weight' => $field['widget']['weight'],
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );

  // Seperate from tree becase of that silly things won't be
  // displayed if they are a child of '#type' = form issue
  $form[$fieldname][$fieldname . '_upload'] = array(
    '#type' => 'file',
    '#description' => $field['widget']['description'],
    '#tree' => FALSE,
    '#weight' => 9,
  );
  $form[$fieldname]['upload'] = array(
    '#type' => 'button',
    '#value' => t('Upload'),
    '#name' => 'cck_imagefield_' . $fieldname . '_op',
    '#attributes' => array(
      'id' => $fieldname . '-attach-button',
    ),
    '#tree' => FALSE,
    '#weight' => 10,
  );

  // Store the file data object to be carried on.
  if (is_array($node_field) && count($node_field)) {
    foreach ($node_field as $delta => $file) {
      if ($file['filepath'] && !$file['flags']['delete']) {
        $form[$fieldname][$delta] = array(
          '#theme' => 'imagefield_edit_image_row',
        );
        $form[$fieldname][$delta]['flags']['delete'] = array(
          '#type' => 'checkbox',
          '#title' => t('Delete'),
          '#default_value' => 0,
        );
        $filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];
        $form[$fieldname][$delta]['preview'] = array(
          '#type' => 'markup',
          '#value' => theme('imagefield_image', $file, $file['alt'], $file['title'], array(
            'width' => '150',
          ), FALSE),
        );

        //drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');
        $form[$fieldname][$delta]['description'] = array(
          '#type' => 'markup',
          '#value' => '<strong>' . t('Filename: ') . '</strong>' . $file['filename'],
        );
        $form[$fieldname][$delta]['alt'] = array(
          '#type' => 'hidden',
          '#value' => $file['filename'],
        );

        // overwrite with an input field if custom_alt is flagged;
        if ($field['widget']['custom_alt']) {
          $form[$fieldname][$delta]['alt'] = array(
            '#type' => 'textfield',
            '#title' => t('Alternate text'),
            '#default_value' => $file['alt'],
            '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
            '#maxlength' => 255,
            '#size' => 10,
          );
        }
        $form[$fieldname][$delta]['title'] = array(
          '#type' => 'hidden',
          '#value' => $file['filename'],
        );

        // overwrite with an input field if custom_title is flagged;
        if ($field['widget']['custom_title']) {
          $form[$fieldname][$delta]['title'] = array(
            '#type' => 'textfield',
            '#title' => t('Title'),
            '#default_value' => $file['title'],
            '#description' => t('Text to be displayed on mouse overs.'),
            '#maxlength' => 255,
            '#size' => 10,
          );
        }

        // Special handling for single value fields
        if (!$field['multiple']) {
          $form[$fieldname][$delta]['replace'] = array(
            '#type' => 'markup',
            '#value' => t('If a new image is chosen, the current image will be replaced upon submitting the form.'),
          );
        }
      }
      elseif ($file['filepath'] && $file['flags']['delete']) {

        // Hide all the form values if this item is marked for deletion
        $form[$fieldname][$delta]['flags']['delete'] = array(
          '#type' => 'value',
          '#value' => $file['flags']['delete'],
        );
        $form[$fieldname][$delta]['title'] = array(
          '#type' => 'value',
          '#value' => $file['title'],
        );
        $form[$fieldname][$delta]['alt'] = array(
          '#type' => 'value',
          '#value' => $file['alt'],
        );
      }
      $form[$fieldname][$delta]['filename'] = array(
        '#type' => 'value',
        '#value' => $file['filename'],
      );
      $form[$fieldname][$delta]['filepath'] = array(
        '#type' => 'value',
        '#value' => $file['filepath'],
      );
      $form[$fieldname][$delta]['filemime'] = array(
        '#type' => 'value',
        '#value' => $file['filemime'],
      );
      $form[$fieldname][$delta]['filesize'] = array(
        '#type' => 'value',
        '#value' => $file['filesize'],
      );
      $form[$fieldname][$delta]['fid'] = array(
        '#type' => 'value',
        '#value' => $file['fid'],
      );
    }
  }
  return $form;
}