You are here

function _linkimagefield_widget_form in Link Image Field 5

1 call to _linkimagefield_widget_form()
linkimagefield_widget in ./linkimagefield.module
Implementation of hook_widget().

File

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

Code

function _linkimagefield_widget_form($node, $field, &$node_field) {
  $fieldname = $field['field_name'];
  drupal_add_css(drupal_get_path('module', 'linkimagefield') . '/linkimagefield.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',
    '#title' => $delta == 0 ? t('Image') : NULL,
    '#description' => $field['widget']['description'],
    '#tree' => FALSE,
    '#weight' => 1,
  );
  $form[$fieldname]['upload'] = array(
    '#type' => 'button',
    '#value' => t('Upload'),
    '#name' => 'cck_linkimagefield_' . $fieldname . '_op',
    '#attributes' => array(
      'id' => $fieldname . '-attach-button',
    ),
    '#tree' => FALSE,
    '#weight' => 2,
  );

  // 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' => 'linkimagefield_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('linkimagefield_image', $file, $file['alt'], $file['url'], $file['title'], array(
            'width' => '150',
          ), FALSE),
        );

        //drupal_set_message('linkimagefield['. $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,
          );
        }
        $form[$fieldname][$delta]['url'] = array(
          '#type' => 'textfield',
          '#title' => t('URL'),
          '#default_value' => $file['url'],
          '#description' => t('URL. Leave blank to point to the respective node.'),
          '#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]['url'] = array(
          '#type' => 'value',
          '#value' => $file['url'],
        );
      }
      $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;
}