function autosave_form_alter in Autosave 7.2
Same name and namespace in other branches
- 5.3 autosave.module \autosave_form_alter()
- 5 autosave.module \autosave_form_alter()
- 5.2 autosave.module \autosave_form_alter()
- 6.2 autosave.module \autosave_form_alter()
- 6 autosave.module \autosave_form_alter()
- 7 autosave.module \autosave_form_alter()
Implements hook_form_alter().
File
- ./
autosave.module, line 167 - Does background saves of node being edited.
Code
function autosave_form_alter(&$form, &$form_state, $form_id) {
// Autosaving node forms can be enabled both by adding the form_id at
// admin/config/content/autosave or by checking the 'Enable Autosave'
// checkbox at admin/structure/types/manage/[node:type]. The following
// variable is TRUE if autosaving is enabled on the latter link and we
// are on a node form.
$autosave_node_form = FALSE;
if (!empty($form['#node_edit_form'])) {
$node = $form['#node'];
$autosave_node_form = variable_get('autosave_' . $node->type, 0) && arg(0) != 'admin';
}
$form_ids = explode("\n", variable_get('autosave_form_ids', ''));
foreach ($form_ids as &$formid) {
$formid = trim($formid);
}
if ($autosave_node_form || in_array($form_id, $form_ids)) {
// Remove the autosaved form when submitting it.
array_unshift($form['#submit'], 'autosave_remove_autosaved_form_submit');
if (empty($_POST['autosave_form_path'])) {
$request_path = request_path();
// We store the drupal system paths, not the aliases.
$path = drupal_lookup_path('source', $request_path);
// For already system paths $path is FALSE.
if (!$path) {
$path = $request_path;
}
// Make a note in the form of what the original path is, since when
// submitting the autosaved form to our own callback it will not be
// the same.
$form['autosave_form_path'] = array(
'#type' => 'hidden',
'#value' => $path,
);
// Store form arguments in db only if the form has not been autosaved
// before. The second part of the condition prevents the creation of db
// entries with autosave/restore/ path during autosave restore AJAX.
// Note that 'autosave/restore' might be after a language prefix.
if (!empty($form_state['build_info']['args']) && strpos($path, 'autosave/restore/') === FALSE) {
$key = array(
'form_id' => $form_id,
'uid' => $GLOBALS['user']->uid,
'path' => $path,
);
db_merge('autosaved_forms')
->key($key)
->insertFields(array_merge($key, array(
'timestamp' => 0,
'serialized' => '',
'args' => serialize($form_state['build_info']['args']),
)))
->updateFields($key)
->execute();
}
$settings = array();
$settings['autosave']['form_id'] = $form_id;
$settings['autosave']['url'] = url('autosave/handler');
$settings['autosave']['period'] = variable_get('autosave_period', 10);
$settings['autosave']['timeout'] = intval(variable_get('autosave_timeout', 0));
$settings['autosave']['q'] = $path;
$settings['autosave']['hidden'] = variable_get('autosave_hidden', 0);
$settings['autosave']['ignoreBehavior'] = variable_get('autosave_ignore_behavior', 0);
$settings['autosave']['theme'] = $GLOBALS['theme'];
// If an autosaved version of the form exists, let the user know so that
// he can restore it if desired.
$timestamp = db_query("SELECT timestamp FROM {autosaved_forms} WHERE form_id = :form_id AND path = :path AND uid = :uid", array(
':form_id' => $form_id,
':path' => $path,
':uid' => $GLOBALS['user']->uid,
))
->fetchField();
$settings['autosave']['savedTimestamp'] = $timestamp ? $timestamp : 0;
$settings['autosave']['savedDate'] = $timestamp ? format_date($timestamp) : 0;
$settings['autosave']['form_token'] = drupal_get_token($form_id);
$form['#id'] = str_replace("_", "-", $form_id);
$form['autosave_form_path']['#attached']['library'][] = array(
'autosave',
'autosave',
);
$form['autosave_form_path']['#attached']['js'][] = array(
'data' => $settings,
'type' => 'setting',
'scope' => JS_DEFAULT,
);
}
}
}