2

I am trying to create a custom field widget to be used by the taxonomy term reference field. Basically I would like to select which taxonomy terms can be shown in the select list.

So far I am able to register the widget along with a custom settings page but when I use the widget on a node edit form. The values are not saved.

Here is the code below I have implemented a hook_widget_info hook_widget_settings_form and hook_field_widget_form from my understanding those three hooks should be enough

function my_module_field_widget_info() {
  return array(
    'my_module_term_reference_limit' => array(
      'label' => t('my module Term reference select'),
      'field types' => array('taxonomy_term_reference'),
      'description' => t('Allows you to limit the number of terms allowed in a select list'),
    ),
    'settings' => array(
        'terms' => array(),
      ),
  );
}



function my_module_utils_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
  $type = $instance['widget']['type'];
  $terms = taxonomy_get_tree($vocabulary->vid);
  $options = array();
  $default_value = array();

  foreach($terms as $key => $term){
    $options[$term->name] = t($term->name);
  }


  $form = array();
  if($instance['widget']['type'] == 'my_module_term_reference_limit') {
    $form['terms'] = array(
      '#type' => 'checkboxes',
      '#title' => t('My module terms'),
      '#options' => $options,

      '#description' => t('Select compatible templates'),
  );
}
  return $form;
}


function my_module_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $settings = $instance['widget']['settings'];

 $type = $instance['widget']['type'];

  $options = array();
  foreach($settings['terms'] as $key => $value){
    if($value !== 0){
     $options[$value] = t($value);
   } 
 }

$widget = $element;

  switch ($instance['widget']['type']) {
    case 'ii_term_reference_limit':
      $field['cardinality'] = 1;
      $widget += array(
       '#title' => t('my term select box'),
       '#type' => 'select' ,
       '#options' => $options,

  );
      break;
  }

  $element +=  $widget;
  return $element;

}

Thank you for your help

  • As an aside, $field['cardinality'] = 1; will have no effect as $field is not passed by reference – Clive Apr 21 '15 at 16:46
  • the whole example is a bot rough around the edges but your answer below solves my issue. Im sure they may be a few other issues icase anbody wants to use this example – t14 Apr 22 '15 at 15:46
1

Looks like you're just missing the column name for the field (tid).

Just before the return of the widget form function, try changing

$element +=  $widget;

to

$element['tid'] = $widget;
| improve this answer | |

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.