How to access entities' "natural sort order" in Rules/Buttons/scripts?

Entities can be (re)ordered manually via dragging (i.e. in a Card or List view).
How can such entities’ relative order be determined or set in a Rule/Button/script?

Hi, Matt!
While we’re thinking about the answer, would be glad to hear more details about your use case, really curious about it))))

Example use case:

I have a hierarchical DB of “web pages”. I manually order (by dragging) these to define the desired order of the pages within the website navigation menu.

I want to create a Button that will read this “natural sort order” and generate a bash function to effect this ordering within the website’s WordPress menu (e.g., wp cli commands`).

If you just need to read the sort order, then it is available via a (hidden) field called ‘Rank’ which is a number that determines the ordering (loweest number is highest up the list).

const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Rank']);

However the numbers are not simple, single-digit integers. For example, my 5 tasks have the following values:
image

I don’t know how useful this is :thinking:

1 Like

I think that would work fine - thanks!

Does the API also allow us to set a new value for Rank?

Hello. Yes, api allows that. You should be a bit careful though.

For example if you have an entity with rank 1000 and to insert entity after it you will set rank 1001 to new one, then on UI you will get rank-recalculation process started right after d&d between those cards, as 1 is the minimum precision we have. Rank recalculation will prevent you from d&d cards until it is finished. It will take some time depending on the number of entities in your workspace.

On UI when inserting a card between two, we just generate rank as the middle value in between of their ranks.

And what is your case here? Could you please describe it a bit…

I have a DB of “web pages”, a self-referencing hierarchy of parent and child page entities.
Essentially I want to show a Table view of these pages, ordered like a Hierarchical List view.
The script would sort the pages into their correct order as they would appear in a Menu on the website.

Like this:

Of course the better solution would be to implement such a hierarchical sort in Table view itself.

List view is not ideal because it does not align the columns, nor allow editing like Table view.

If you don’t cross-prioritize those “web pages” with entities from other databases, I think it is totally ok to set ranks programmatically. I suggest to use 1000000 as a step between entities ranks, at least we use this value on UI. So you may take Number.MIN_SAFE_INTEGER as a beginning and add taht step each time.

Will require some javascript to complete your task of course…

Looking at your screen grab, wouldn’t sorting by Path achieve what you want?

In this case, if everything is wanted in alphabetical order, yes. But the more general question is how a script can read or change the ordering of entities that may have been manually reordered (as page order within a menu usually is).

Yep, if you need to read the manually set order, then using the API to access Rank is the way to go.
FWIW we are planning to make improvements to Table View so that it combines some of the best bits of List View whilst retaining the classic features (like column alignment) but I have no idea of the ETA I’m afraid.

2 Likes