When users fill values in node form input fields I set cookies of those values with help of javascript code, example :
var date = new Date();
date.setTime(date.getTime() + (60 * 60 * 1000));
$('#edit-field-topic-und').on('change', function() {
var section = $(this).find("option:selected").val();
console.log(section);
if(section){
$.cookie("section", section, { expires: date });
}
})
$('#edit-field-topic2-und').on("change", function () {
var topic = $(this).val();
console.log(topic);
if(topic){
$.cookie("topic", topic, { expires: date });
}
});
$('#edit-field-language-und').on('change', function() {
var language = $(this).find("option:selected").val();
console.log(language);
if(language){
$.cookie("language", language, { expires: date });
}
})
If user does not submits from and next time come to node add form page, I set default values in from with help of form alter:
case 'link_node_form':
/*show default values from cookie*/
if (empty($form['nid']['#value'])) {
if(isset($_COOKIE['section'])) {
$form['field_topic']['und']['#default_value'] = $_COOKIE['section'];
}
if(isset($_COOKIE['topic'])) {
$form['field_topic2']['und']['#default_value'] = $_COOKIE['topic'] ;
}
if(isset($_COOKIE['language'])) {
$form['field_language']['und']['#default_value'] = $_COOKIE['language'];
}
// clear cookies after from submit
$form['#submit'][] = 'custom_overrides_link_node_form_submit';
}
Now when user submit form and node got created successfully cookie is not clearing or the cookies got values of last submitted node.
Please suggest how to clear / unset cookies when form get submitted.
function custom_overrides_link_node_form_submit($form, &$form_state) {
if(isset($_COOKIE['section'])) {
unset($_COOKIE['section']);
}
if(isset($_COOKIE['topic'])) {
unset($_COOKIE['topic']);
}
if(isset($_COOKIE['language'])) {
unset($_COOKIE['language']);
}
}