You are here

function emthumb_widget_element_process in Embedded Media Field 6

Same name and namespace in other branches
  1. 6.3 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 95
Allows for custom thumbnail overrides to Embedded Media Field.

Code

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

  // Get our file
  $file = array();
  if ($form_state['values'] && $form_state['values']['nid'] && ($node = node_load($form_state['values']['nid']))) {
    $field_data = $node->{$fieldname}[$delta];
    if ($node->{$fieldname} && !empty($field_data['data']['emthumb'])) {

      // Get saved file if we have it.
      $file = $field_data['data']['emthumb'];
    }
  }
  if (!empty($form_state['storage']) && isset($form_state['storage']['emthumb']) && isset($form_state['storage']['emthumb'][$upload_op])) {

    // Get uploaded file if we have it. Merge it with existing file to get alt text
    $file = array_merge($file, $form_state['storage']['emthumb'][$upload_op]);
  }

  // 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[$filefield_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['upload'] = array(
      '#type' => 'submit',
      '#value' => t('Upload'),
      '#name' => $upload_op,
      '#attributes' => array(
        'id' => $fieldname . '-attach-button',
      ),
      '#tree' => FALSE,
      '#submit' => array(
        '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']) {
        $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;
}