Lightweight Autosave for Drupal with Garlic.js

Out of the box, Drupal 7 does not have autosave capabilities. The complexity of the kinds of content you can create is probably the reason for this, but that doesn't change the fact that it's a bummer. There are efforts underway to change this for Drupal 8; whether they will actually make it in is another question.

Meantime, there is help, and it comes from a JavaScript library called Garlic (how could I not love this?!). Garlic uses HTML5 local storage, supported by all browsers these days, to cache what you're writing as you write it. I like this solution because, unlike WordPress, it doesn't create a bajillion post revisions. The values are stored by your browser until your submit the form, at which point they're cleared out.

There is a Garlic module for Drupal that offers very basic integration, and there's an issue in the Garlic queue to help improve the module by backporting some of the Drupal 8 effort back to Drupal 7. I took Wim Leers' latest Drupal 8 patch and got started on making this work in Drupal 7. The main thing is simplifying Garlic's way of figuring out which form elements to cache so it's more Drupal-centric, and a couple other touches. I definitely recommend grabbing my patch from that issue and using it. It needs testing, but is working for me so far! You'll also need jQuery Update so you can use jQuery 1.7 or greater.

Currently, to enable this on a form, you need to add some code to your theme or a module, just a simple form_alter. For example, if you want to enable autosave on all your node editing forms, this would work:

<?php
/**
* Implements hook_form_FORMID_alter()
*/
function MYMODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
  // if garlic is available, enable autosave
  if (module_exists('garlic')) {
    $form['#attributes']['data-persist'] = 'garlic';
  }
}
?>

I'm probably going to create another patch for Garlic module that lets you check a box in your settings to do the same thing. If you use a WYSIWYG editor, there's a little more work involved that I'll be going into later.

Categories: 

Comments

Alex Weber

Awesome! Thanks for sharing! :)

Kieran Mathieson

Niiice! A chocolatey kudo to you.

Kieran

czigor

Autosave module has an issue with a patch to save any form.

Comments are closed on this post to keep spammers at bay. If you want to chime in, please email or send a tweet.