Back to FilelistField class

Method getOptions

protected array
getOptions
()
Method to get the list of files for the field options.
Returns
  • array The field option objects.
Since
  • 1.7.0
Class: FilelistField
Project: Joomla

Method getOptions - Source code

/**
 * Method to get the list of files for the field options.
 * Specify the target directory with a directory attribute
 * Attributes allow an exclude mask and stripping of extensions from file name.
 * Default attribute may optionally be set to null (no file) or -1 (use a default).
 *
 * @return  array  The field option objects.
 *
 * @since   1.7.0
 */
protected function getOptions()
{
    $options = array();
    $path = $this->directory;
    if (!is_dir($path)) {
        $path = JPATH_ROOT . '/' . $path;
    }
    $path = Path::clean($path);
    // Prepend some default options based on field attributes.
    if (!$this->hideNone) {
        $options[] = HTMLHelper::_('select.option', '-1', Text::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)));
    }
    if (!$this->hideDefault) {
        $options[] = HTMLHelper::_('select.option', '', Text::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)));
    }
    // Get a list of files in the search path with the given filter.
    $files = Folder::files($path, $this->fileFilter);
    // Build the options list from the list of files.
    if (\is_array($files)) {
        foreach ($files as $file) {
            // Check to see if the file is in the exclude mask.
            if ($this->exclude) {
                if (preg_match(\chr(1) . $this->exclude . \chr(1), $file)) {
                    continue;
                }
            }
            // If the extension is to be stripped, do it.
            if ($this->stripExt) {
                $file = File::stripExt($file);
            }
            $options[] = HTMLHelper::_('select.option', $file, $file);
        }
    }
    // Merge any additional options in the XML definition.
    $options = array_merge(parent::getOptions(), $options);
    return $options;
}