1

I created a custom field widget implementing hook_field_widget_info(), created a custom formatter implementing hook_field_formatter_info() and also implemented hook_field_formatter_view() to generate the formatter's display.

Everything works as intended (validation, persisting values, formatter) with one glaring exception: I can't figure out how to make my widget use my formatter by default.

Every time I add a an iToggle field I need to manually change its formatter in Manage Displays for that content type.

Any ideas?

Here's my code:

function itoggle_field_field_widget_info() {
 return array(
   'itoggle' => array(
    'label' => t('iToggle Widget'),
    'field types' => array('list_boolean'),
    'settings' => array('display_label' => 1),
    'behaviors' => array(
      'multiple values' => FIELD_BEHAVIOR_CUSTOM,
    ),
  ),
);
}

function itoggle_field_field_formatter_info() {
  return array(
    'itoggle' => array(
    'label' => t('iToggle Widget'),
    'description' => t('Displays the field as an iToggle Widget'),
    'field types' => array('list_boolean'),
  ),
);
}

Thanks!

1

Short answer is you can't. You can only specify a default formatter for each field type itself in hook_field_info():

default_formatter: The machine name of the default formatter to be used by instances of this field type, when no formatter is specified in the instance definition. This formatter must be available whenever the field type is available (i.e. provided by the field type module, or by a module the field type module depends on).

4
  • That kinda sucks, so I create an awesome new Widget but by default it uses a crappy old Formatter? That's just not right... – Alex Weber Jan 10 '12 at 20:05
  • Yeah in this case it's probably better to create your own field type so you can set the default formatter and widget yourself. You can make your field type extend another type. This is what the image field in core essentially does and extends the file field type. – Dave Reid Jan 11 '12 at 6:55
  • Thanks Dave, I've been having other problems with my Widget extending Boolean Field Type and I've been avoiding creating my own Field Type but it might actually be better! – Alex Weber Jan 11 '12 at 15:21
  • What are your options for Drupal 6? – Patrick Feb 9 '12 at 23:19
0

So basically, as per Dave Reid's answer, it can't be done nicely.

That doesn't mean it can't be done at all (sort of), I found a decent workaround:

Implement hook_field_info_alter() and alter the default_formatter of the Field Type. Sure, its kinda rude because we update it for all ther Widgets of that Type, even ones that have nothing to do with our formatter.

That said, this is a very particular case (booleans) and it's safe to display the Widget regardless of the value. Also its just as easy to go to Manage Display and change the formatter back to the old one. It's a lot less disappointing than enabling an iToggle Widget and not getting the formatter. I think in this case the gains outweigh the losses!

0

Not sure how you defined your fields. I created them in my .install file instead of using the classic hook_field_info. In my case for some reason hook_field_alter doesn't work. So i had to define my own field type:

function MYMODULE_field_info_alter(&$info) {
$info['MYFIELD_TYPE']['default_formatter'] = 'MYFORMATTER';
}//THIS WORKS

otherwise defining on an existing field type like 'text' would interfere with other custom types:

function MYMODULE_field_info_alter(&$info) {
$info['text']['default_formatter'] = 'MYFORMATTER';
}//THIS WORKS

Let me know if you have a better solution. There might be a way to define it at creation type but dunno how to yet but so far all the solutions I tried were douchebags.

1
  • Thanks for sharing! The solution I went with is the one in the accepted answer (which is pretty much the same thing you said!) :) – Alex Weber Aug 17 '12 at 21:49

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.