You are here

function filefield_widget in FileField 5

Same name and namespace in other branches
  1. 5.2 filefield.module \filefield_widget()
  2. 6.3 filefield.module \filefield_widget()
  3. 6.2 filefield.widget.inc \filefield_widget()

Implementation of hook_widget().

File

./filefield.module, line 261
Defines a file field type.

Code

function filefield_widget($op, $node, $field, &$node_field) {
  $fieldname = $field['field_name'];
  switch ($op) {
    case 'prepare form values':

      // @todo split this into its own function. determine if we can make it a form element.
      if (!count($_POST)) {
        filefield_clear_session();
      }

      // Attach new files
      if ($file = file_check_upload($fieldname . '_upload')) {
        $file = (array) $file;

        // test allowed extensions. We do this when the file is uploaded, rather than waiting for the
        // field itseld to reach op==validate.
        $ext = array_pop(explode('.', $file['filename']));
        $allowed_extensions = array_unique(explode(' ', trim($field['widget']['file_extensions'])));
        if (in_array($ext, $allowed_extensions)) {

          // do mime/extension specific handling here.
        }
        else {
          form_set_error($field['field_name'] . '_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions', array(
            '%ext' => $ext,
            '%allowed_extensions' => $field['widget']['file_extensions'],
          )));
          return FALSE;
        }
        $filepath = file_create_filename($file['filename'], file_create_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;
        }

        // prepare file array.
        $file['fid'] = 'upload';
        $file['preview'] = $filepath;

        // if this is a single value filefield mark any other images for deletion.
        if (!$field['multiple']) {
          if (is_array($node_field)) {
            foreach ($node_field as $delta => $session_file) {
              $node_field[$delta]['remove'] = TRUE;
            }
          }

          // Remove old temporary file from session.
          filefield_clear_field_session($fieldname);
        }

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

      // Load files from preview state. before committing actions.
      if (is_array($_SESSION['filefield'][$fieldname]) && count($_SESSION['filefield'][$fieldname])) {
        foreach ($_SESSION['filefield'][$fieldname] as $delta => $file) {
          $node_field[] = $file;
        }
      }
      break;
    case 'form':
      $form = _filefield_widget_form($node, $field, $node_field);
      return $form;
    case 'validate':
      if ($field['required']) {
        if (!count($node_field)) {
          form_set_error($fieldname, $field['widget']['label'] . ' is required.');
        }
      }
      return;
  }
}