How to add tri-state checkboxes to form created with Form API? I want classical tr-state checkboxes, like the ones on the picture below:



Checked would mean "true for node and all it's children", intermediate "true for some children, leave it as it was" and uncecked "false for all children". Desired values (but that's not a must) are:


checked - TRUE
unchecked - FALSE
intermediate - NULL


Is there a module to provide control like this? Or to provide intermediate value for regular checkboxes?

I need it for Drupal 7, but I'll gladly welcome answers for 6 and 8 too.
From what I understand, the indeterminate property of a checkbox is purely a visual thing, and also cannot be set with HTML, which in turn means that you won't be able to use the FormAPI or create an indeterminate element without JavaScript.

Chris Coyier sets out some example situations though in this blog post, but an important quote is below (in case the post goes down).


  Here are some things to know about indeterminate checkboxes:
  
  You can't make a checkbox indeterminate through HTML. There is no
  indeterminate attribute. It is a property of checkboxes though, which
  you can change via JavaScript.


I should imagine you could probably use Drupal.settings to your advantage here though, and run the check server-side for "true for some children", and pass an array of ID's through to Drupal.settings. Following that run something like the following in a Drupal behaviour:

 // This is an array of IDs
 trueForSomeChildren = Drupal.settings.myModule.trueForSomeChildren;

 $(trueForSomeChildrenjoin(', ')).attr("indeterminate", true);


Howeverm I don't think there's an already-built module to do what you want. There is a patch here for the FacetAPI where they allow for indeterminate, but it kind of follows this example.