Accessing functions of other modules

Events happening in the community are now at Drupal community events on www.drupal.org.
firnnauriel's picture

I am creating a sample module that will create a wrapper function of another module. For example, I have a profile_service module (that extends Service module) and would like to exposed an API named profile.get(). Within this function, it will call the user.get() of user_service. I have tried this in code:

function profile_service_get($uid) {
print_r("Calling the function of another module..");
include("../../services/services/user_service.inc");

   return user_service_get($uid);
}

I'm getting this error:
Fatal error: Call to undefined function user_service_get() in /var/www/groovenet2/sites/all/modules/profile_service/profile_service.module on line 133

Please help me on this. Thank you in advance.

Comments

Ok. Got the fix. Make sure

firnnauriel's picture

Ok. Got the fix. Make sure that the module is enabled, then just call the function directly, no need for include:

function profile_service_get($uid) {
   print_r("Calling the function of another module..");
   return user_service_get($uid);
}

The idea is all functions of an enabled module are accessible anywhere. That's just my thought. With this, one can easily exposed a Service API of all enabled modules by creating a wrapper function.

I am trying to do the same thing

tseven's picture

Can you please ellaborate how you got this working?

I am trying to call the views_service_get() function from within my Drupal 6 module.

My module looks like this so far:

function my_view_menu() {
    $items['api'] = array(
        'page callback' => 'my_view',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );

  return $items;
}

function my_view(){
    my_view_sevice_get();
}

function my_view_sevice_get(){
    $data = views_service_get('chat_message');
    print_r($data);
}

I would like the function my_view_sevice_get() to return the object generated by the view 'chat_message'.

I'm getting the Fatal error: Call to undefined function views_service_get() error message.

I'm new to Drupal development, please let me know what I'm doing wrong.

Thank you.

Solution

tseven's picture

Thanks to the helpful chaps at IRC #Drupal pointed me to http://api.drupal.org/api/function/module_load_include/6

I was able to include the views_service.inc and now I have access to the function :)

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: