Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to create a widget for fields of type "taxonomy_term_reference". The widget "taxonomy_select" is very nearly what I need to build, but needs some modification. I have seen examples that start with a given widget to build a custom version. The function below fails because there is no "taxonomy_select_field_widget_form" defined. Where is the "taxonomy_select" form creation code?

/**
 * Implements hook_field_widget_form().
 */
function taxonomy_multiselect_add_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  $element += taxonomy_select_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);

  return $element;
}
share|improve this question

2 Answers

up vote 0 down vote accepted

The taxonomy_term_reference field type uses the core options_select widget and implements hook_options_list() to provide the values, the taxonomy module doesn't implement any of the widget code itself.

The options_select widget is defined in options_field_widget_info() in the code options.module file (inside the field module folder). The form elements themselves are built up in options_field_widget_form() in the same file. They're pretty well commented so you should be able to get a good idea of how they work.

I'd also recommend the Examples module if you haven't already got it, it has a field type/widget example module with good sample code and comments.

share|improve this answer

I'm now trying to customize the core options multiselect with this code:

function taxonomy_multiselect_add_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  $instance['widget']['type'] = 'options_select';
  $element += options_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);

  return $element;
}

But my widget now builds a multiple-value table with the default first row being the multiselect. How can I build my own identical version of the multiselect to begin modifying?

share|improve this answer
UPDATE: I now have a good starting point for modifying the options_select widget. I needed to supply a 'behaviors' array to my hook_field_widget_info that sets 'multiple values' => FIELD_BEHAVIOR_CUSTOM – Joe Beuckman Jul 9 '12 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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