You are here

function flag_form_alter in Flag 7.2

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

Implements hook_form_alter().

File

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

Code

function flag_form_alter(&$form, &$form_state, $form_id) {

  // Alter node forms.
  if (isset($form['type']) && isset($form['#node']) && $form_id == $form['type']['#value'] . '_node_form') {
    global $user;
    $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.
    $flags_in_form = 0;
    $flags_visible = 0;
    foreach ($flags as $name => $flag) {
      if (!$flag->show_on_form) {
        continue;
      }
      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;
      }

      // If the flag is not global and the user doesn't have access, skip it.
      // Global flags have their value set even if the user doesn't have access
      // to it, similar to the way "published" and "promote" keep the default
      // values even if the user doesn't have "administer nodes" permission.
      $access = $flag
        ->access($nid, $flag_status ? 'unflag' : 'flag', $user);
      if (!$access && !$flag->global) {
        continue;
      }

      // Add the flag checkbox if displaying on the form.
      $form['flag'][$flag->name] = array(
        '#type' => 'checkbox',
        '#title' => $flag
          ->get_label('flag_short', $nid ? $nid : $form['type']['#value']),
        '#description' => $flag
          ->get_label('flag_long', $nid ? $nid : $form['type']['#value']),
        '#default_value' => $flag_status,
        '#return_value' => 1,
        '#attributes' => array(
          'title' => $flag
            ->get_title(),
        ),
      );

      // If the user does not have access to the flag, set as a value.
      if (!$access) {
        $form['flag'][$flag->name]['#type'] = 'value';
        $form['flag'][$flag->name]['#value'] = $flag_status;
      }
      else {
        $flags_visible++;
      }
      $flags_in_form++;
    }
    if ($flags_in_form) {
      $form['flag'] += array(
        '#weight' => 1,
        '#tree' => TRUE,
      );
    }
    if ($flags_visible) {
      $form['flag'] += array(
        '#type' => 'fieldset',
        '#title' => t('Flags'),
        '#collapsible' => TRUE,
        // Vertical tabs support:
        '#group' => 'additional_settings',
        '#attributes' => array(
          'class' => array(
            'flag-fieldset',
          ),
        ),
        '#attached' => array(
          'js' => array(
            'vertical-tabs' => drupal_get_path('module', 'flag') . '/theme/flag-admin.js',
          ),
        ),
      );
    }
  }
}