Searching and Sorting with the FileList Object

Concrete5 provides the Concrete\Core\File\FileList object to make it easy for developers to query concrete5 for a list of files based on different criteria. Any time you want to sort, search or otherwise filter a list of files you should use this object, as it checks permissions and provides a nice API to what is a fairly complex table structure underneath.

$list = new \Concrete\Core\File\FileList();

By default, this list will include every file in your site, since it's not filtered in any way. It will only include files that the current user can see, based on permissions.

$files = $list->getResults();

This is an array of all the files in the file manager, with no limit. Each entry is just a \Concrete\Core\File\File object.

Basic Filtering Examples

Filter by File Type. File Type is a class constant on the \Concrete\Core\File\Type\Type class. Available constants include:

\Concrete\Core\File\Type\Type::T_IMAGE = 1

\Concrete\Core\File\Type\Type::T_TEXT = 1

\Concrete\Core\File\Type\Type::T_AUDIO = 1

\Concrete\Core\File\Type\Type::T_DOCUMENT = 1

\Concrete\Core\File\Type\Type::T_APPLICATION = 1

\Concrete\Core\File\Type\Type::T_UNKNOWN = 1

$list->filterByType(\Concrete\Core\File\Type\Type::T_IMAGE);

Filter by Extension

$list->filterByExtension('png');

Filter By Keywords

$list->filterByKeywords('foobar');

Filtering by keywords is a simple like search against the filename, description, title, tags, username of uploader, as well as the textual representation of any file attributes that have been marked as "included in search index."

$list->filterBySize(1024, 2048); // Only includes files that are between 1MB and 2MB in Size

Filter by Attribute

Filtering By Attribute is Easier

$list->filterByAttribute('width', 200, '>='); // Only include files where "width" is 200 or greater.

Sorting

Sort by filename is ascending alphabetical order.

$list->sortByFilenameAscending();

Sort by File Set Display Order. (Note: in order to use this method, you must include a single file set in your filter methods.)

$list->sortByFileSetDisplayOrder();

Permissions

By default, permissions will be checked for every file retrieved in the result set. Ignoring permissions for the logged-in user is easy:

$list->ignorePermissions();

Custom Queries

The FileList class has been completely rewritten in concrete5 version 7 as reliant on the Doctrine DBAL QueryBuilder. You can grab the underlying QueryBuilder object in order to operate on it directly from any FileList object:

$query = $list->getQueryObject();

Subclassing

Pagination

Once you have filtered your FileList object, you can use getResults() to get the list of File objects. Many times, however, you'll want to retrieve just few results at a time. For this, you'll want to use the Pagination object.

$pagination = $list->getPagination();

With Permissions

If your File list is honoring permissions, the $pagination object will be an instance of the Concrete\Core\Search\Pagination\PermissionablePagination object. This means that the entire result set (up to 1000) will be loaded and then segmented, with the permissions checker run against it.