You are here

function signup_alter_node_form in Signup 5.2

Same name and namespace in other branches
  1. 5 signup.module \signup_alter_node_form()
  2. 6.2 includes/node_form.inc \signup_alter_node_form()
  3. 6 includes/node_form.inc \signup_alter_node_form()
  4. 7 includes/node_form.inc \signup_alter_node_form()

Alters the node form to inject the appropriate per-node signup settings.

1 call to signup_alter_node_form()
signup_form_alter in ./signup.module
Implementation of hook_form_alter().

File

./signup.module, line 452
The Signup module (http://drupal.org/project/signup) manages replies to nodes. In particular, it's good for event management. Signup supports sending reminder emails and automatically closing signups for nodes with a start time, via the Event…

Code

function signup_alter_node_form($form_id, &$form) {
  global $user;

  // Load the node if it already exists.
  if (!empty($form['nid']['#value'])) {
    $node = node_load($form['nid']['#value']);
  }
  else {
    $node = NULL;
  }
  $node_type = $form['type']['#value'];
  $signup_type_default = variable_get('signup_node_default_state_' . $node_type, 'disabled');
  if (!empty($node)) {
    $node_scheduler = _signup_get_node_scheduler($node);
  }
  else {
    $node_scheduler = _signup_get_node_type_scheduler($node_type);
  }
  $node_has_date = $node_scheduler != 'none';

  // If signups are possible, and the current user either has the global
  // 'administer all signups' permission or has the 'administer signups
  // for own content' permission and is creating new content or editing
  // their own content, add a fieldset for signup-related settings.
  // Signups are possible if they're not explicitly disallowed for this
  // node type, or if this node is already signup-enabled (in case an
  // admin erroneously marks a node-type to disallow signups when there
  // are already nodes of that type with signups enabled).
  $signups_possible = $signup_type_default != 'disabled' || !empty($node) && !empty($node->signup);
  $admin_all = user_access('administer all signups');
  $admin_own = user_access('administer signups for own content') && (empty($node) || $node->uid == $user->uid);
  if ($signups_possible && ($admin_all || $admin_own)) {
    $form['signup'] = array(
      '#type' => 'fieldset',
      '#title' => t('Signup settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 30,
    );

    // Figure out what the options should be.  If there are already
    // people signed-up for this node, we need a 3rd choice: disable
    // signups and remove all signup data.
    $has_signups = !empty($node) && db_result(db_query("SELECT COUNT(*) from {signup_log} WHERE nid = %d", $node->nid));
    $radio_options[1] = t('Enabled');
    if ($has_signups) {
      $radio_options[0] = t('Disabled, but save existing signup information');
      $radio_options[2] = t('Disabled, and remove all signup information') . ' <strong>(' . t('This can not be undone, use with extreme caution!') . ')</strong>';
    }
    else {
      $radio_options[0] = t('Disabled');
    }

    // Figure out what the default selection for signups should be.
    if (isset($node->signup)) {
      $default_option = $node->signup;
    }
    else {
      $default_option = $signup_type_default == 'enabled_on' ? 1 : 0;
    }
    if ($default_option == 1) {
      $hint = t('If enabled, you can control whether users may sign up by visiting the !signups tab and toggling if signups are %open or %closed for this %node_type.', array(
        '!signups' => !empty($node) ? l(t('Signups'), 'node/' . $node->nid . '/signups') : theme('placeholder', t('Signups')),
        '%open' => t('open'),
        '%closed' => t('closed'),
        '%node_type' => node_get_types('name', $node_type),
      ));
    }
    else {
      $hint = '';
    }

    // Add the form element to toggle if signups are allowed.
    $form['signup']['signup_enabled'] = array(
      '#type' => 'radios',
      '#options' => $radio_options,
      '#default_value' => $default_option,
      '#description' => $hint . '<div class="js-hide">' . t('If disabled, all of the other signup settings will be ignored.') . '</div>',
      '#prefix' => '<div class="signup-allow-radios">',
      '#suffix' => '</div>',
    );

    // If JS is enabled, signup.css will hide all the settings on page
    // load if signups aren't enabled on this node.
    $settings_class = "signup-node-settings";
    if ($default_option != 1) {
      $settings_class .= " js-hide";
    }

    // Add the actual settings.  We wrap this in a div to make it easy
    // to use jQuery to hide these settings when signups are disabled.
    drupal_add_js(drupal_get_path('module', 'signup') . '/js/node_form.js');
    drupal_add_css(drupal_get_path('module', 'signup') . '/signup.css');
    $form['signup']['node_settings'] = array(
      '#prefix' => '<div class="' . $settings_class . '">',
      '#suffix' => '</div>',
    );
    $form['signup']['node_settings']['settings'] = signup_node_settings_form($node, $node_type, $node_has_date);
  }
}