array('id' => $id . '-imce-filefield-fid'), '#type' => 'hidden', '#value' => '', ); $element['imce_filefield_submit'] = array( '#type' => 'submit', '#value' => t('Select'), '#validate' => array(), '#submit' => array('imce_filefield_field_submit'), '#limit_validation_errors' => array(array_slice($element['#parents'], 0, -1)), '#name' => $element['#name'] . '[imce_filefield_submit]', '#id' => $id . '-imce-filefield-submit', '#attributes' => array('style' => 'display: none;'), '#ajax' => array( 'path' => $ajax_path, 'wrapper' => $ajax_wrapper, 'method' => 'replace', 'effect' => 'fade', ), ); return $element; } /** * Submit handler for the IMCE widget. */ function imce_filefield_field_submit(&$form, &$form_state) { file_field_widget_submit($form, $form_state); $form_state['rebuild'] = TRUE; } /** * Field value callback. */ function _imce_filefield_field_value($element, &$item, &$form_state) { if ($file = file_load($item['imce_filefield_fid'])) { if (imce_filefield_element_validate($element, $file)) { $item = array_merge($item, (array) $file); } } else { form_error($element, t('The referenced file could not be used because the file does not exist in the database.')); } $item['imce_filefield_fid'] = ''; } /** * Pre render callback. Add page elements. */ function _imce_filefield_field_pre_render(&$element) { imce_filefield_jscss(); $jset['imce_filefield']['fields'][preg_replace('/\-upload$/', '', $element['#id'])] = array('path' => $element['#entity_type'] . '/' . $element['#bundle'] . '/' . $element['#field_name']); drupal_add_js($jset, 'setting'); if (!empty($element['#value']['fid'])) { $element['imce_filefield_fid']['#access'] = FALSE; $element['imce_filefield_submit']['#access'] = FALSE; } } /** * Validate a file with element validators. */ function imce_filefield_element_validate($element, $file) { $errors = array(); // Check download access first if (file_uri_scheme($file->uri) !== 'public' && !imce_filefield_download_access($file)) { form_error($element, t('You do not have permission to use the selected file.')); return FALSE; } // Validate. foreach ($element['#upload_validators'] as $function => $args) { array_unshift($args, NULL); $args[0] = &$file; $errors = array_merge($errors, call_user_func_array($function, $args)); } // Set errors. if (!empty($errors)) { $message = isset($errors[1]) ? '