tm2000
April 26, 2024, 4:08pm
1
Hi there
I ave used this snippet on a lot of sites - it’s really useful to show recently added products or posts.
But I tried it on a MODX3 install, and it’s not working.
I assume it’s a syntax issue, as I’ve found a few snippets have broken when moving to MODX3 - any ideas what could be wrong with it?
<?php
/* RecentCheck snippet */
$output = 'non_recent';
$interval = $modx->getOption('interval', $scriptProperties, '2 weeks');
$pd = $modx->getOption('publishedon', $scriptProperties, '');
$cutoffTime = strtotime('now - ' . $interval);
if ($cutoffTime === false) {
$output = '[RecentCheck] Invalid interval';
} else {
$ptime = strtotime($pd);
$currentTime = time();
if ($ptime > $cutoffTime) {
/* Yes, it's recent -- Set the return value */
$output = 'recent';
}
}
return $output;
dejaya
April 26, 2024, 4:20pm
2
Hey … this seems to work OK for me in 3.0.5.
For a resource created today:
[[!recentCheck? &interval=`1 week` &publishedon=`[[*publishedon]]`]]
returns recent
[[!recentCheck? &interval=`0 weeks` &publishedon=`[[*publishedon]]`]]
returns non_recent
What’s happening when you call it?
tm2000
April 26, 2024, 4:58pm
3
Thanks for checking.
Hmm. Mine comes back with non_recent on all posts regardless of their published date.
Strange
bobray
April 27, 2024, 6:06am
4
What does your snippet tag look like?
Are you using it inside getResources or pdoResources?
Here’s something similar , though I haven’t tested it in MODX 3. Mine gets the publishedon
value directly from the resource.
You could try sending the id
in the tag and get it from the resource, though I don’t see any reason why your code shouldn’t work.
tm2000
April 27, 2024, 11:01am
6
Actually I just checked and the call is correct:
[l! recentCheck? &interval=‘4 weeks’ &publishedon=’ [[+publishedon]] 'll
I wonder if the interval setting isn’t recognised with modx3?
bobray
April 27, 2024, 8:50pm
7
That’s definitely not correct (backticks and braces), but maybe those are just typos in the post.
tm2000
April 27, 2024, 9:05pm
8
Sorry. I was on my iPhone and it’s impossible to copy code out of the Modx editor which is frustrating. This is the correct call:
[[!recentCheck? &interval=`4 weeks` &publishedon=`[[+publishedon]]`]]
bobray
April 27, 2024, 9:10pm
9
Looks perfect.
Is this inside an aggregator snippet like getResources? If not, you want [[*publishedon]]
, rather than [[+publishedon]]
.
tm2000
April 27, 2024, 9:13pm
10
Yes it’s in pdoResources.
It’s a “problem” with pdoResources :
[[*publishedon]]
in the resource has the format 2024-04-28 07:56:00
.
[[+publishedon]]
in the pdoResources-template however is a timestamp → 1714290960
.
You have to take that into account in your snippet code, by e.g. not executing strtotime()
when $pd
is already a number.
1 Like
AHA - awesome thanks Harry.
For others with the same issue, I have amended the snippet to this, and it works perfectly.
<?php
/* RecentCheck snippet */
$output = 'non_recent';
$interval = $modx->getOption('interval', $scriptProperties, '2 weeks');
$pd = $modx->getOption('publishedon', $scriptProperties, '');
$cutoffTime = strtotime('now - ' . $interval);
if ($cutoffTime === false) {
$output = '[RecentCheck] Invalid interval';
} else {
$currentTime = time();
if ($pd > $cutoffTime) {
/* Yes, it's recent -- Set the return value */
$output = 'recent';
}
}
return $output;
1 Like