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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nathanraft’s picture

FileSize
1.93 KB

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.

deekayen’s picture

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.

deekayen’s picture

Version: 5.x-0.1 » 7.x-1.x-dev
Priority: Minor » Normal
kenorb’s picture

+1

webadpro’s picture

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);
}

mtcs’s picture

FileSize
2.26 KB

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

artfulrobot’s picture

FileSize
8.89 KB
4.65 KB

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.

deekayen’s picture

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

artfulrobot’s picture

#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.

Nikro’s picture

Oh damn, okay I didn't search before so I did my patch.

Basically it does the following:

  • Extends Form Settings to 3 options (Ajax-based): All forms, Form IDs list, Except Form_IDs
  • Adds the SaveGuard only to those (depending on the rule)
Nikro’s picture

Status: Needs work » Needs review
glynster’s picture

@Nikro you are a rockstar this makes the module even better! +RTBC

dbassendine’s picture

Patch at #10 works well - thanks! I have only tested "Specify form_id's", though.

One minor improvement I'd make is, in the textarea for the list of form_ids that appears after you click "Specify form_id's" - add a note saying what the format to list multiple form ids is. The only syntax that works is putting a form_id on each line, without any separator. It would save trial and error time to explain that up front.

IRuslan’s picture

Patch is not relevant for last dev version.

I've updated patch for the last version with several corrections:
- new variables added to hook_uninstall
- added constants for apply mode
- adopted $form['attached'] for proper JS inclusion.

glynster’s picture

@IRuslan thanks for the patch update however on my end the last hunk fails? Any ideas as to why that would happen?

IRuslan’s picture

@glynster

I rechecked and on my end, it applied correctly. Could you ensure you are trying to use dev version of the module to apply against?

glynster’s picture

@IRuslan thanks for the speedy reply. I see there is no available dev version so does this mean I need to be downloading the master from Git?

IRuslan’s picture

@glynster

Yes, correct. That's because dev version is pretty different from the last release.
However, if you want to provide some tests, I could add patch for you particular version. Which one do you use?

Oleksiy’s picture

Thanks for the patch #14. Works fine for me only with saveguard 2.x, which is missed on project page and available only in the project GIT repository.

bwaindwain’s picture

We're using patch #14 and it works great for us.