Populating a Select Field with Taxonomy Terms

It's very easy to add a drop-down of taxonomy terms from a vocabulary to a content type. You need merely associate the vocabulary with a specific content type, and ta-da! A drop-down of terms appears in that content type. The terms are very easy to manage through the categories interface which means you can protect your content types from being edited every time a client wants a new option.

However, the drop-down generated by taxonomy is not very configurable. It's not a field per-say. You can't change it to appear as checkboxes for example - or choose only a sub-selection of terms from a vocabulary to appear.

But I have just discovered how to get the best of both worlds. First, create a vocabulary of terms you wish to use in a select box or series of checkboxes. Then, create a select field in your content type. Populate the allowed values list using PHP as shown in the example below:

$allowed_values = array();
$categories = taxonomy_get_tree(2);
sort($categories);
foreach ($categories as $category) {
$allowed_values[$category->tid] = $category->name;
}
return $allowed_values;

The '2' indicates the vocabulary id that you wish to pull from. And that's it! You now have a field with options based on a vocabulary!