You are here

function flag_form_alter in Flag 5

Same name and namespace in other branches
  1. 8.4 flag.module \flag_form_alter()
  2. 6.2 flag.module \flag_form_alter()
  3. 6 flag.module \flag_form_alter()
  4. 7.2 flag.module \flag_form_alter()

Implementation of hook_form_alter().

File

./flag.module, line 214
The Flag module.

Code

function flag_form_alter($form_id, &$form) {
  global $user;
  if ($form_id == 'node_type_form') {
    $flags = flag_get_flags('node', $form['#node_type']->type, $user);
    foreach ($flags as $flag) {
      if ($flag->show_on_form) {
        $var = 'flag_' . $flag->name . '_default';
        $form['workflow']['flag'][$var] = array(
          '#type' => 'checkbox',
          '#title' => $flag
            ->get_label('flag_short'),
          '#default_value' => variable_get($var . '_' . $form['#node_type']->type, 0),
          '#return_value' => 1,
        );
      }
    }
    if (isset($form['workflow']['flag'])) {
      $form['workflow']['flag'] += array(
        '#type' => 'item',
        '#title' => t('Default flags'),
        '#description' => t('Above are the <a href="@flag-url">flags</a> you elected to show on the node editing form. You may specify their initial state here.', array(
          '@flag-url' => url('admin/build/flags'),
        )),
        // Make the spacing a bit more compact:
        '#prefix' => '<div class="form-checkboxes">',
        '#suffix' => '</div>',
      );
    }
  }
  elseif (isset($form['type']) && isset($form['#node']) && $form_id == $form['type']['#value'] . '_node_form') {
    if (!$user->uid) {
      return;
    }
    $nid = !empty($form['nid']['#value']) ? $form['nid']['#value'] : NULL;
    $flags = flag_get_flags('node', $form['type']['#value'], $user);

    // Filter out flags which need to be included on the node form.
    foreach ($flags as $name => $flag) {
      if (!$flag->show_on_form) {
        unset($flags[$name]);
      }
    }
    if (count($flags)) {
      $form['flag'] = array(
        '#type' => 'fieldset',
        '#weight' => 1,
        '#tree' => TRUE,
        '#title' => t('Flags'),
        '#collapsible' => TRUE,
      );
    }
    foreach ($flags as $flag) {
      if (isset($form['#node']->flag[$flag->name])) {
        $flag_status = $form['#node']->flag[$flag->name];
      }
      else {
        $flag_status_default = variable_get('flag_' . $flag->name . '_default_' . $form['type']['#value'], 0);
        $flag_status = $nid ? $flag
          ->is_flagged($nid) : $flag_status_default;
      }
      $form['flag'][$flag->name] = array(
        '#type' => 'checkbox',
        '#title' => $flag
          ->get_label('flag_short', $nid),
        '#description' => $flag
          ->get_label('flag_long', $nid),
        '#default_value' => $flag_status,
        '#return_value' => 1,
      );
    }
  }
}