Repository navigation
Assets index: show folder size and file count on folder rows #20321
Unanswered
MattAppleton
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Folder rows in the Assets index have an empty File Size cell. It would be useful if each folder row showed its total size and file count, e.g.
19.2 MB · 7 files, so editors can see at a glance where the weight of a volume is without opening every folder or installing a utility plugin.Why we wanted it
We are tidying a migrated library of about 1,100 images into one folder per entry. Editors kept asking 'how big is this folder?' and 'is this one empty?'. Craft shows file sizes per asset, and the footer count once you open a folder, but nothing on the folder row itself.
Why it can't be done from a plugin today
Folders in the index are rendered as pseudo-assets (
Asset::$isFolder), andAsset::getAttributeHtml()returns''for them beforeEVENT_DEFINE_ATTRIBUTE_HTMLfires (src/elements/Asset.php, around line 2816 in 5.11). So no PHP event can fill the cell. The only route is browser-side.What we built (works, two files, Craft 5.11)
A CP script registered from
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATEwatches for folder chips (.element[data-is-folder][data-folder-id], note they are on the chip, not thetr), posts their ids to a module action, and writes the answer intotd[data-attr="size"]. The action runs one aggregate query per folder (COUNT(*),SUM(size)over the folder and its subfolders, trashed excluded). Measured: 0.3 ms per folder, 34 ms for a page of 100 folders. It runs after the index has rendered, so nothing blocks, and pages without folder rows make no request.Module code (Craft 5.11): the two methods, plus the web controller
Registered from the module's
init()on CP requests only ($this->registerFolderStats()).controllers/FoldersController.php:Screenshot: see below.
Suggestion
Either of these would make it native:
getAttributeHtml()reachEVENT_DEFINE_ATTRIBUTE_HTMLfor folder pseudo-assets (one-line change), so plugins and modules can fill folder cells.sizeon folder pseudo-assets inAsset::indexElements()with an aggregate query, with the count in thetitleattribute, perhaps behind a volume setting since large volumes with thousands of folders would pay for it on every listing.Happy to turn the module into a PR for option 1 if that is the preferred direction.
All reactions