This code is in the category.module which starts around line 1461. I annotated the code with an example so I can figure out what I did. The code I added has to do with $removed_options and $unverified_options. I labeled the stuff I did with // new. The example works with subcities in order to get a nested category list as a target.

The problem I was having is both Rensselaer and Littleton would be displaying in the target list if New York was selected. The issue is that Littleton should be removed because it's parent Denver was removed.

I haven't done extensive testing, but it works for my case (so far anyway), so I figured I'd post it so others can check it out :)

azbok

//    Example Data:
//      States (container)
//        New York (selected)
//        Colorado
//
//      Cities (target container)
//        Albany (parent New York)
//          Rensselaer (parent Albany)
//        Denver (parent Colorado)
//          Littleton (parent Denver)
//
//
//    Assume 'New York' is just selected, then the potential options are Albany, Denver, Littleton
//    The process is to figure out which options are related to the selected choice

      $options = _category_node_select_options($cnid, $blank, $node_cats);
      if (isset($options) && is_array($options)) {
        $removed_options = array();  // new
        $unverified_options = array();  // new
        foreach ($options as $key => $value) {
          $parents_in_cont = FALSE;
          $matching_parents = FALSE;

          // The parent of Albany is New York
          // The parent of Rensselaer is Albany
          // The parent of Denver is Colorado
          // The parent of Littleton is Denver
          $parents = category_get_parents($key);

          // A valid option's parent is in the same container as the selected container
          foreach ($parents as $x) {
            $cid = $x->cid;

            // The parent category of New York is States
            // The parent category of Rensselaer is Cities
            // The parent category of Denver is States
            // The parent category of Littleton is Cities
            $parent_cat = category_get_category($cid);

            // Is New York container (States) the same as the selected item's container (States) == true
            // Is Rensselaer container (Cities) the same as the selected item's container (States) == false
            // Is Denver container (States) the same as the selected item's container (States) == true
            // Is Littleton container (Cities) the same as the selected item's container (States) == false
            if ($parent_cat->cnid == $parent_cnid) {
              $parents_in_cont = TRUE;

              // Is Albany's parent New York the same as the selected item New York == true
              // Is Denver's parent Colorado the same as the selected item New York == false
              if (isset($array[$cid])) {
                $matching_parents = TRUE;
              }
            }
          }

          // Remove item who's parent doesn't match the selected parent
          // Remove Denver since Colorado != New York
          if ($parents_in_cont && !$matching_parents) {
            $removed_options[$key] = $key;  // new
            unset($options[$key]);
          }

          // Rensselaer's parent Cities didn't match States, it is unverified
          // Littleton's parent Cities didn't match States, it is unverified
          if (!$parents_in_cont && !$matching_parents) {  // new
            $unverified_options[$key] = $parents;
          }
        }

        // If there's no removed options, just skip all of this processing
        if (count($removed_options) > 0) {  // new
          $child_is_an_orphan = false;
          foreach ($unverified_options as $cid => $parents) {
            foreach ($parents as $x) {
              $parent_cid = $x->cid;

              // Rensselaer's parent Albany was not removed
              // Littleton's parent Denver was removed so Littleton is an orphan
              if (! in_array($parent_cid, $removed_options)) {
                $child_is_an_orphan = true;
              }
            }

            // Littleton is an orphan so remove it
            if ($child_is_an_orphan == false) {
              unset($options[$cid]);
            }
          }
        }