You are here

function notifications_ui_node_form in Notifications 5

Form for node subscriptions @ TODO: offer the same form in a block to be put in the contents region.

Parameters

$node: a node object

$subform: Optional produce subform instead of full form with buttons

Return value

Partial subscription form, just missing submit button.

1 call to notifications_ui_node_form()
notifications_ui_form_alter in notifications_ui/notifications_ui.module
Implementation of hook_form_alter()
1 string reference to 'notifications_ui_node_form'
notifications_ui_block in notifications_ui/notifications_ui.module
Implementation of hook_block()

File

notifications_ui/notifications_ui.module, line 128
User Interface for subscriptions modules

Code

function notifications_ui_node_form($node, $subform = TRUE) {
  global $user;
  $form = array();
  $node_options = notifications_ui_user_node($user, $node);
  if (count($node_options)) {

    // Process all options building the array of indexed params for each
    $options = $params = $defaults = array();
    $index = 1;

    // If we start with zero, get some value sent as 0 => 0
    $number = 0;

    // Number of subscriptions
    foreach ($node_options as $option) {
      $options[$index] = $option['name'];

      // Check wether user is subscribed
      if (!empty($option['subscription'])) {
        $params[$index] = (array) $option['subscription'];
        $defaults[] = $index;
        $number++;
      }
      else {
        $params[$index] = $option;
      }
      $index++;
    }

    // Now we have compiled the data, build the form. Note that we are passing the parameters
    // in the 'params' value as an array, while the checkboxes are in 'options' fieldset
    if ($subform) {
      $form['subscriptions'] = array(
        '#type' => 'fieldset',
        '#title' => t('Subscriptions (%number)', array(
          '%number' => $number,
        )),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE,
      );
    }
    else {
      $form['subscriptions'] = array(
        '#tree' => TRUE,
      );
    }
    $form['subscriptions']['params'] = array(
      '#type' => 'value',
      '#value' => $params,
    );
    $form['subscriptions']['options'] = array(
      '#type' => 'checkboxes',
      '#default_value' => $defaults,
      '#options' => $options,
    );
    $form['subscriptions']['account'] = array(
      '#type' => 'value',
      '#value' => $user,
    );
    if (!$subform) {
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
      );
    }
  }

  // Allow other modules, i.e. autosubscribe to change this form
  notifications_alter('node_form', $form);
  return $form;
}