2

I need to automatically set the expire date of a new entry (only) from a specific section.

I know that it's possible to do this via the frontend twig templates, however I need the user to have the flexibility to change the date from the default that's set if they want to.

So when they create a new entry it'll have an expiration date of today + 3 months, but if they wanted they could change this date to next Wednesday or whatever they wanted.

Thanks for your help

dave_agilepixel
  • 309
  • 1
  • 10

2 Answers2

3

If you haven't solved it yet with a custom solution, have a look at my Auto Expire plugin. You would add a new expiration rule with the expiration date set to:

{ 'now'|date_modify('+3 month 5am')|date('c') }

or

{ postDate|date_modify('+3 month 5am')|date('c') }

and make sure the option to allow user changes is checked.

carlcs
  • 36,220
  • 5
  • 62
  • 139
2

Sounds like the work of a plugin! Unfortunately, there is no built in Entry API Event for setting anything when a new entry is created.

You could use entries.onBeforeSaveEntry to set $entry->expiryDate to three months from "now", but this wouldn't show until the user saved and returned to the entry. Use a function like this in your plugin's main class file.

function init()
{
    craft()->on('entries.saveEntry', function(Event $event) {
        $entry = $event->params['entry'];
        $now = strtotime(date('Y-m-d H:i:s'));
        $entry->expiryDate = strtotime("+3 months", $now);
        $save = craft()->entries->saveEntry($entry);
    });
}

Hope this helps!

Aaron Berkowitz
  • 3,819
  • 10
  • 20