You are here

function filefield_sources_field_process in FileField Sources 7

Same name and namespace in other branches
  1. 8 filefield_sources.module \filefield_sources_field_process()
  2. 6 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_element_info in ./filefield_sources.module
Implements hook_element_info().

File

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

Code

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

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

  // Do all processing as needed by each source.
  $sources = filefield_sources_info();
  $enabled_sources = _filefield_sources_enabled($instance['widget']['settings']['filefield_sources']);
  $context = array(
    'enabled_sources' => &$enabled_sources,
    'element' => $element,
    'form_state' => $form_state,
  );

  // Allow other modules to alter the sources.
  drupal_alter('filefield_sources_sources', $sources, $context);
  foreach ($sources as $source_name => $source) {
    if (empty($enabled_sources[$source_name])) {
      unset($sources[$source_name]);
    }
    else {
      if (isset($source['process'])) {
        $function = $source['process'];
        $element = $function($element, $form_state, $form);
      }
      if (isset($source['file'])) {
        _filefield_sources_form_include($source['module'], $source['file'], $form_state);
      }
    }
  }
  $element['#filefield_sources'] = $sources;

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

  // Hide default 'upload' type?
  if (!isset($enabled_sources['upload'])) {
    foreach (array(
      'upload_button',
      'upload',
    ) as $field) {
      if (isset($element[$field])) {
        $element[$field]['#access'] = FALSE;
      }
    }
  }

  // Add basic JS and CSS.
  $path = drupal_get_path('module', 'filefield_sources');
  $element['#attached']['css'][] = $path . '/filefield_sources.css';
  $element['#attached']['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;
    }
  }

  // Adjust the AJAX settings so that on upload and remove of any individual
  // file, the entire group of file fields is updated together.
  // Copied directly from file_field_widget_process().
  $field = field_widget_field($element, $form_state);
  if ($field['cardinality'] != 1) {
    $parents = array_slice($element['#array_parents'], 0, -1);
    $new_path = 'file/ajax/' . implode('/', $parents) . '/' . $form['form_build_id']['#value'];
    $field_element = drupal_array_get_nested_value($form, $parents);
    $new_wrapper = $field_element['#id'] . '-ajax-wrapper';
    foreach (element_children($element) as $key) {
      foreach (element_children($element[$key]) as $subkey) {
        if (isset($element[$key][$subkey]['#ajax'])) {
          $element[$key][$subkey]['#ajax']['path'] = $new_path;
          $element[$key][$subkey]['#ajax']['wrapper'] = $new_wrapper;
          $element[$key][$subkey]['#limit_validation_errors'] = array(
            $parents,
          );
        }
      }
    }
  }

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