Drupal - Are "files[]" from custom_module.info not included on form submits?
Files listed in .info files are not automatically included when the module is loaded; they are loaded when a module tries to create an object for a class that PHP doesn't find. In that case, a callback registered with spl_autoload_register() will check the content of the Drupal registry to find which file needs to be loaded to allow the creation of the object.
The only way to load a file containing a function is to use module_load_include()
.
In the specific case, where the function to load is a form validation handler (or a form submission handler) that is added to a form altered with hook_form_alter()
(or hook_form_FORM_ID_alter()
), I would rather keep the form handlers in the same file containing the hook implementation. Technically, the file is loaded by hook_form_alter()
when it is not necessary, as the form handlers are used after the form has been submitted; so far the code worked, but it is not said that it would not work in future versions of Drupal.
Drupal 7 allows modules to implement hook_hook_info(), which is used from Drupal to know in which files the hook implementations are defined. The System module define its own implementation, which contains the following code:
function system_hook_info() {
$hooks['token_info'] = array(
'group' => 'tokens',
);
$hooks['token_info_alter'] = array(
'group' => 'tokens',
);
$hooks['tokens'] = array(
'group' => 'tokens',
);
$hooks['tokens_alter'] = array(
'group' => 'tokens',
);
return $hooks;
}
With this implementation of the hook, when Drupal is looking for the file containing the implementation of hook_tokens()
for the module custom.module, it will look for the file custom.tokens.inc contained in the directory where the module custom.module is installed.
Third-party modules are allowed to implement their own hook_hook_info()
, but it should be remembered they are modifying where Drupal looks for the hook implemented by other modules too, which means that changing the group for hook_token_alter()
changes the files where Drupal looks for any implementation of hook_token_alter()
.
It is possible to use hook_hook_info()
to keep some hooks in a file that is different from the module file, using an implementation similar to the following:
function custom_hook_info() {
return array(
'form_alter' => array(
'group' => 'form',
),
);
}
What I reported before is still valid; future versions of Drupal could stop working because they would not find where the form handlers are, even though they know where to find the implementation of hook_form_alter()
.
This is the reason I keep to put the implementation of hook_form_alter()
(or hook_form_FORM_ID_alter()
) and every related form handler in the module file.
Only classes are tracked and automatically included. Everything else needs to be included manually.
Also, I'm not really sure what you are doing here, you do not need to alter your own forms. Second, _submit()/_validate() callbacks should be in the same file as the form function itself then you don't need to include them manually.