You are here

function filefield_source_attach_process in FileField Sources 7

Same name and namespace in other branches
  1. 6 sources/attach.inc \filefield_source_attach_process()

A #process callback to extend the filefield_widget element type.

1 string reference to 'filefield_source_attach_process'
filefield_source_attach_info in sources/attach.inc
Implements hook_filefield_source_info().

File

sources/attach.inc, line 122
A FileField extension to allow use of files within a server directory.

Code

function filefield_source_attach_process($element, &$form_state, $form) {
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['widget']['settings']['filefield_sources']['source_attach'];
  $element['filefield_attach'] = array(
    '#weight' => 100.5,
    '#theme' => 'filefield_source_attach_element',
    '#filefield_source' => TRUE,
  );
  $path = _filefield_source_attach_directory($instance);
  $options = _filefield_source_attach_options($path, $instance['settings']['file_extensions']);

  // If we have built this element before, append the list of options that we
  // had previously. This allows files to be deleted after copying them and
  // still be considered a valid option during the validation and submit.
  if (!isset($form_state['triggering_element']) && isset($form_state['filefield_sources'][$instance['field_name']]['attach_options'])) {
    $options = $options + $form_state['filefield_sources'][$instance['field_name']]['attach_options'];
  }
  else {
    $form_state['filefield_sources'][$instance['field_name']]['attach_options'] = $options;
  }
  $description = t('This method may be used to attach files that exceed the file size limit. Files may be attached from the %directory directory on the server, usually uploaded through FTP.', array(
    '%directory' => drupal_realpath($path),
  ));

  // Error messages.
  if ($options === FALSE || empty($settings['path'])) {
    $attach_message = t('A file attach directory could not be located.');
    $attach_description = t('Please check your settings for the %field field.', array(
      '%field' => $instance['label'],
    ));
  }
  elseif (!count($options)) {
    $attach_message = t('There currently are no files to attach.');
    $attach_description = $description;
  }
  if (isset($attach_message)) {
    $element['filefield_attach']['attach_message'] = array(
      '#markup' => $attach_message,
    );
    $element['filefield_attach']['#description'] = $attach_description;
  }
  else {
    $validators = $element['#upload_validators'];
    if (isset($validators['file_validate_size'])) {
      unset($validators['file_validate_size']);
    }
    $description .= '<br />' . filefield_sources_element_validation_help($validators);
    $element['filefield_attach']['filename'] = array(
      '#type' => 'select',
      '#options' => $options,
    );
    $element['filefield_attach']['#description'] = $description;
  }
  $element['filefield_attach']['attach'] = array(
    '#name' => implode('_', $element['#array_parents']) . '_attach',
    '#type' => 'submit',
    '#value' => t('Attach'),
    '#validate' => array(),
    '#submit' => array(
      'filefield_sources_field_submit',
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#ajax' => array(
      'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
      'wrapper' => $element['upload_button']['#ajax']['wrapper'],
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#access' => !isset($attach_message),
  );
  return $element;
}