2

Having this code:

function mymybase_update_7200(){
    if(!field_info_field('field_timer_exam')){
        $field = [
            'field_name'  => 'field_timer_exam',
            'label'       => 'Countdown timer',
            'type'        => 'mymy_field_countdown',
        ];
        field_create_field($field);

        $instance = [
            'field_name'  => 'field_timer_exam',
            'entity_type' => 'user',
            'bundle'      => 'user',
            'label'       => 'Countdown timer',
            'settings'    => [
                'user_register_form' => 0,
            ],
            'widget'      => [
                'type'      => 'mymy_field_countdown_widget',
                'settings'  => [
                    'display_label' => TRUE,
                ],
            ],
        ];
        field_create_instance($instance);
    }

    if(!field_info_field('field_timer_final_exam')){
        $field = [
            'field_name'  => 'field_timer_final_exam',
            'label'       => 'Countdown timer',
            'type'        => 'mymy_field_countdown',
        ];
        field_create_field($field);

        $instance = [
            'field_name'  => 'field_timer_final_exam',
            'entity_type' => 'user',
            'bundle'      => 'user',
            'label'       => 'Countdown timer',
            'settings'    => [
                'user_register_form' => 0,
            ],
            'widget'      => [
                'type'      => 'mymy_field_countdown_widget',
                'settings'  => [
                    'display_label' => TRUE,
                ],
            ],
        ];
        field_create_instance($instance);
    }
}

Which i think it is ok, field get created when running in the hook_menu page callback for example, but during installation the fields does not get created.

There is no error in the watchdog nor in apache logs, the field does not exist in the user entity nor tables like field_data_field_timer_exam

The update get fired, at system table i can see the schema_version columna get updated for my module

What could be happening?

1

The function hook_update_N() only gets called by running update.php. You also need to implement a hook_install(), for example like this:

function mymybase_install() {
  mymybase_update_7200();
}

Do note that this only runs when you enable the module for the first time. So if you already enabled the module, you can test this by first disabling it and then uninstalling it. If you want to run the function each time the module is enabled, you should use hook_enable().

| improve this answer | |
  • Sorry, i did not mention it but i'm using the reinstall module feature from the devel module to test this out. In my .info file the version is version = 7.x-2.0 so my function update should be called *_7200. By seeing the schema_version get update i'm guessing that the updates are fired but he code inside just does nothing – Gianni Di Falco Feb 10 '17 at 12:26
  • hook_update_N() doesn't get called when you install a module. Your version will automatically be set to your latest update function. So you need to implement a hook_install() or hook_enable(). Apart from that, does your code work when you manually call mymybase_update_7200()? – Wim Mostrey Feb 10 '17 at 12:31
  • I'm using a ´hook_install()´ but the updates function are not called in there, they are only defined in .install file. Running the code in a page callback from a item url defined in a hook_menu works. I have to mannualy add the updates inside the ´hook_install´ implementation function? – Gianni Di Falco Feb 10 '17 at 12:35
  • 1
    Exactly, you need to add the updates in hook_install() as well. – Wim Mostrey Feb 10 '17 at 12:39
  • Hmmmm i did not know that! Ty, let me try it – Gianni Di Falco Feb 10 '17 at 12:40
1

You need to either

  • Update the version number in your hook (hook_update_7001 => hook_update_7002)

or

  • Reset the schema_version in the system table so that it's lower than the number in your update hook

every time you run update.php, otherwise your update function will not be executed.

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