Description:
The weight module adds a weight option to enabled node types. It uses the "sticky" field in the database to store weights as well as sticky information (so that feature is not lost). Nodes will be sorted first by stickiness, then by weight (lightest to heaviest), then by creation date.
To enable weight sorting on existing nodes, visit the weight db setup page and select which node types to allow weighting. When you click "Save configuration," the module will convert old sticky values to new weight-encoded values for proper sorting. If you de-select a type, weights on all nodes of that type will be converted back to standard sticky values.
Users with "administer nodes" permission will always be able to adjust weight for enabled node types. However, enabling "assign node weight" will allow non-node-admin users to adjust weight on their own nodes. Find these settings here.
You may easily manage the weight of multiple nodes simultaneously by using the node admin page.
', array( '@setup' => url('admin/settings/weight/setup'), '@access' => url('admin/user/permissions'), '@node_admin' => url('admin/content/node') ) ); } } function weight_perm() { return array('assign node weight'); } /** * Implementation of hook_menu(). */ function weight_menu() { $items = array(); // Ajax callback for weight changer page. $items['admin/node/weight/_weight_change'] = array( 'page callback' => '_weight_change', 'access arguments' => array('administer nodes'), 'type' => MENU_CALLBACK ); // Top level settings. $items['admin/settings/weight'] = array( 'title' => 'Weight', 'access arguments' => array('administer site configuration'), 'description' => 'Add weight-based sorting to nodes.', 'page callback' => 'drupal_get_form', 'page arguments' => array('weight_settings_form'), ); return $items; } function weight_nodeapi(&$node, $op) { switch ($op) { case 'presave': // Non-weighted nodes have a weight of zero. if (is_null($node->node_weight)) { $node->node_weight = 0; } // If the admin wants to use the menu weight, see if there is one. if (variable_get('weight_use_menu', FALSE)) { $node->node_weight = (isset($node->menu['link_title']) && !empty($node->menu['link_title'])) ? $node->menu['weight'] : $node->node_weight; } // Encode weight into the sticky value for the database. _weight_encode($node); break; case 'load': _weight_decode($node); break; } } /** * 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. */ function weight_form_alter(&$form, $form_state, $form_id) { $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); } if ($form_id == 'node_admin_content') { // 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 unset($form['filters']['status']['status']['#options']['sticky-1']); unset($form['filters']['status']['status']['#options']['sticky-0']); $form['weight_help'] = array( '#type' => 'markup', '#value' => t('Note: When the weight module is enabled, it is not possible to filter based on sticky status.') ); // 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['admin']['options']['#suffix'] .= t('Weight: To change the weight of a node, select a value from the corresponding dropdown box under Operations. Node weights are submitted immediately. Selectors are only available for node types configured on the weight admin page.', array('@weight_admin' => url('admin/settings/weight')) ); // Add weight selector under the operations section of the admin node // overview page (admin/content/node). if (!empty($form['admin']['operations'])) { foreach ($form['admin']['operations'] as $nid => $title) { // only add weight selector if weight is enabled for this node type if (in_array($form['admin']['name'][$nid]['#value'], $weight_node_type_names) ) { $selector = weight_node_selector($nid); $form['admin']['operations'][$nid]['weight_selector']['#value'] = $selector['selector']; $form['admin']['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); $position = variable_get('weight_position', 0); $where = 'weight_form'; if ($position == 10 && user_access('administer nodes')) { // We will add it to the Workflow fieldset. $where = 'options'; $form['options']['#collapsed'] = ($node->node_weight == 0); } else { // Add the node weight selector fieldset. $form['weight_form'] = array( '#type' => 'fieldset', '#title' => t('Node Weight'), '#collapsible' => TRUE, '#collapsed' => ($node->node_weight == 0), '#weight' => $position, ); } $form[$where]['node_weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#default_value' => (int)$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_form']['node_weight']['#description'] .= ''. t('This will be the +/- range for node weight.') .'
', ); $form['weight_use_menu'] = array( '#type' => 'checkbox', '#title' => t('Use Menu Weight'), '#default_value' => variable_get('weight_use_menu', FALSE), '#description' => ''. t('If the node has not been weighted, should we use the menu item weight?') .'
', ); $form['weight_position'] = array( '#type' => 'weight', '#delta' => 10, '#title' => t('Weight selector position weight'), '#default_value' => variable_get('weight_position', 0), '#description' => ''. t('This controls where the selection for node weight goes on the node edit form. If the position is 10 and the user has "administer nodes" permission, it will be added into the "Workflow options."') .'
', ); $form['weight_node_types'] = array( '#type' => 'checkboxes', '#title' => t('Display On'), '#default_value' => variable_get('weight_node_types', $types), '#options' => $types, '#description' => ''. t('Add node weighting to these content types.
Note: Unselecting a node type after having changed weights for nodes of that type will leave these nodes with the current weight. You may want to check the content page before unsetting any node types.', array( '@posts_page' => url('admin/content/node')) ) .'
', ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save configuration'), ); return $form; } function weight_settings_form_submit($form, &$form_state) { $before = array_filter(variable_get('weight_node_types', array())); $after = array_filter($form_state['values']['weight_node_types']); $del = array_diff($before, $after); $add = array_diff($after, $before); if ($add) { weight_old_nodes($add); } if ($del) { weight_disable($del); } variable_set('weight_range', $form_state['values']['weight_range']); variable_set('weight_node_types', $after); variable_set('weight_position', $form_state['values']['weight_position']); drupal_set_message(t('Settings updated.')); } /** * Update the sticky value of existing nodes if they are enabled for weights. * This ensures that they will sort correctly. */ function weight_old_nodes($weight_node_types = array()) { if ($weight_node_types) { drupal_set_message(t('Enabling weight for: !types', array('!types' => implode(', ', $weight_node_types)))); $placeholders = db_placeholders($weight_node_types, 'text'); db_query("UPDATE {node} SET sticky = 100 WHERE sticky = 1 AND type IN ($placeholders)", $weight_node_types); $count = db_affected_rows(); db_query("UPDATE {node} SET sticky = -100 WHERE sticky = 0 AND type IN ($placeholders)", $weight_node_types); $count += db_affected_rows(); drupal_set_message(t('@count nodes weight enabled.', array('@count' => $count))); } } /** * Set nodes back to normal sticky values if they are not enabled for weights. */ function weight_disable($weight_node_types = array()) { if ($weight_node_types) { drupal_set_message(t('Disabling weight for: !types', array('!types' => implode(', ', $weight_node_types)))); $placeholders = db_placeholders($weight_node_types, 'text'); db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1 AND type IN ($placeholders)", $weight_node_types); $count = db_affected_rows(); db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0 AND type IN ($placeholders)", $weight_node_types); $count += db_affected_rows(); } else { db_query("UPDATE {node} SET sticky = 1 WHERE sticky > 1"); $count = db_affected_rows(); db_query("UPDATE {node} SET sticky = 0 WHERE sticky < 0"); $count += db_affected_rows(); } drupal_set_message(t('@count nodes weight disabled.', array('@count' => $count))); } /** * Generate JS code for selecting individual node weights on admin page */ function weight_node_selector($nid) { static $js_included; if (!$js_included) { $path = drupal_get_path('module', 'weight'); drupal_add_js($path .'/httpRequest.js', 'module', 'header', TRUE); $js_included = TRUE; drupal_add_css($path .'/weight.css'); } $selector_template = "\n"."'; $weight_selector = $selector_template; // TODO: need node weight to be able to choose this! $weight_selector = preg_replace("/(value='$weight')/", "$1 selected='selected'", $weight_selector); $weight_selector = preg_replace("/\[NID\]/", $nid, $weight_selector); $weight_selector = '