0

Is there a way to have my Taxonomy Terms in Dropdown but also allow the user to enter a new term (Like autocomplete widget does) if they don't see their term in the select list?

So essentially I would have a backup to allow the user to create one.

My use-case is I have a bunch of businesses, when someone adds a new one, if they don't see their business type, they can just add a new one.

0

Install the Select or Other module

You can use the Select or Other module to add a dropdown with an Other option.

Selecting Other prompts the user to enter the new value in a separate field.

Your question asked to add the new business type directly; this module adds it to a separate field (however, an admin can later add the business type as a taxonomy term if you wish).

Advantages of this approach

  • You get admin approval of user input. This way a bad user can't enter something like Sex sex sex which then appears as a business type on your site.
  • You have control over how long the list gets instead of letting users add an unlimited number of terms.

Coding it yourself

You can also code this yourself pretty easily using the States API instead of installing the Select or Other module.

Add a new text field, field_businesstype_other, to store the Other values for field_businesstype.

Create a taxonomy term named Other in your "business type" vocabulary.

Then, in hook_form_alter(), add something like this:

  $form['field_businesstype_other']['#states'] = array(
    'visible' => array(
      'select[name="field_businesstype"]' => array(
        array('value' => '123'),
      ),
    ),
  );

Replace 123 with the term ID of the Other term you created.

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.