I have the following code in my form, but I'd like to also save the eventDate of the selected entry into a separate date field to enable sorting/selection later.

<label for="eventName">Event
<select id="eventName" name="fields[eventName]">  
  <option value="">select from list</option>
    {% set events = craft.entries.find({section:'events,activities', eventDate : ">= " ~ now.w3cDate() , order:'eventDate'}) %}
    {% if events %}
        {% for entry in events %}                
            <option value="{{ entry.eventDate.format('D j M') }},{{ entry.eventName }}">{{ entry.eventDate.format('D j M') }},{{ entry.eventName }}</option>
        {% endfor %}
    {% endif %}

You could use the Preparse plugin to create a field that is automatically updated with the eventDate value whenever the entry is saved. It wouldn't be a datetime field, but if you stored the timestamp (i.e. {{ dateString|date('U') }}) it'd be easy to use as such.

Another option is to have an hidden input in your form for the eventDate datetime field, and use JavaScript to set its value whenever the <select> input changes (i.e. listen for the change event for the latter, in your JS).

The third option would be to write a plugin which listens to the entries.beforeSaveEntry event in order to set the eventDate value from the dropdown field.

  • Auto Expire plugin allows to copy to a Date/Time field. – carlcs Mar 10 '16 at 0:39

As there was no simple answer that didn't require another plugin or javascript I revised my entry form to collect the date manually. Which is a bit of a pain, as the input format for the date has to be mm/dd/yyyy, which isn't normal in the UK.

  • Don't picture the Preparse plugin as "yet another plugin". It's a tool that actually should be part of every Craft install, so useful. – carlcs Mar 10 '16 at 0:35
  • I'm coming from ExpressionEngine, and have been bitten by addons that are no longer supported and not updated, and so now I can't update the EE version. I don't want that to happen again. Part of Craft's attraction is that almost everything I want is part of the core so updating isn't a problem. – Paul Frost Mar 10 '16 at 15:57
  • Have a look at all the maintainers: github.com/aelvan/Preparse-Field-Craft/commits/master. Brandon is a one of them and he's a big fan of that plugin. It's here to stay, even if Andre should remove it from GitHub, I bet there will be a replacement plugin within a few days. – carlcs Mar 10 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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