31st October 2012

Coder Lounge part 2

John Ennew
Technical Director

Coder lounge is our monthly code sprint into Drupal core which is now into it's second month. This month, 5 of us continued on the javascript theme armed only with pizza, Dr Pepper and our eclectic mix of programming IDEs. Since last month we have received a lot of feedback on the form data loss issues in Drupal. Now the basic approach has been vetted and validated via community review we could smooth the rough edges and improve the design patterns within it. This issue is all about what happens when a content administrator has filled in a large Drupal form then accidentally clicks off it or closes the browser without clicking save. It happens enough that when we hear the cry of GREAT ODIN'S RAVEN! WHY ME? we know the client is referring to this issue. What needs to happen is that the forms last state is recalled locally until the user submits the data back to the server. For this we are using HTML 5's localstorage mechanism so will only work for users with IE8+.

It is not always appropriate to save a forms data locally, for example, settings forms. So we wanted to be able to give developers the ability to specify that a forms data should be locally stored via the formapi. For this we created the following new extension to the formapi: #autosave Used by: form Description: Use to describe the local autosaving of a form Values: An array containing: '#enabled' => TRUE, // Set to TRUE to enable autosaving. '#presave' => TRUE, // Set to TRUE to save as soon as the form loads. This is set automatically in the case that the form did not save after a submit (e.g. on validation error or preview) '#changed' => 3424324324, // A timestamp from the server on the latest version of the form. If this is greater than the local copy the local copy is discarded, someone has overwritten you. Usage example (NodeFormController.php): // Let the node form use localstorage javascript mechanism. $form['#autosave'] = array( '#enabled' => TRUE, '#changed' => isset($node->changed) ? $node->changed : NULL, ); We also had a good look at the excellent Aloha in core initiative, got it running and started looking into some of the open issues. This is a really exciting development in Drupal 8 and will make Drupal much more user friendly once these issues are ironed out.