t('IMCE file browser'), 'label' => t('File browser'), 'description' => t('Select a file to use from a file browser.'), 'process' => 'filefield_source_imce_process', 'value' => 'filefield_source_imce_value', 'weight' => -1, 'file' => 'sources/imce.inc', ); return $source; } /** * Implements hook_menu(). */ function filefield_source_imce_menu() { $items = array(); $items['file/imce/%/%/%'] = array( 'page callback' => 'filefield_source_imce_page', 'page arguments' => array(2, 3, 4), 'access callback' => '_filefield_sources_field_access', 'access arguments' => array(2, 3, 4), 'file' => 'sources/imce.inc', 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_theme(). */ function filefield_source_imce_theme() { return array( 'filefield_source_imce_element' => array( 'render element' => 'element', 'file' => 'sources/imce.inc', ), ); } /** * Implements hook_filefield_source_settings(). */ function filefield_source_imce_settings($op, $instance) { $return = array(); if ($op == 'form') { $settings = $instance['widget']['settings']['filefield_sources']; $return['source_imce'] = array( '#title' => t('IMCE file browser settings'), '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#access' => module_exists('imce'), ); $return['source_imce']['imce_mode'] = array( '#type' => 'radios', '#title' => t('File browser mode'), '#options' => array( 0 => t('Restricted: Users can only browse the field directory. No file operations are allowed.'), 1 => t('Full: Browsable directories are defined by IMCE configuration profiles. File operations are allowed.', array('!imce-admin-url' => url('admin/config/media/imce'))), ), '#default_value' => isset($settings['source_imce']['imce_mode']) ? $settings['source_imce']['imce_mode'] : 0, ); } elseif ($op == 'save') { $return['source_imce']['imce_mode'] = 0; } return $return; } /** * A #process callback to extend the filefield_widget element type. */ function filefield_source_imce_process($element, &$form_state, $form) { $instance = field_widget_instance($element, $form_state); $element['filefield_imce'] = array( '#weight' => 100.5, '#theme' => 'filefield_source_imce_element', '#filefield_source' => TRUE, // Required for proper theming. '#description' => filefield_sources_element_validation_help($element['#upload_validators']), ); $filepath_id = $element['#id'] . '-imce-path'; $display_id = $element['#id'] . '-imce-display'; $select_id = $element['#id'] . '-imce-select'; $element['filefield_imce']['file_path'] = array( // IE doesn't support onchange events for hidden fields, so we use a // textfield and hide it from display. '#type' => 'textfield', '#value' => '', '#attributes' => array( 'id' => $filepath_id, 'onblur' => "if (this.value.length > 0) { jQuery('#$select_id').triggerHandler('mousedown'); }", 'style' => 'position:absolute; left: -9999em', ), ); $imce_function = 'window.open(\'' . url('file/imce/' . $element['#entity_type'] . '/' . $element['#bundle'] . '/' . $element['#field_name'], array('query' => array('app' => $instance['label'] . '|url@' . $filepath_id))) . '\', \'\', \'width=760,height=560,resizable=1\'); return false;'; $element['filefield_imce']['display_path'] = array( '#type' => 'markup', '#markup' => '' . t('No file selected') . ' (' . t('browse') . ')', ); $element['filefield_imce']['select'] = array( '#name' => implode('_', $element['#array_parents']) . '_imce_select', '#type' => 'submit', '#value' => t('Select'), '#validate' => array(), '#submit' => array('filefield_sources_field_submit'), '#limit_validation_errors' => array($element['#parents']), '#name' => $element['#name'] . '[filefield_imce][button]', '#id' => $select_id, '#attributes' => array('style' => 'display: none;'), '#ajax' => array( 'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'], 'wrapper' => $element['upload_button']['#ajax']['wrapper'], 'method' => 'replace', 'effect' => 'fade', ), ); return $element; } /** * A #filefield_value_callback function. */ function filefield_source_imce_value($element, &$item) { if (isset($item['filefield_imce']['file_path']) && $item['filefield_imce']['file_path'] != '') { $field = field_info_field($element['#field_name']); $scheme = $field['settings']['uri_scheme']; $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); $file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper->getDirectoryPath(); $uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_imce']['file_path']); // Resolve the file path to an FID. $fid = db_select('file_managed', 'f') ->condition('uri', rawurldecode($uri)) ->fields('f', array('fid')) ->execute() ->fetchField(); if ($fid) { $file = file_load($fid); if (filefield_sources_element_validate($element, $file)) { $item = array_merge($item, (array) $file); } } else { form_error($element, t('The selected file could not be used because the file does not exist in the database.')); } // No matter what happens, clear the value from the file path field. $item['filefield_imce']['file_path'] = ''; } } /** * Theme the output of the autocomplete field. */ function theme_filefield_source_imce_element($variables) { $element = $variables['element']; $output = drupal_render_children($element);; return '