Project:SaveGuard
Version:7.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work

Issue Summary

Is it possible to disable this modules functionality on certain forms. I only want it enabled on the body tag of a certain node and not on the search field, login fields etc.

Comments

#1

Here is a rough mod with visibility controls added so that you can choose based on path and roles. Probably needs to be cleaned up and probably could add content type controls as well. If you do clean this up I would appreciate it if you would send me the updated module.

Good luck.

AttachmentSize
saveguard.txt 1.93 KB

#2

Status:active» needs work

Patch uses visibility api - need to make sure that's going to upgrade to Drupal 6 since there's not a release node for it yet.

#3

Version:5.x-0.1» 7.x-1.x-dev
Priority:minor» normal

#4

+1

#5

Hi,

I have added the possibility to add it to only the form you really want.

For Drupal 6.

<?php
// $Id: saveguard.module,v 1.4.2.2 2008/12/12 21:44:21 deekayen Exp $

/**
* @file
* Warn user when they are leaving a page with unsaved changes.
*
* Adds a onBeforeUnload event to all modified forms.
*/

/**
* Implementation of hook_menu().
*/
function saveguard_menu() {
  $items['admin/settings/saveguard'] = array(
    'title' => 'SaveGuard',
    'description' => 'Set the popup message text.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('saveguard_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration')
  );

  return $items;
}

/**
* Define a settings form.
*/
function saveguard_admin_settings() {
  $form = array();

  $form['saveguard_message'] = array(
    '#type' => 'textfield',
    '#title' => t("Popup Message"),
    '#default_value' => variable_get('saveguard_message', NULL),
  );

  return system_settings_form($form);
}


/**
* Implementation of hook_form_alter().
* Just add javascript to all forms.
*
* @param $form_id
*    name of the form being acted on
* @param $form
*    array with form structure
*/
function saveguard_form_alter(&$form, $form_state, $form_id) {

if($form_id == 'node_type_form'){
  $options = array(0 => t('Disabled'), 1 => t('Enabled'));
$form['save_guard'] = array(
'#type' => 'fieldset',
'#title' => t('Safe Guard'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('administer safeguard'),
);

$form['save_guard']['save_guard_expose'] = array(
'#type' => 'radios',
'#title' => t('Enable Save Guard for this form'),
'#default_value' => variable_get('save_guard_expose_' . $form['#node_type']->type, 0),
'#description' => t('Enable/Disable SaveGuard for this form.'),
'#options' => $options,
'#weight' => -1,
);

}
elseif (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id && is_save_guard_form($form['#node'])) {
drupal_add_js(drupal_get_path('module', 'saveguard') .'/saveguard.js');
static $done;
if (!$done) {
$settings['saveguard'] = array(
'msg' => variable_get('saveguard_message', NULL),
);
drupal_add_js($settings, 'setting');
$done = TRUE;
}

}
}

/**
* Determine if a given node is a SaveGuard Form.
* @param $type
*   The node object or the node's type
*/
function is_save_guard_form($type) {
  if (is_object($type)) {
    $type = $type->type;
  }
  return variable_get('save_guard_expose_'. $type, 0);
}

#6

Thanks for this solution!
It seems to work as expected. (More testing is needed though.)
I've attached it as a patch file.

AttachmentSize
saveguard_form_select.patch 2.26 KB

#7

Another take on the problem: Here's a patch (and a complete module in .tgz file, if easier) that offers 2 config options:

  1. Form ids to saveguard on change (normal behaviour)
  2. Form ids to saveguard always (even before a change detected)

Each option takes form ids, one per line, which can include * for wildcard. So *_node_form is all node forms and * would be all forms. To not break original behaviour, it defaults to * in the "saveguard on change" option.

So option (1) is hopefully clear: you can choose which forms saveguard applies to.

But why option (2)? I hear you ask. Because CKEditor, and possibly others, don't trigger onchange or onkeypress on the textareas they replace. So if you edit a page which defaults to using a Rich Text Editor, you can merrily edit, then hit a link and Saveguard has not triggered the caution; you lose work. Of course, a nicer solution would be to get RT editors to play ball, but I didn't have time to look into that.

Hope this is useful.

AttachmentSize
saveguard.diff 4.65 KB
saveguard.tgz 8.89 KB

#8

There are a number of code style violations in #7, mostly tabs instead of spaces. You should not be modifying the version line in .info, either. See http://drupal.org/patch/submit

#9

#8 got you. I understand the importance of all that, but haven't had a spare day to climb the rather overbearing learning curve for patches, and to absorb that into an efficient work flow. When I get chance I'll clean up my code and re-post. As it is functioning and possibly useful to others, I thought it was good to share it. Apols if I got that wrong.

nobody click here