8

I'm trying to create a field widget that is similar to the normal term reference widget with one key difference. The user needs to be able to add multiple new taxonomy terms at the same time, this may demonstrate it better:

enter image description here

I've got to a point where the functionality is similar to what the taxonomy/options/list modules provide (i.e. the checkboxes/radios are shown, saving etc.)

Here's important bit of the code I'm using in hook_field_widget_form():

 $element += array(
    '#type' => $multiple ? 'checkboxes' : 'radios',
    '#default_value' => $multiple ? $default_value : reset($default_value),
    '#options' => $options,
    '#value_key' => $value_key,
    '#element_validate' => array('options_field_widget_validate'),
    '#properties' => $properties,
  );

What's throwing me here is that $element isn't your usual form array so I'm not sure how to add the textfield and and button - especially as their values aren't meant to be saved. What's the best way to add these fields to my widget?

As for adding the Javascript needed, am I right in thinking I should use #ajax that calls a function that rebuilds the #options for the checkboxes?

Thanks in advance!

EDIT - My initial question was quite vague, here's some more details on what I've tried:

  $element += array(
    '#type' => 'fieldset',
    '#delta' => $delta,
  );

  $element['value'] = array(
    '#type' => $multiple ? 'checkboxes' : 'radios',
    '#default_value' => $multiple ? $default_value : reset($default_value),
    '#options' => $options,
    '#value_key' => $value_key,
    '#element_validate' => array('options_field_widget_validate'),
    '#properties' => $properties,
  );

  $element['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Add another'),
    '#weight' => 5,
  );

This a is an amalgamation of various examples I've seen, and seems to get me closer. The problem is when I submit the entity form I get the following error:

Notice: Undefined index: tid in taxonomy_field_is_empty() (line 1402 of modules\taxonomy\taxonomy.module).

I've taken a closer look at this function and it's expecting $item to have the structure $item = array('tid', 2). When using the above code there seems to be a delta coming from somewhere and the structure of $item ends up being $item = array(0, array('tid', 2). I can't work out where '0' is coming from?

ANOTHER EDIT

I've been able to fix the above error with the following code by altering $element like so:

$element += array(
    '#type' => 'fieldset',
    '#process' => array('taxonomy_free_entry_ignore_parent'),
);

and adding the function:

function taxonomy_free_entry_ignore_parent(&$form, &$form_state, $complete) {
  array_pop($form['#parents']);
  return $form;
}

When I save the entity I'm getting a message telling me it's been saved. When I check the table for the field, it isn't being saved. What am I doing wrong?

2

I'm sure you could do this in hook_field_widget_form() (which I'm guessing is where your current code is) by just adding a child element to your existing element, e.g.

$element['text'] = array(
  '#type' => 'textfield',
  '#title' => 'Title',
  // etc...
);

If that doesn't work I know it will definitely work if you use hook_field_widget_form_alter().

For example, the following is a small function which enables the alt field for an Image field type using the Media Selector widget (for some reason it's not enabled by default):

function media_image_attributes_field_widget_form_alter(&$element, &$form_state, &$context) {
  if ($context['field']['type'] == 'image') {
    $element['alt'] = array(
      '#type' => 'textfield',
      '#title' => 'Image Description',
      '#weight' => 10000,
      '#default_value' => count($element['#default_value']) ? $element['#default_value']['alt'] : ''
    );
  }
}

As far as the ajax functionality goes, I think you're definitely going along the right lines. I think the Field Example module in Examples has some sample code which might help.

| improve this answer | |
  • Thanks for the answer, I've up-voted it as it's given me some good information to go on but hasn't quite answered it! Check out my edit that gives some more information about what I've tried thanks to your answer. – splatio Mar 20 '12 at 21:23
  • @splat have you been able to implement a working widget? Would you be willing to share it? – Joe Beuckman Jul 9 '12 at 22:23
  • Sorry Joe, I wasn't able to work this out and ended up using a complexly different approach! If you work it out post it here as I'd be interested how for the future. I'd also happily accept it as the answer. – splatio Jul 11 '12 at 14:17
  • @splatio Can you give some info on you alternate approach? – vfclists Sep 9 '12 at 16:01
1

The Module Hierarchical Select at least gets very close to what you want to achieve. Its widget looks a little different but basically does the same job and would be a good starting point for customisation. Take a look at the taxonomy demo Places I've visited.

Another option might be the Autocomplete Deluxe module.

| improve this answer | |
0

I just found Taxonomy Other module that may suits your needs.

This is a simple module to add an optional '- Other -' to taxonomy select lists. It allows users with the right permissions to create new terms.

For Drupal 7 port, check: Port Taxonomy Other to D7 (direct link).

| 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.