You are here

function emthumb_emfield_widget_extra in Embedded Media Field 5

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

when editing a node with an emfield, this will add our custom thumbnail upload form if allowed.

File

contrib/emthumb/emthumb.module, line 242

Code

function emthumb_emfield_widget_extra($op, $node, $field, $items) {
  $fieldname = $field['field_name'];
  $content_type = $field['type_name'];
  switch ($op) {
    case 'prepare form values':

      // clean up the session if we weren't posted.
      if (!count($_POST)) {
        emthumb_clear_session();
      }

      // Attach new files
      if ($file = file_check_upload($fieldname . '_upload')) {
        $file = (array) $file;
        if (strpos($file['filemime'], 'image') !== FALSE) {
          $file = _emthumb_scale_image($file, $field['widget']['emthumb_max_resolution']);

          // Create the filepath for the image preview
          $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['emimport_image_path']));
          if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
            if (strpos($filepath, file_directory_path()) !== FALSE) {
              $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/');
            }
            $filepath = 'system/files/' . $filepath;
          }
          $file['fid'] = 'upload';
          $file['preview'] = $filepath;

          // If a single field, mark any other images for deletion and delete files in session
          if (!$field['multiple']) {
            if (is_array($items)) {
              foreach ($items as $delta => $session_file) {
                $items[$delta]['data']['emthumb']['flags']['delete'] = TRUE;
              }
            }
            emthumb_clear_field_session($fieldname);
          }

          // Add the file to the session
          $file_id = count($items) + count($_SESSION['emthumb'][$fieldname]);
          $_SESSION['emthumb'][$fieldname][$file_id] = $file;
        }
      }

      // Load files from preview state. before committing actions.
      if (is_array($_SESSION['emthumb'][$fieldname]) && count($_SESSION['emthumb'][$fieldname])) {
        foreach ($_SESSION['emthumb'][$fieldname] as $delta => $file) {
          $items[$delta - 1]['data']['emthumb'] = $file;

          //          $items[$delta-1] = array_merge($items[$delta-1], $file);
          //           $items[$delta-1] = $file;
        }
      }
      break;
    case 'form':
      if ($field['widget']['emthumb']) {
        $form = _emthumb_widget_form($node, $field, $items);
        return $form;
      }
      break;
  }
}