You are here

function _emthumb_widget_form in Embedded Media Field 5

1 call to _emthumb_widget_form()
emthumb_emfield_widget_extra in contrib/emthumb/emthumb.module
when editing a node with an emfield, this will add our custom thumbnail upload form if allowed.

File

contrib/emthumb/emthumb.module, line 303

Code

function _emthumb_widget_form($node, $field, $items) {
  $fieldname = $field['field_name'];
  drupal_add_css(drupal_get_path('module', 'emthumb') . '/emthumb.css');
  $form = array(
    '#type' => 'fieldset',
    '#title' => t($field['widget']['emthumb_label'] ? t('@label', array(
      '@label' => $field['widget']['emthumb_label'],
    )) : t('@field custom thumbnail', array(
      '@field' => $field['widget']['label'],
    ))),
    '#weight' => $field['widget']['emthumb_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 . '_upload'] = array(
    '#type' => 'file',
    '#description' => $field['widget']['emthumb_description'] ? t('@description', array(
      '@description' => $field['widget']['emthumb_description'],
    )) : t('If you upload a custom thumbnail, then this will be displayed when the @field thumbnail is called for, overriding any automatic thumbnails by custom providers.', array(
      '@field' => $field['widget']['label'],
    )),
    '#tree' => FALSE,
    '#weight' => 9,
  );
  $form['upload'] = array(
    '#type' => 'button',
    '#value' => t('Upload'),
    '#name' => 'emthumb_' . $fieldname . '_op',
    '#attributes' => array(
      'id' => $fieldname . '-attach-button',
    ),
    '#tree' => FALSE,
    '#weight' => 10,
  );

  // Store the file data object to be carried on.
  $file = $items['data']['emthumb'];

  //   drupal_set_message('file:<pre>' . print_r($file) . '</pre>');
  //   print_r($node);
  if ($file['filepath'] && !$file['flags']['delete']) {
    $form['emthumb'] = array(
      '#theme' => 'emthumb_edit_image_row',
    );
    $form['emthumb']['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']['emimport_image_path'])) : $file['filepath'];

    // drupal_set_message('filename: ' . $filename);
    $form['emthumb']['preview'] = array(
      '#type' => 'markup',
      '#value' => theme('emthumb_image', $file, $file['emthumb_alt'], $file['emthumb_title'], array(
        'width' => '150',
      ), FALSE),
    );

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

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

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

    // Special handling for single value fields
    if (!$field['multiple']) {
      $form['emthumb']['replace'] = array(
        '#type' => 'markup',
        '#value' => t('If a new custom thumbnail is chosen, the current custom thumbnail will be replaced upon submitting the form.'),
      );
    }
  }
  elseif ($file['filepath'] && $file['flags']['delete']) {
    $form['emthumb']['flags']['delete'] = array(
      '#type' => 'hidden',
      // A value type will not persist here, must be hidden
      '#value' => $file['flags']['delete'],
    );
  }
  return $form;
}