0

How do I modify the taxonomy terms that are listed as a field in my Node edit form?

I need to be able to hide/unpublish/disable my taxonomy terms. Since there is no way to "unpublish" taxonomy terms the way you can unpublish a node, I have created a boolean field in my taxonomy term that holds its status (enabled/disabled).

I would like to use the value of this boolean field to determine whether or not to add a warning text in the taxonomy term name (eg. Termname [DISABLED]) so that users choosing that taxonomy term are properly warned.

If it matters, I'm currently using the select list widget.

I know I can look up each term in hook_field_widget_options_select_form_alter since the tids and term names are available in the $element, but I hope there is a solution which won't require me to re-load/re-fetch the term from the database again.

2
+50

I know I can look up each term in hook_field_widget_options_select_form_alter since the tids and term names are available in the $element, but I hope there is a solution which won't require me to re-load/re-fetch the term from the database again.

Unfortunately I think you will have to load them again and do the hook_field_widget_options_select_form_alter. There are a lot of functions to load taxonomy information which might be helpful and some of those probably have caches and were used in building the node form so the cache is already primed.

Is your concern around performance or code complexity?

  • Yes, concern about reloading/re-reading from database is due to performance. – nmc Apr 16 '12 at 13:05
  • As greggles says, you shouldn't worry about performance. The load function Drupal provides has already thought about that, so there won't be any extra queries executed. Hence, using hook_field_widget_options_select_form_alter is a perfectly good solution. – Olof Johansson Apr 16 '12 at 13:49
  • couldn't you just go into the taxonomy vocabulary admin screens and rename the term to term [DISABLED]? – Jimajamma Apr 16 '12 at 23:22
  • @Jimajamma - I am not the one making changes to the taxonomy terms. There are other site users who do this. Also, if changing the actual term name would change the way it's displayed everywhere and this is not what I want. – nmc Apr 17 '12 at 13:06
  • 1
    @nmc As well as the database cache tables there is a lot of static caching done throughout a Drupal page request. For example if you (or any other function in the page request) call $node = node_load(1);, the data will be loaded from the database. If it is called again during that same page load the cached node object will be returned, rather than hit the database again. This static cache pattern is used hugely throughout core, and definitely for Taxonomy terms. The others are right, don't worry about performance in this instance – Clive Apr 18 '12 at 13:17
1

A Drupal module called Term Status was created in 2011 (1 year before this answer was posted). It is available at https://www.drupal.org/project/termstatus and will let you publish and un-publish taxonomy terms. A "published" checkbox will appear on each term to let you control whether or not the term should be seen by others. If I recall correctly, it also provides a permission to let privileged users see unpublished terms.

  • This seems like one possible answer to the question. Thanks for adding it. I don't see this as the only or most obvious or best answer to the question. – greggles Sep 29 '15 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.