Date Difference functionality


(Sean) #1

Matrix Version: v5.5.6.10

Hi all

I’m trying to find a way to get the difference between 2 dates into a metadata field and sort an asset listing by it but even though I can get the correct result the asset list won’t sort it correctly.

The reason for this functionality is to list Event assets and News assets in the same asset listing and sort them by the difference in time between now and when the news item was published or the start date of the event. ie. show the item dated closest to right now regardless of asset type.
I know I could just have a metadata field and manually update it each time a new asset is created but I’d prefer to automate the process by pulling in the required date using keywords.

I have a simplified test setup with assets as below. Dates are in the metadata field config.date_diff. Asset list is set to sort on this field in ascending order (‘closest to now’ to ‘furthest from now’).

Assets
News 1
1/1/2020

News 2
1/1/2021

News 3
4/8/2021

When the config.date_diff metadata field is just:
%metadata_field_dcterms.date^date_format:U^subtract:{globals_date_U}^abs%

The result is as expected (News 1: 50258682, News 2: 18636256, News 3: 56672) and the asset list sorts as expected (News 3, News 2, News 1).

But as soon as I try to add any conditionals so that News assets and Event assets use different date sources I get unexpected results. Sometimes the values seem correct but the sort still isn’t correct and other times the values returned are just whacky.

This is ideally where I want to end up with the Metadata field:

%begin_asset_type^eq:News Item%
%metadata_field_dcterms.date^date_format:U^subtract:{globals_date_U}^abs%
%end_asset_type%

%begin_asset_type^neq:News Item%
%asset_attribute_start_date^date_format:U^subtract:{globals_date_U}^abs%
%end_asset_type%

Note: the only difference is the value I pull the date from.

Any thoughts on this or other possible approaches?

Cheers
Sean


(Sean) #2

Well, I’ve just tried a different tactic.

I have a metadata field that applies only to News Items and another one that applies only to Events but named them both the same (news_events.date).

In the one applied to News Item assets I have
%metadata_field_dcterms.date^date_format:U^subtract:{globals_date_U}^abs%

and in the one applied to Event assests I have
%asset_attribute_start_date^date_format:U^subtract:{globals_date_U}^abs%

Then in the asset listing I just sort by the metadata field news_events.date.

Of course Matrix complains about duplicate metadata field names but it seems to be working (need to do more testing though).
I’d probably prefer a solution that doesn’t come with backend warnings and also not sure if this might cause other issues or might break at some point or under some circumstances. This might be plan B for now.


(Bart Banda) #3

your second tactic shouldn’t throw warnings if the fields are part of separate schemas and those schemas are not both applied to the same asset, or are they?

Have you tried this sort of single keyword for the single shared metadata text field?

%asset_attribute_start_date^emprty:metadata_field_dcterms.date^date_format:U^subtract:{globals_date_U}^abs%

That way the metadata_field_dcterms.date will only be used if the start date keyword is empty, which I think should be if you try and render it against assets that don’t have it.

Another potentially cleaner option could be to use a trigger (or 2 triggers) that updates that metadata field automatically and differently based on whether it’s a new item or event.


(Sean) #4

Thanks Bart

I have the fields in the same schema but in separate ‘Sections’. Those sections are then only applied to the relevant asset type. As I said this is working but throwing warnings. I guess I could put them into totally separate Schemas though.
Do you think the current way I have it is likely to cause any actual issues though?

Also, that keyword might actually do the trick as it looks like what I need but I wasn’t quite sure how to write it in that format so I just kept getting errors when I tried.
I’ll try it today when I get a chance and report back.

Cheers


(Sean) #5

I tried the single keyword but it had issues.
I did a little troubleshooting and this part works
%asset_attribute_start_date^empty:metadata_field_dcterms.date%

but as soon as I add a modifier like below only the asset_attribute_start_date part works for assets that have that field (Calendar Events) but all other assets return a 0 value.

Not sure why.

Still curious to know if you think how I’ve set up the metadata fields could cause an issue or if the warning is just because matrix only checks for duplicates based on the schema being applied despite it not actually applying to an asset due to asset type restrictions.
Feels a little dirty having the warnings show up on every asset with the schema applied but it seems to work without issue.


(Bart Banda) #6

Thanks Sean,

Yeah I’d probably try and avoid it, it could have unforeseen consequences with other functionality, hence the warning. I’d def recommend putting them into a separate schema that is specific for a certain asset type if you need to keep the metadata field name exactly the same.

Interesting. Maybe you could then do a condition on ^eq:0 and print a different value based on the true/false result of that?


(Sean) #7

Thanks Bart

I couldn’t get the keyword to work still but have refactored the Schema so that there are no duplicate metadata fields.
I think this is the simplest way forward and is all working as intended.

Cheers