You are here

function weight_form_alter in Weight 5

Same name and namespace in other branches
  1. 6 weight.module \weight_form_alter()
  2. 7 weight.module \weight_form_alter()

Implementation of hook_form_alter().

This is where we tweak the admin/content/node to include our weight selector; hide the 'sticky' filter (it won't work when using weight module), and add some help text to the form.

File

./weight.module, line 150

Code

function weight_form_alter($form_id, &$form) {
  $weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names')));
  $weight_node_type_names = array();
  foreach ($weight_node_types as $type) {
    $weight_node_type_names[] = node_get_types('name', $type);
  }

  // The admin node page does not use the nodeapi for getting lists of nodes, so
  // I never have a chance to convert the sticky flag to 0/1. and trying to
  // filter on that field will not work. Therefore, I am going to hide the
  // 'filter on sticky status' altogether when weight_module is enabled
  if ($form_id == 'node_filter_form') {

    // no comment
    unset($form['filters']['status']['status']['#options']['sticky-1']);
    unset($form['filters']['status']['status']['#options']['sticky-0']);
    $form['weight_help'] = array(
      '#type' => 'markup',
      '#value' => t('<strong>Note:</strong> When the weight module is enabled, it is not possible to filter based on sticky status.'),
    );
  }
  if ($form_id == 'node_admin_nodes') {

    // I can't add a table header for weight, so instead I'm going to explain
    // the weight dropdown to the user. Also, to position my help text
    // appropriately, I'm using this '#suffix' hack rather than adding
    // a form property as i'd like to do.
    $form['options']['#suffix'] .= t('<strong>Weight:</strong> To change the weight of a node, select a value from the corresponding dropdown box under <i>@operations</i>. Node weights are submitted immediately. Selectors are only available for node types configured on the <a href="@weight_admin">weight admin page</a>.', array(
      '@weight_admin' => url('admin/settings/weight'),
      '@operations' => t('Operations'),
    ));

    // add my weight selector under the operations section of the admin node
    // overview page (admin/content/node)
    if (!empty($form['operations'])) {
      foreach ($form['operations'] as $nid => $title) {

        // only add weight selector if weight is enabled for this node type
        if (in_array($form['name'][$nid]['#value'], $weight_node_type_names)) {
          $selector = weight_node_selector($nid);
          $form['operations'][$nid]['weight_selector']['#value'] = $selector['selector'];
          $form['status'][$nid]['#value'] .= $selector['status'];
        }
      }
    }
  }

  // node edit page weight selector
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (user_access('assign node weight') || user_access('administer nodes')) {
      $node = $form['#node'];
      if (in_array($node->type, $weight_node_types)) {
        $range = variable_get('weight_range', 20);
        $form['weight_fieldset'] = array(
          '#type' => 'fieldset',
          '#title' => t('Node Weight'),
          '#collapsible' => TRUE,
          '#collapsed' => $node->node_weight == 0,
          '#weight' => 2,
        );
        $form['weight_fieldset']['node_weight'] = array(
          '#type' => 'weight',
          '#title' => t("Node Weight"),
          '#default_value' => $node->node_weight,
          '#delta' => $range,
          '#description' => t('In a node list context (such as the front page or term pages), list items (e.g. "teasers") will be ordered by "stickiness" then by "node weight" then by "authored on" datestamp. Items with a lower (lighter) node weight value will appear above those with a higher (heavier) value.'),
        );
        if (variable_get('weight_use_menu', FALSE)) {
          $form['weight_fieldset']['node_weight']['#description'] .= '<br /> ' . t('<strong>Note</strong>: If this node is used in a menu, then this weight will be ignored.');
        }
      }
    }
  }
}