How to prevent deleting a term if used? Is there any way to prevent deleting a taxonomy term if it used in any node ?

I have a Taxonomy that is used in Field Collection in my Content Type , I want to warn/prevent deletion of any Term in that Taxonomy if it is used in any node.

I checked taxonomy_term_confirm_delete it is where the confirmation message is formed,
I read from some posts about using 

db_query("SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid = :aid", array(':aid' =&gt; $tid) )-&gt;fetchField()


to get the number of nodes a term is used in but this is not working in my case -maybe because the taxonomy is in field collection ?
I wrote an article tackling this problem.

In short, you have to use hook_form_alter on the form taxonomy_form_term and use the function taxonomy_select_nodes() to check if a node uses the term in question.

You can just copy the code from the article into your custom module (naming the 'mymodule_form_alter' accordingly to your module's name), flush the caches and that should do it.