2

I would like to have different field widgets for distinct forms. Is there a way to define multiple widget types for a single field? Here's my case: on the create content form, the widget for the field "Tags", which is a term reference field, is an autocomplete term; but on another form that uses that same field, I would like to use a select list for the widget.

  • For clarification: You have one form that you are displaying on two separate pages (A,B). On A you want the form to use autocomplete on the taxonomy field, on B you want the form to use select on the taxonomy field? – Chris Rockwell Jul 10 '13 at 15:00
  • Well, I have two forms that use the same field. I would like to use different widgets for the field on each form. I'll try to implement your hook below, that may work for me. – steve Jul 10 '13 at 15:23
  • Are both forms for the same content type? – Chris Rockwell Jul 10 '13 at 15:36
  • Yes. One is the create content form, the other is a view that is using views bulk operations to modify the entities, in this case the taxonomy fields. – steve Jul 10 '13 at 15:38
3

I'm seeking clarification but I'll go ahead and add this here:

As you commented, you can use hook_field_widget_properties_alter() like this:

function important_dates_field_widget_properties_alter(&$widget, $context) {
  // kpr($widget);

  $pageCheck = FALSE;
  $formPath = 'my/form/path';
  if ($formPath == current_path() || $formPath == request_path()) {
    $pageCheck = TRUE;
  }

  if ($pageCheck) {
    // switch from options to autocomplete
    /*if ($widget['type'] == 'options_select') {
      $widget['type'] = 'taxonomy_autocomplete';
      $widget['module'] = 'taxonomy';
    }*/
    // switch from autocomplete to options
    if ($widget['type'] == 'taxonomy_autocomplete') {
      $widget['type'] = 'options_select';
      $widget['module'] = 'options';
    }
  }
}

You can not define multiple widgets for a field. You can, however, create another field of type "Term Reference" and choose a different widget.Image showing two term reference field types

| improve this answer | |
  • I've already thought of that, but I need to edit the same field with both forms. I think I can use a hook_field_widget_properties_alter for this, but I'm not sure how to check for field instances in this case. – steve Jul 10 '13 at 14:20
  • This is great, very straightforward, but how can I do a check for the form I want to alter? I'm already using a hook_form_alter, but I don't want to nest this other hook in that. – steve Jul 10 '13 at 16:50
  • You would have to use some path checking. I will add an example to the answer. – Chris Rockwell Jul 10 '13 at 17:05
  • Could this also possibly be used as a custom field widget fallback somehow? I have a scenario, where I have created a custom field widget with extended features for authenticated users based on their account data, but I'd like to have my anonymous users also be able to fill in the field, but with the default widget, so I'd have to be able to call for another field widget if $user->uid==0. There's a reference in stackoverflow for that by Jonathan Rowny: stackoverflow.com/questions/9466895/… but not too many details. – Alari Truuts Mar 7 '16 at 16:46

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.