By Wilted on
I'm creating a module just to learn, so it doesn't do anything interesting.
The folders structure is
testmodule
| -- include
| | - file1.inc
| | - file2.inc
|
| - testmodule.module
| - testmodule.infoInside testmodule.module I have a switch and according to a variable I have to call a function contained into include/file1.inc or into include/file2.inc
this is the code
function testmodule_call_external_functions($type)
{
switch ( $type )
{
case 'function1':
echo testmodule_function1(); //this is into file1.inc
break;
case 'function2':
echo testmodule_function2(); //this is into file2.inc
break;
}
}
Of course at the moment I have a "call to undefined function" in both cases.
I have already included those files into testmodule.info
How do I say to Drupal where to find those functions?
Comments
Not a very Drupal way of
Not a very Drupal way of doing things.
In Drupal, unless your module is providing an API, functions are generally triggered through menu callbacks registered in hook_menu() which for each menu callback allows you to define which file the callback function is in. Functions that are shared by the module are most easily defined in the main module file (yourmodule.module).
I see. I'll put those
I see. I'll put those functions inside the testmodule.module file.
Thank you.
Adding the PHP files to the
Adding the PHP files to the .info file with
files[]notation is actually more of suggesting, and they only work for interfaces and classes.You can include the PHP file before calling the function.
What's new and changing in PHP 8.4
Thank you.
Thank you.
Similar question... but I want to do it the drupal way.
I have some code in mymodule.module file which removes stored data (variable_del). A user can add or remove a feature at will which add or removes it from the database.
I also want to remove data that gets stored in the database by implementing hook_uninstall(). I am a big fan of reusing code where it makes sense. In this case, the logic to remove data is already written and sits in mymodule.module, is the method you mentioned a good way to reuse this code?
You should be able to just
You should be able to just call any function in a .module file.