You are here

function template_preprocess_node_form in Node form columns 7

Preprocess function to run ahead of other modules.

File

./nodeformcols.module, line 106

Code

function template_preprocess_node_form(&$aVars) {
  drupal_add_css(drupal_get_path('module', 'nodeformcols') . '/css/nodeformcols.css');
  $default_region = variable_get('nodeformcols_default_region', NODEFORMCOLS_DEFAULT_REGION);
  $form =& $aVars['form'];
  $class = array(
    'node-form',
    'clearfix',
  );
  $regions = array();
  $has_elements = array();
  $weight = 0;
  foreach (nodeformcols_form_regions() as $name => $title) {
    $regions[$name] = array(
      '#prefix' => '<div class="form-region-' . $name . '">',
      '#suffix' => '</div>',
      '#weight' => $weight,
    );
    $weight++;
  }
  drupal_alter('nodeformcols_pre_placement', $form);
  $variant = isset($form['#nodeformcols_variant']) ? $form['#nodeformcols_variant'] : 'default';
  $placements = nodeformscols_field_placements($form['#node']->type, $variant);

  // Track if new fields should be adjusted above the buttons.
  // TODO: This should be generalized to a way to tell nodeformcols where to place new fields (above below field X).
  $adjust_to_buttons = isset($placements['buttons']['region']) && $placements['buttons']['region'] == $default_region;
  foreach (element_children($form) as $key) {
    $field = $form[$key];
    if (isset($field['#type']) && in_array($field['#type'], array(
      'value',
      'hidden',
      'token',
    )) || isset($field['#access']) && $field['#access'] == FALSE) {
      continue;
    }
    if (isset($placements[$key])) {
      $p = $placements[$key];
      if (isset($p['weight'])) {
        $field['#weight'] = $p['weight'];
      }
      if (isset($p['collapsed']) && isset($field['#collapsible']) && $field['#collapsible']) {
        $field['#collapsed'] = $p['collapsed'];
      }
      $regions[$p['region']][$key] = $field;
      $has_elements[$p['region']] = TRUE;
      unset($form[$key]);
    }
    else {

      // Set the default placement for unknown fields
      $regions[$default_region][$key] = $field;
      if ($adjust_to_buttons && $regions[$default_region][$key]['#weight'] >= $placements['buttons']['weight']) {
        $regions[$default_region][$key]['#weight'] = $placements['buttons']['weight'] - 0.1;
      }
      $has_elements[$default_region] = TRUE;
      unset($form[$key]);
    }
  }
  foreach ($regions as $name => $data) {
    if (!empty($has_elements[$name])) {
      $class[] = 'node-form-has-region-' . $name;
      $form['nodeformcols_region_' . $name] = $regions[$name];
    }
  }
  $aVars['class'] = join($class, ' ');
}