Best practice to have a 'category' fields where admin can choose wich voices the user can choose

drupal.stackexchange https://drupal.stackexchange.com/questions/1125

  •  16-10-2019
  •  | 
  •  

Question

I have a content type that needs a 'category' field; this can both be a taxonomy vocabulary or a text field (with the select widget), it doesn't really matter. (I won't have more then 5-6 voices at once.)

However, those voices may change over time, and normal users should be able to choose only few values when creating a new node.

What is the best practice to achieve this?

My idea is to use a taxonomy vocabulary, where only administrators can insert new voices, but I will need some kind of attribute (e.g., a boolean) that I can use in a module in order to filter them in hook_form_alter(). Could this be a Drupal-ish way to achieve that?

Was it helpful?

Solution

You could use a Node Reference field where your 'Voices' are nodes that only administrators can make, that way you can restrict the maximum number of 'Voices' allowed in the Node Reference field settings, and you could turn 'Voices' off by un-publishing them, or if you need the boolean value for the Node output you could just add a Checkbox text field on the 'Voices' node and use the Custom Formatters module to render the Node Reference field out as you wish.

OTHER TIPS

I would use taxonomy terms for voices. From there I see two options to store their enabled/disabled states. You can either add a custom table (using the Schema API) with tid and state columns. Or, if you only have a few enabled voices, use a configuration variable (variable_get & cie.). In the later case, adding a configuration page/form to let administrator select the enabled voice is easy with system_settings_form. You can also alter the taxonomy_form_term form to add a state form field and custom submit handler to terms from your voice vocabulary.

Once you have the enabled/disabled state of voices stored, you can use hook_form_alter to alter the taxonomy form field and restricts its allowed value. You will have to make a decision on what to do when editing an existing node using a currently disabled voice. Either allow it to keep that voices or force it to use an enabled one.

Each term in a vocabulary has a 'weight field' to allow for custom sorting. That could allow you to set a specific order of terms. Are you sure you need the boolean? I assume it is to activate & deactivate a voice from showing in the list? You can just delete the term, but that would orphan the term on the nodes that used it.

You may be better off with a 'computed field' that is fixed once the node is published. http://drupal.org/project/computed_field

Allowing admins to edit taxonomies is probably safer than allowing them to edit a CCK select list.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top