3

In CP, there is the widget called "Recent Entries". You can pull out entries from your sections, limit a count of entries.

I'm building a plugin that has very similar widget to recent entries widget.

THE GOAL

Filter entries by date range within widget.

I was only able to set a fixed date range within widget's class ...

// To get entries from a specific date range
$criteria->postDate = array('and', '> 2016-06-10 23:59:59', '< 2016-06-30 00:00:00');

1 Answer 1

3

Have a look at the Dashboard Widget section in the Craft docs. It’ll walk you through defining your “Start Date” and “End Date” settings, and how to render the settings template, where you can make use of the dateTimeField Twig macros to render the Date and Time fields.

An important step to make your widget save the values properly to the database. You’ll have to convert them DateTime objects to string before save in the prepSettings(), and convert them back in the getSettingsHtml() before rendering the settings template. There’s this convenient DateTime::createFromString() helper that you should proabably want to use.

Another thing that you’ll probably find useful is the mySqlDateTime() method that prepares a DateTime object for use in criteria model parameters.

Have a look at P&T Store Hours plugin, that shows some of these methods in code context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.