You are here

function field_weight_display_overview_form in Field display weights (per node) 7

Same name and namespace in other branches
  1. 7.2 field_weight.module \field_weight_display_overview_form()

Admin form displayed at node/%/display.

1 string reference to 'field_weight_display_overview_form'
field_weight_menu in ./field_weight.module
Implements hook_menu().

File

./field_weight.module, line 64
Field display weight module.

Code

function field_weight_display_overview_form($form, &$form_state, $node) {
  $form = array();
  $form_state['#node'] = $node;

  // Tree set to get weights in nested array under field keys in form state.
  $form['#tree'] = TRUE;

  // Get all field instances for this bundle type
  $instances = field_info_instances('node', $node->type);
  $weights = _field_weight_get_node_weight($node->nid);

  // Create new array of instances
  $weight_instances = array();

  // $weights will only be present if field weights have been saved
  // for that node.
  if ($weights) {
    foreach (array_keys($instances) as $key) {

      // Default to 0 if weight not found. i.e field added
      // after weights have last been saved.
      $weight_instances[$key] = array_key_exists($key, $weights) ? $weights[$key] : array(
        'weight' => 0,
        'hidden' => 0,
      );
    }
  }
  else {

    // If no weight entry found in db, use default field order from bundle.
    $weight_instances = $instances;
    drupal_set_message(t("Field weights for this node have not been overridden yet."), 'warning');
  }
  uasort($weight_instances, 'drupal_sort_weight');
  $form['field_weight'] = array(
    '#type' => 'fieldset',
    '#title' => t("Manage %node_type field display", array(
      '%node_type' => $node->type,
    )),
    // Put table/tabledrag theming of form into theme template.
    '#theme' => 'field_weight_display_overview',
  );
  $form['field_weight']['markup'] = array(
    '#markup' => '<p>' . t("Use the below table to arrange the order that fields will be displayed when this node is viewed. Saving your changes will override the default \n      field display order set in admin/structure/types/[node-type]/display. If you change one value, all of the weights will change accordingly. To return to the default display order, use the reset button.") . '</p>',
  );

  // Create sensible weights based on amount of fields.
  // Copied from block.admin.inc.
  $weight_delta = round(count($instances) / 2);

  // Pass hidden value to form submit so we can use instances
  // already stored there.
  $form['field_weight']['instances'] = array(
    '#type' => 'value',
    '#value' => $weight_instances,
  );

  // Include so we can use field_ui_formatter_options().
  module_load_include('inc', 'field_ui', 'field_ui.admin');
  foreach ($weight_instances as $field => $values) {
    $field_info = field_info_field($field);
    $formatter_options = field_ui_formatter_options($field_info['type']);
    $form['field_weight'][$field]['field'] = array(
      '#markup' => check_plain($instances[$field]['label']),
    );
    $form['field_weight'][$field]['weight'] = array(
      '#type' => 'weight',
      '#delta' => $weight_delta,
      '#default_value' => isset($values['weight']) ? $values['weight'] : 0,
      '#attributes' => array(
        'class' => array(
          'field-weight',
        ),
      ),
    );
    $form['field_weight'][$field]['hidden'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($values['hidden']) ? $values['hidden'] : 0,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Save"),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t("Reset"),
    '#submit' => array(
      '_field_weight_remove_weights',
    ),
  );
  return $form;
}