You are here

function _weight_set_defaults in Weight 7.2

Set nodes to the default weight.

1 call to _weight_set_defaults()
weight_node_type_form_submit in ./weight.module
Additional submit function for node_type_form().

File

./weight.module, line 803

Code

function _weight_set_defaults($default, $type) {

  // Get the nodes to work with.
  $select = db_select('node', 'n');
  $select
    ->fields('n', array(
    'nid',
  ))
    ->condition('type', $type);
  $results = $select
    ->execute();
  foreach ($results as $result) {
    if ($default == 'menu') {

      // Get the menu weight fot this node.
      $weight = db_select('menu_links', 'ml');
      $weight
        ->fields('ml', array(
        'weight',
      ))
        ->condition('link_path', 'node/' . $result->nid);
      $weight = $weight
        ->execute()
        ->fetchField();

      // If there is no menu item, set the weight to 0.
      if (!$weight) {
        $weight = 0;
      }
    }
    else {

      // Get the weight for this node.
      $weight = db_select('weight_weights', 'w');
      $weight
        ->fields('w', array(
        'weight',
      ))
        ->condition('entity_id', $result->nid);
      $weight = $weight
        ->execute()
        ->fetchField();

      // If there is no weight, set the weight to the default.
      if (!$weight) {
        $weight = $default;
      }
    }
    db_merge('weight_weights')
      ->key(array(
      'entity_id' => $result->nid,
    ))
      ->fields(array(
      'entity_id' => $result->nid,
      'entity_type' => 'node',
      'weight' => $weight,
    ))
      ->updateFields(array(
      'weight' => $weight,
    ))
      ->execute();
  }
}