Folder items, sort alphabetically

Hello,

we use Plone to manage our documents. The person who implemented and maintained Plone is no longer employed in the company. I kind of inherited this project.

We use:
Plone 4.3.1 (4306)
CMF 2.2.7
Zope 2.13.20

Basically we have folders, containing subfolders, which contain documents (doc, pdf, ...)

MainFolder
...Subfolder_1
......Subfolder_1_1
......Subfolder_1_2
...Subfolder2
......Subfolder_2_1
.........Document1.doc
.........Document2.doc

Recently it was demanded, that the listed subfolders and documents should be sorted alphabetically.
Using the documentation I couldn't find a solution yet.

What works, is if I am in a folder on the tab "Contents", to arrange items manually. When I change to the tab "View" my arrangement is saved.

If I am in a folder on the tab "Contents" and click "title" or "modified" the items are arranged according to the column. But when I change back to "view" the sorting isn't saved and back to the way it was before.

Am I missing something, or doing something wrong?
Or is there a smarter way to achieve an alphabetically sorting of all folders and in all folders?

Can anyone help me, or point me in the right direction?

Thanks in advance for any help.
Kind Regards
Markus

It is possible to add a "Collection" to a folder.
Then you can set the 'default view' of the folder to the collection (Content Item as Default view).
For a Collection, you can choose criteria, like where to look, what kind of content types. And you can set 'sort on'.

In you case you would add a collection, make the criteria 'in (this) parent folder' and sort on "Title", alphabetically.

Note: this would not sort the folder, it will just list the content alphabetically (which is probably just as good)

1 Like

Hi Espen,

thanks for your reply. I got your solution basically working.

It works great for something like this:

Folder
...File_1
...File_2
...File_3

But regarding folders I have the problem that too much is displayed as a result of the collection.

Folder Setup:

ParentFolder
..SubFolder_1
....SubSubFolder_1
..SubFolder_2
..SubFolder_3

What I would like to see, when selecting the ParentFolder:
SubFolder_1
SubFolder_2
SubFolder_3

What I get, when I add a collection:
search criteria 1 -> Absolute Path = /Plone/ParentFolder/
search criteria 2 -> type = folder

Collection result:
ParentFolder
SubFolder_1
SubFolder_2
SubFolder_3
SubSubFolder_1

Is there a way to adjust search criteria for a better result?
"Search for Folders directly in this path,not below and don't show the ParentFolder" would be the logic I'm looking for.

Kind regards
Markus

Hallo Markus,

if you are not able resolving this request by collections you could maybe try changing this view with javascript?! What View is it you'd like to change? Is it 'List' ('Liste' in DE/AT/CH)?

Hi Jan,

currently it is list (Liste) and table (Tabelle), but I can change it all to list.
Where would I find the javascript, to alter this view?
And is it just a parameter I'd have to change or do I have to write new code?

Thanks.

Markus

Hi Markus,

this should be easy writing /adapting. You could find lots of examples e.g. on stackoverflow. I know it is most probably not best style in Plone doing it like this way:
For not changing templates (what would be easy here: you had a new view 'List Alphabetically' - but after upgrading Plone it could be you had to change this template again) i think it is ok using for example 'Catagorization': Giving this folder a catagory like 'AlphabeticList', then add a js-file in ZMI/portal_skins/custom, register it in ZMI/portal_javascripts (set this to debug and reload page, then uncheck debug-mode again).
So in js check if Catagory contains the above 'AlphabeticList' for this Folder and if so reorder items. Keep in mind : as always there shouldn't be too many item in this folder, espacially if the view will only list first 20 of 100 it'll not work.
On the other hand there are some addons for listing content in other ways, maybu you'll find one bespoken here in this community.

So this is what works for me with list-view:

$( document ).ready(function(){ var $divs = $("div.tileItem"); if ($(".link-category:contains('ListContentAlphabetically')").length) { var alphabeticallyOrderedDivs = $divs.sort(function (a, b) { return $(a).find("a.contenttype-folder").text() > $(b).find("a.contenttype-folder").text(); }); $("#content").append(alphabeticallyOrderedDivs); } });
You will need to adapt here used elemt-id's for to fit the ones used in the theme used.
After adding ListContentAlphabetically as category the list-view-items are sorted alphabetically.
PS: Thanks @tmassman, I'll try sortmyfolder tomorrow, did not know this one.

There's an add-on somewhere with "subtractive criteria" that might help, haven't used it though.

Hi, for Plone 4 you can use https://pypi.python.org/pypi/collective.sortmyfolder/1.0.7.

Is there any sort ('command'/code) avalable for Plone 5.2 ?
( I want to sort content of all my (1000) folders alphabetically

Since you're already familiar with TAL templating; modify the listing view, tabular view and use z3c.jbot to override, or use these as a base to make a new one to suit your needs.

You can update the layout proprty in an import step.

I want to sort the content in the folders (not the visual presentation).

Like here? Query for content sorted by getObjPositionInParent in multi level objects - #5 by maha

To sort the items in a view can be done (for example) like this:

self.context.restrictedTraverse('@@contentlisting')(sort_on='sortable_title', batch=True, b_size=40);

But in my case, I want some python code to sort items in the folder alphabetically.

The main reason for this is that I want a) some users to 'check some documents, and when done: move them and b) see if there i duplicate content (there is a mix of 'number Name' and 'Name Number' in the filenames) and I dont want these to 'move automatically after we change them (as they would in a collection)

I have loads (thousands) of folders, so I would prefer to do it by code.

Just a rough idea using indexes:

  • create a custom index and indexer indexing a string of its own position and parents, like, ROOT/FIRST/SECOND/THIRD (level) which reads like "001-002-001-003"
  • Pros: you can easily sort on this index.
  • Cons: If you move a object in an upper level, you need to reindex all childs using a subscriber.

Maybe an (Extendend)PathIndex could help as well. I would need to dig deeper into code to determine if it is helpful.

Another option could be to create some kind of specialized NavTreeBuilder. Same here, I would need to dig into code. And that one is complex.

I dont think I described it properly (or at least it has been misunderstood):

I dont need to sort 'all items', I just want every folder to reorder its content 'alphabetically', so I want to make a script that walks all the thousand(s) folders and do 'folder.sortMyFolderContent()'.

have you looked at the portal catalog getPositionInParent index? I don't know it well enough to be sure it could work for you, but my approach would be to update this index as you walk your folders. i.e (from memory) make a pass through the folder.objectItems() and sort these, then update the index for each content item. I forget if it's objectItems or objectListing, or maybe a more modern approach using the plone api.

It seems logical to me that this index is then used to display folder_contents. Or perhaps there is a flag on folder_contents which lets you arrange the items alphabetically.

Also, regarding updating the layout in each folder, if the specific layout was never set, your default folder view will be used. You can make one that sorts all items alphabetically and make it the default view for folders in your portal...