You are here

function emthumb_widget_element_process in Embedded Media Field 6.3

Same name and namespace in other branches
  1. 6 contrib/emthumb/emthumb.module \emthumb_widget_element_process()
  2. 6.2 contrib/emthumb/emthumb.module \emthumb_widget_element_process()

Process our emthumb element.

1 string reference to 'emthumb_widget_element_process'
emthumb_elements in contrib/emthumb/emthumb.module
Implementation of hook_elements().

File

contrib/emthumb/emthumb.module, line 130
Allows for custom thumbnail overrides to Embedded Media Field.

Code

function emthumb_widget_element_process($element, $edit, &$form_state) {
  $field = $element['#field'];
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $delta = $element['#delta'];
  $upload_op = 'emthumb_' . $field_name . '_' . $delta . '_upload';
  $emthumb_element_name = $field_name . '_' . $delta . '_file';

  // First grab the file if it's just been uploaded.
  $file = _emthumb_file_upload($form_state, $field_name, $field, $delta, $upload_op, TRUE);

  // If there is no uploaded file, check the form cache to see if it was
  // uploaded earlier, such as from a preview or AHAH.
  // @TODO: Doesn't work with AHAH.
  if (empty($file) && isset($form_state['storage']) && isset($form_state['storage']['emthumb']) && isset($form_state['storage']['emthumb'][$upload_op])) {
    $file = $form_state['storage']['emthumb'][$upload_op];
  }

  // If we still don't have a file, then use the version from the node.
  if (empty($file) && $form_state['values'] && $form_state['values']['nid'] && ($node = node_load($form_state['values']['nid']))) {
    $field_data = $node->{$field_name}[$delta];
    if ($node->{$field_name} && !empty($field_data['data']['emthumb'])) {

      // Get saved file if we have it.
      $file = $field_data['data']['emthumb'];
    }
  }

  // Do not display custom thumb stuff if we don't allow a custom thumb.
  if ($field['widget']['emthumb']) {

    // Separate from tree becase of that silly things won't be
    // displayed if they are a child of '#type' = form issue
    $element[$emthumb_element_name] = array(
      '#type' => 'file',
      '#description' => isset($field['widget']['emthumb_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,
    );
    $element['emthumb-upload'] = array(
      '#type' => 'submit',
      '#value' => t('Upload'),
      '#name' => $upload_op,
      '#attributes' => array(
        'id' => $field_name . '-attach-button',
      ),
      '#tree' => FALSE,
      '#submit' => array(
        'node_form_submit_build_node',
        'emthumb_widget_upload_button_submit',
      ),
      '#weight' => 10,
    );
  }
  if (isset($file) && isset($file['filepath'])) {
    $element['#title'] = t('Replace');
    $element['emthumb'] = array(
      '#theme' => 'emthumb_edit_image_row',
    );
    $element['emthumb']['flags']['delete'] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete'),
      '#description' => t("Checking this field causes the thumbnail to be redownloaded, deleting the current thumbnail."),
      '#default_value' => 0,
    );
    $filename = $file['filepath'];
    $element['emthumb']['preview'] = array(
      '#type' => 'markup',
      '#value' => theme('emthumb_image', $file, $file['emthumb_alt'], $file['emthumb_title'], array(
        'width' => $field['widget']['thumbnail_width'],
        'height' => $field['widget']['thumbnail_height'],
      ), FALSE),
    );
    $element['emthumb']['description'] = array(
      '#type' => 'markup',
      '#value' => '<strong>' . t('Filename:') . ' </strong>' . check_plain($file['filename']),
    );

    // Overwrite with an input field if custom_alt is flagged.
    if ($field['widget']['emthumb_custom_alt']) {
      $element['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,
      );
    }

    // Overwrite with an input field if custom_title is flagged.
    if ($field['widget']['emthumb_custom_title']) {
      $element['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,
      );
    }
    $element['emthumb']['file'] = array(
      '#type' => 'value',
      '#value' => $file,
    );

    // If this was an uploaded file, we need to save it. Otherwise it will be
    // forgotten on node save
    $element['emthumb']['replace'] = array(
      '#type' => 'markup',
      '#value' => $field['widget']['emthumb'] ? t('If a new custom thumbnail is chosen, the current custom thumbnail will be replaced upon submitting the form.') : '',
    );
  }
  else {
    if ($field['widget']['emthumb_store_local_thumbnail']) {
      $element['emthumb']['no_current_thumb'] = array(
        '#type' => 'markup',
        '#value' => t('If possible, a remote thumbnail will be downloaded on the next save.'),
      );
      if ($field['widget']['emthumb'] && emthumb_edit_access($field_name)) {
        $element['emthumb']['no_current_thumb']['#value'] .= ' ' . t("Alternatively, you may specify a custom thumbnail in this field.");
      }
    }
  }
  if (isset($form_state['clicked_button']) && in_array('node_form_submit', $form_state['clicked_button']['#submit']) && isset($form_state['storage']['emthumb'])) {

    // Form is being submitted and we want to empty our storage
    // so we can redirect to wherever was specified
    if (isset($form_state['storage']['emthumb'][$upload_op])) {
      unset($form_state['storage']['emthumb'][$upload_op]);
    }
    if (empty($form_state['storage']['emthumb'])) {
      unset($form_state['storage']['emthumb']);
    }
  }
  return $element;
}