Have you seen ugly switches in widget_form? formatter_view? Yup. These are not hooks. These are callbacks to be defined in hook_field_info. Easy to change, just needs typing.

Comments

chx’s picture

Example:

function text_field_widget_info() {
    'text_textfield' => array(
      'label' => t('Text field'), 
      'field types' => array('text'), 
      'settings' => array('size' => 60), 
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT, 
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
      'widget callback' => 'text_textfield_field_widget_form',
    ), 
}

and so on for every field type callback.

chx’s picture

And, change $function = $instance['widget']['module'] . '_field_widget_form'; to $function = $instance['widget']['widget callback']; in field_default_form

chx’s picture

Issue tags: +Novice

Find me on IRC if it doesnt feel so novice :)

Other such hooks are http://api.drupal.org/api/drupal/modules--field--field.api.php/group/fie... load, validate, submit, presave, insert, update, delete, delete_revision, is_empty, schema, prepare_view, prepare_translation, (these callback definitions all go into hook_field_info) then formatter_prepare_view, formatter_view (these go into hook_field_formatter_info) and then widget_form, widget_error (these go into hook_field_widget_info).

catch’s picture

pat redmond’s picture

Assigned: Unassigned » pat redmond
Status: Active » Needs review
Issue tags: +DDU2012
StatusFileSize
new8.17 KB

I've run through and changed all the files which contain "_field_widget_info()". Getting some errors from simpletest -> not really sure why or where to fix. I figure it is a good idea to fix these before running through the other 'hooks'.

deciphered’s picture

