Training

Our next U.S. stop:

Mike Anello and Andrew Riley from Mediacurrent are teaming up to offer the first Blue Collar Git workshop on Friday, June 8 as part of DrupalCamp Charlotte (also part of the Southeast LinuxFest). The cost is only $149 for the full day if you register during the month of May ($199 otherwise). 

New Podcast

Posted Wednesday, May 9 at 7:50 am
Brandon Morrison (Brandonian on drupal.org) joins Andrew Riley and Mike Anello on the first post-DrupalCon Denver edition of the podcast to talk about all things Geo in Drupal 7. Brandon is one of the maintainers of the GeoField module and is an active member of the Drupal Geo community.
Download Podcast 82
DrupalEasy_ep82_20120509.mp3
Syndicate content

Newsletter

Stay up-to-date on DrupalEasy workshops and other events. On average, we send about one newsletter per month.

Testimonial

Mike is a great teacher!

Who are we?

DrupalEasy is the collective expertise of Ryan Price and Michael Anello, who joined forces to provide training and consulting services worldwide. Read all about them and what they can do.

What is Drupal?

Drupal is a free, super-powerful content management system for sites that require information posting and collection, including blogs, forums, videos, photos, and databases of information. We think it is the best platform available. Here's why...

Why Drupal?

More and more savvy organizations are going with Drupal for content management, and its no mystery why. It’s free, flexible, and easy to maintain for small or large volume sites. Learn more...

Module Development - Alternative to the "Are You Sure?" Screen

There have been several times where I've had to write a custom module that enables the site admin to make some major changes to their site. With changes like these, I want the site admin to be super-duper sure they know what they're doing, so while sometimes I use Drupal's built-in Confirm Form function, I sometimes decide to take a less-intrusive approach.

One alternative that I recently used is to force the user to check an extra box in order for the site-altering code to be run. In this most recent case, the action was tied to a node update via hook_nodeapi(). I added a single on/off checkbox to the content type and then used hook_form_alter() to ensure that the checkbox is always presented to the user as un-checked.

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ('superpowerful_node_form' == $form_id) {
    // Always force the user to check the "update site" box for safety...
    $form['field_updatesite']['#default_value'][0]['value'] = 0;
  }
}

Then, in the hook_nodeapi() code, I make sure the box is checked before proceeding with the site-altering code.

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'superpowerful') {
    switch ($op) {
      case 'insert':
      case 'update':
        if ($node->field_updatesite[0]['value']) {
          // do lots of stuff here...
        }
        else {
          drupal_set_message('Since the "Update Site" checkbox was not checked, the site was <em>not</em> updated.');
        }
        break;
    }
  }
}

Nothing is fool-proof, but my clients seem to appreciate the one less click and page load.

Trackback URL for this post:

http://drupaleasy.com/trackback/180
No votes yet

More Quicktips


1 comment

Pingback

[...] Module Development - Alternative to the "Are You Sure?" Screen | DrupalEasy drupaleasy.com/quicktips/module-development-alternative-are-you-sure-screen – view page – cached There have been several times where I've had to write a custom module that enables the site admin to make some major changes to their site. With chang..., There have been several times where I've had to write a custom module that enables the site admin to make some major changes to their site. With changes like these, I want the site admin to be super-duper sure they know what they're doing, so while — From the page [...]

Syndicate content