You are here

function filefield_sources_field_process in FileField Sources 6

Same name and namespace in other branches
  1. 8 filefield_sources.module \filefield_sources_field_process()
  2. 7 filefield_sources.module \filefield_sources_field_process()

A #process callback to extend the filefield_widget element type.

Add the central JavaScript and CSS files that allow switching between different sources. Third-party modules can also add to the list of sources by implementing hook_filefield_sources_info().

1 string reference to 'filefield_sources_field_process'
filefield_sources_elements in ./filefield_sources.module
Implements hook_elements().

File

./filefield_sources.module, line 124
Extend FileField to allow files from multiple sources.

Code

function filefield_sources_field_process($element, $edit, &$form_state, $form) {
  static $js_added;

  // If not a recognized field instance, do not process.
  if (!isset($element['#field_name']) || !($field = content_fields($element['#field_name'], $element['#type_name']))) {
    return $element;
  }

  // Do all processing as needed by each source.
  $sources = filefield_sources_info();
  $enabled_sources = isset($field['widget']['filefield_sources']) ? $field['widget']['filefield_sources'] : array();
  foreach ($sources as $source_name => $source) {
    if (empty($enabled_sources[$source_name])) {
      unset($sources[$source_name]);
    }
    elseif (isset($source['process'])) {
      $function = $source['process'];
      $element = $function($element, $edit, $form_state, $form);
    }
  }

  // Exit out if not adding any sources.
  if (empty($sources)) {
    return $element;
  }

  // Add basic JS and CSS.
  $path = drupal_get_path('module', 'filefield_sources');
  drupal_add_css($path . '/filefield_sources.css');
  drupal_add_js($path . '/filefield_sources.js');

  // Check the element for hint text that might need to be added.
  foreach (element_children($element) as $key) {
    if (isset($element[$key]['#filefield_sources_hint_text']) && !isset($js_added[$key])) {
      $type = str_replace('filefield_', '', $key);
      drupal_add_js(array(
        'fileFieldSources' => array(
          $type => array(
            'hintText' => $element[$key]['#filefield_sources_hint_text'],
          ),
        ),
      ), 'setting');
      $js_added[$key] = TRUE;
    }
  }

  // Add the list of sources to the element for toggling between sources.
  if (empty($element['fid']['#value'])) {
    $element['filefield_sources_list'] = array(
      '#type' => 'markup',
      '#value' => theme('filefield_sources_list', $element, $sources),
      '#weight' => -20,
    );
  }
  return $element;
}