You are here

function _weight_set_weight in Weight 7.2

Set the weight of a node.

5 calls to _weight_set_weight()
weight_node_insert in ./weight.module
Implements hook_node_insert().
weight_node_update in ./weight.module
Implements hook_node_update().
weight_views_submit in ./weight.module
Submit handler for Views Weight form.
_weight_set_ajax in ./weight.module
_weight_set_weight_from_menu_link in ./weight.module
Set the weight of a node from the menu link.

File

./weight.module, line 631

Code

function _weight_set_weight($node) {

  // Default weight value: passed in weight value
  $weight = $node->weight_weight;

  // Get the settings for this node
  $settings = _weight_get_settings($node->type);

  // If menu_weight selected & exists, override menu weight
  if (isset($settings['menu_weight']) && $settings['menu_weight']) {

    // Get the menu weight for this node (if it exists).
    $weight = db_select('menu_links', 'ml');
    $weight
      ->fields('ml', array(
      'weight',
    ))
      ->condition('link_path', 'node/' . $node->nid)
      ->condition('module', 'menu');
    $weight = $weight
      ->execute()
      ->fetchField();
    if (!$weight) {
      $weight = $settings['default'];
    }
  }
  if (module_exists('translation') && $node->nid != $node->tnid) {
    if ($settings['sync_translations'] && $node->tnid != 0) {
      $weight = _weight_get_weight($node->tnid);
    }
  }
  if ($node->nid && is_numeric($weight)) {
    db_merge('weight_weights')
      ->key(array(
      'entity_id' => $node->nid,
    ))
      ->fields(array(
      'entity_id' => $node->nid,
      'entity_type' => 'node',
      'weight' => $weight,
    ))
      ->updateFields(array(
      'weight' => $weight,
    ))
      ->execute();

    // Reset this node's cache.
    entity_get_controller('node')
      ->resetCache(array(
      $node->nid,
    ));

    // Let Search API know the weight has been updated.
    if (module_exists('search_api')) {
      search_api_track_item_change('node', array(
        $node->nid,
      ));
    }
  }
}