You are here

protected static function Attach::getAttachOptions in FileField Sources 8

Get attach options.

Parameters

string $path: Path to scan files.

string $extensions: Path to scan files.

Return value

array List of options.

1 call to Attach::getAttachOptions()
Attach::process in src/Plugin/FilefieldSource/Attach.php
Process callback for file field source plugin.

File

src/Plugin/FilefieldSource/Attach.php, line 241

Class

Attach
A FileField source plugin to allow use of files within a server directory.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

protected static function getAttachOptions($path, $extensions = FALSE) {
  if (!\Drupal::service('file_system')
    ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) {
    \Drupal::messenger()
      ->addError(t('Specified file attach path %path must exist or be writable.', [
      '%path' => $path,
    ]), 'error');
    return FALSE;
  }
  $options = [];
  $pattern = !empty($extensions) ? '/\\.(' . strtr($extensions, ' ', '|') . ')$/' : '/.*/';
  $files = \Drupal::service('file_system')
    ->scanDirectory($path, $pattern);
  if (count($files)) {
    $options = [
      '' => t('-- Select file --'),
    ];
    foreach ($files as $file) {
      $options[$file->uri] = str_replace($path . '/', '', $file->uri);
    }
    natcasesort($options);
  }
  return $options;
}