1

I'm creating a module to introduce a new field type. I downloaded the example module by drupal and i start programming. But when I install the module the .install don't add colums to the dbtable related to new field.

 function field_instagram_field_schema($field) {

   watchdog('start field_instagram_field_schema()', 'We are in!');

   switch ($field['type']) {
    case 'instagram_gallery':
      $columns = array(
        'stream_type' => array(
          'type' => 'varchar',
          'length' => '25',
          'not null' => FALSE
        ),
        'input_tag' => array(
          'type' => 'varchar',
          'length' => '25',
          'not null' => FALSE
        ),
         'thumb_dimetion' => array(
          'type' => 'varchar',
          'length' => '25',
          'not null' => FALSE
        ),
        'thumb_number' => array(
          'type' => 'varchar',
          'length' => '25',
          'not null' => FALSE
        )
      );
      break;
  }

  return array('columns' => $columns);
}

UPDATED
When i install the module and create a field of this new type this function is not runned..

4
  • Have you definitely un-installed and re-installed the module rather than just disabling it and re-enabling it (they're different things)?
    – Clive
    Commented Nov 7, 2012 at 18:15
  • Yes, I do multiple times T_T
    – Shyghar
    Commented Nov 7, 2012 at 18:19
  • Sorry, I had to ask :) Just to rule out the more obvious things - is the module called field_instagram? Where is this code, the .install file or .module file? Have you tried putting some debug code in the function (a watchdog() call for example) to see if the function is definitely being executed when the module is installed?
    – Clive
    Commented Nov 7, 2012 at 18:20
  • I tryed right now to add a watchdog() call at the 1st line of this function and, whene i install the module (and create a field..), there isn't message in the log.. so the function is not runned..
    – Shyghar
    Commented Nov 7, 2012 at 18:32

1 Answer 1

2

Your code is correct, but nothing will happen when you install the module itself. It will only be invoked when you create a new field of type instagram_gallery.

But that is not all. The only module that will get's it hook_field_schema invoked, is the module that actually defined the field. This is done with hook_field_info, so you need to have the following code in your field_instagram.module file:

/**
 * Implements hook_field_info().
 */
function field_instagram_field_info() {
  return array(
    'instagram_gallery' => array(
      ...
    ),
  );
}

Since you are defining your own field, you also need to use

  • hook_field_is_empty
  • hook_field_formatter_info
  • hook_field_formatter_view
  • hook_field_widget_info (unless you can use an existing widget)

Update
It's hard to say way you have troubles. What the field module does is this:

module_load_install($field['module']);
$schema = (array) module_invoke($field['module'], 'field_schema', $field);

You can try to load the created field manually and run the hook to see if it works. You need to load the .install file like shown above. My guess is that you have something misnamed somewhere, but it's hard to say where exactly. The above could should with your info look like this:

$field = field_info_field('field_name_here');
module_load_install('field_instagram');
$schema = (array) module_invoke('field_instagram', 'field_schema', $field);

If the above works, you can try to debug the actual field creation, the code is from field.crud.inc line 134.

4
  • I never realised the hook only gets invoked when a field is created but it makes a whole lot of sense. Really useful thanks :)
    – Clive
    Commented Nov 7, 2012 at 18:38
  • I create this field type in my field_instagram.module and i have all this function in my module.. I check the database table after a field creation obviously..
    – Shyghar
    Commented Nov 7, 2012 at 18:40
  • @Shyghar Added more info - hope it helps.
    – googletorp
    Commented Nov 7, 2012 at 19:04
  • After some tests I tryed to rewrite the module and now it works -__-" I think it was a naming problem hard to find.. tnks a lot for the help ^^
    – Shyghar
    Commented Nov 13, 2012 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.