You are here

function _filefield_source_attach_options in FileField Sources 7

Same name and namespace in other branches
  1. 6 sources/attach.inc \_filefield_source_attach_options()
1 call to _filefield_source_attach_options()
filefield_source_attach_process in sources/attach.inc
A #process callback to extend the filefield_widget element type.

File

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

Code

function _filefield_source_attach_options($path, $extensions = FALSE) {
  if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
    drupal_set_message(t('Specified file attach path %path must exist or be writable.', array(
      '%path' => $path,
    )), 'error');
    return FALSE;
  }
  $options = array();
  $pattern = !empty($extensions) ? '/\\.(' . strtr($extensions, ' ', '|') . ')$/' : '/.*/';
  $files = file_scan_directory($path, $pattern);
  if (count($files)) {
    $options = array(
      '' => t('-- Select file --'),
    );
    foreach ($files as $file) {
      $options[$file->uri] = str_replace($path . '/', '', $file->uri);
    }
    natcasesort($options);
  }
  return $options;
}