Status: Needs review » Needs work
+++ b/core/modules/field/field.api.phpundefined
@@ -708,33 +709,36 @@ function hook_field_is_empty($item, $field) {
+	  'behaviors' => array(
...
+	  'behaviors' => array(
...
+	  'behaviors' => array(

+++ b/core/modules/field/field.form.incundefined
@@ -60,7 +60,12 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode,
+	  if ( isset( $instance['widget']['widget callback']) ) {
+	    $function = $instance['widget']['widget callback'];
+	  } else {
...
+	  }
+	  ¶

+++ b/core/modules/field/modules/number/number.moduleundefined
@@ -321,7 +321,13 @@ function number_field_widget_info() {
+	  'behaviors' => array(
...
+	),

+++ b/core/modules/field/modules/options/options.moduleundefined
@@ -46,6 +46,8 @@ function options_field_widget_info() {
+	  'widget callback' => 'options_select_field_widget_form',
+	  'settings' => array('display_label' => 0),

@@ -53,6 +55,8 @@ function options_field_widget_info() {
+	  'widget callback' => 'options_buttons_field_widget_form',
+	  'settings' => array('display_label' => 0),

@@ -60,6 +64,7 @@ function options_field_widget_info() {
+	  'widget callback' => 'options_onoff_field_widget_form',

+++ b/core/modules/field/modules/text/text.moduleundefined
@@ -453,16 +453,31 @@ function text_field_widget_info() {
+	  'behaviors' => array(
...
+	  'behaviors' => array(
...
+	  'behaviors' => array(

+++ b/core/modules/field/tests/field_test.field.incundefined
@@ -169,6 +169,7 @@ function field_test_field_widget_info() {
+	  'widget callback' => 'test_field_widget_field_widget_form',

@@ -177,6 +178,7 @@ function field_test_field_widget_info() {
+	  'widget callback' => 'test_field_widget_multiple_field_widget_form',

+++ b/core/modules/file/file.field.incundefined
@@ -421,6 +421,7 @@ function file_field_widget_info() {
+	  'widget callback' => 'file_generic_field_widget_form',

+++ b/core/modules/image/image.field.incundefined
@@ -273,6 +273,7 @@ function image_field_widget_info() {
+	  'widget callback' => 'image_image_field_widget_form',

+++ b/core/modules/taxonomy/taxonomy.moduleundefined
@@ -1361,6 +1361,7 @@ function taxonomy_field_widget_info() {
+	  'widget callback' => 'taxonomy_autocomplete_field_widget_form',

Tabbing is incorrect, need to use two spaces instead of tabs.

swentel’s picture

Yeah, this is pretty much duplicate of the one that catch mentions in #4. I vote duplicate, but let's at least get one more opinion about it.

pat redmond’s picture

Status: Needs work » Needs review
StatusFileSize
new7.48 KB

OK, this has me stumped. For some reason it isn't displaying the widgets correctly... I originally thought that it was an issue with the text.test, but I don't think that is the case. For some reason the 'widget callback' doesn't seem to be registering.

The other weird thing is that when I change (in text.module)

'settings' => array('rows' => 20, 'summary_rows' => 5),

to

'settings' => array('rows' => 21, 'summary_rows' => 5),

it doesn't seem to make any difference - I var_dump($instance) and 'rows' remains at 20...

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-8.patch, failed testing.

Anonymous’s picture

Assigned: pat redmond » Unassigned

Since there hasn't been activity for 4+ weeks, I'm going to move this back to unassigned.

cedarm’s picture

Assigned: Unassigned » cedarm

Working on this at DrupalCon sprint..

cedarm’s picture

StatusFileSize
new6.96 KB

As for 'widget callback' not "registering", the instance needs it to be populated, just like 'module', in _field_info_prepare_instance_widget(). The newly defined callbacks in various hook_field_widget_info() were not all correct. Checked list, number, options, text, file, image, taxonomy, and test/field_test.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new6.97 KB

Oops, let's try that again.

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-13.patch, failed testing.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.64 KB

Looks like we also need to set $instance['widget']['widget callback'] in _field_write_instance(). Is this right?

Still need to handle _field_widget_error.

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-14.patch, failed testing.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.8 KB

Do we just need to check if isset 'widget callback' ?

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-17.patch, failed testing.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.85 KB

Missed one isset check.

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-19.patch, failed testing.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.5 KB

_field_write_instance() doesn't care about 'widget callback'; rather 'widget callback' was missing from test_field_widget (I had only added it to test_field_widget_multiple).

cedarm’s picture

StatusFileSize
new8.47 KB

Changed 'widget callback' to 'widget form' and added 'widget error'. Is this the right direction?

Status: Needs review » Needs work

The last submitted patch, field-hooks-1333218-22.patch, failed testing.

cedarm’s picture

Status: Needs work » Needs review
StatusFileSize
new8.04 KB

Ok, forgot to put 'widget error' into _field_info_prepare_instance_widget(), but I don't think this is right. Why should the instance even need this information? Let's call field_info_widget_types() and get the callbacks from there instead of the instance.

joachim’s picture

Status: Needs review » Needs work

> Changed 'widget callback' to 'widget form' and added 'widget error'. Is this the right direction?

I would say not. There is a tendency in core (and contrib too) to name properties in an info array that are callbacks 'foo bar callback', eg in hook_menu(). That helps make it clear what it is.

cedarm’s picture

So 'widget form callback' and 'widget error callback' then? And for load, validate, submit, presave, insert, update, ..., should these be 'load callback', 'validate callback', etc? Or should it be 'field load callback', 'field validate callback', etc?

Naming convention aside, is it correct to be getting these from the field info instead of the instance?

amateescu’s picture

I'm not sure that we should waste more energy on this. Field API is going the OO route anyway and will be rewritten when plugins land in D8. #1497366-5: Introduce Plugin System to Core

cedarm’s picture

Assigned: cedarm » Unassigned

That's just what yched said in #702586-9: hook_field_load() and friends are not real hooks, but plugins and OO sound more certain now. If that's the direction for sure let's close this as a duplicate. Unassigning for now.

I am still willing to work on this, even if it's only a stepping stone to determine all of the spots to change for fields. I learned a lot anyway :)

swentel’s picture

Status: Needs work » Closed (duplicate)

Everything are now plugins, much nicer :)