2

I am trying to make a link in a view which, when clicked on, will call a function in a custom module to make a database call. I have made a link by making a Global:custom text field (because thats the way it seemed I had to do it from some posts online), set it to a path accompanied with some arguments. I made a custom module used the hook_menu:

function example_menu() {
$items = array();
$items['path_name'] = array(
'title' => t('Example'),
'page callback' => 'my_callback',
'access arguments' => array('access content'),  
'type' => MENU_CALLBACK,
);
return $items;
}

where function my_callback does a database call.

When I click on the link I just get a blank page with nothing on it. Which pieces am I missing here? The first thing I thought was that I needed to route the link to a page (hence the name 'page_callback') but how would I do this? All I really want done is a database call, I don't mind if you stay on the same page.

EDIT: Additionally, what I have found is that on the content management page there ares some errors on top which tell me that I'm missing arguments 1 through 4 (there were four arguments), which definitly can be part of the problem. The function starts off:

function example_my_callback($staff_action, $first_name, $last_name, $email)

and the url for the link was path_name?staff_action=[varible name found in views]...etc Is there any issue here?

5
  • if your page callback doesn't return anything, that's what will show up on the screen...nothing. if, though, inside your callback function, after you do whatever you want dbwise, you return 'I was here'; or anything else, it will be painted on the screen on your page.
    – Jimajamma
    Commented Apr 5, 2012 at 15:26
  • also, ?staff_action= .... etc are not arguments to the menu callback, they are http variables and if needed can be accessed from inside your function via $_REQUEST or $_GET. the only arguments passed to your menu callback would be defined in your menu() hook (which you aren't doing currently).
    – Jimajamma
    Commented Apr 5, 2012 at 15:31
  • @jimajamma so, what are the access arguments defined in the hook_menu? where are they passed from? Commented Apr 5, 2012 at 16:09
  • access callbacks and arguments are used by drupal to allow/deny users access to this page. you could add in 'access callback' => array('user_access'), to your menu but it is the default so not needed. but the menu system calls that callback with your argument which basically says your users can go to this page if s/he can access content (from permissions). you could also use 'access callback' => TRUE, and no access arguments if you wanted anyone to be able to access the page.
    – Jimajamma
    Commented Apr 5, 2012 at 16:20
  • great info on menu hook: api.drupal.org/api/drupal/developer%21hooks%21core.php/function/…
    – Jimajamma
    Commented Apr 5, 2012 at 17:43

2 Answers 2

1

The page callback function generates the html that is shown on the page when you surf over to it, so, you would do something like this in it:

function my_callback() {

  // do your db stuff

  if ( // success ) {
    return '<div>Success</div>';
  else {
    return '<div>Failure</div>';
  }

}

And a link to it is just like any other html link, it goes to a page. If you want to stay on the current page, you will need to do it via ajax/ahah or something similarly fun with javascript/jquery etc.

2
  • that brought my page back. But actually, I didn't need to do a return at all. Its just that I had two if statements and neither was true and didn't have a last else if. Once I put that on, the drupal screen was rewritten automatically. But, I would be interested to hear how one would go about staying on the current page via ajax. and I have never used ahah at all Commented Apr 5, 2012 at 16:18
  • drupal.org/node/305747 has an example of it. there, the example they have is clicking on a link that populates a div on the page with an image, but you could easily take what they have there and modify it to your purposes, eg, clicking on the link goes and does your db call and then returns a success.jpg or failure.jpg :)
    – Jimajamma
    Commented Apr 5, 2012 at 18:14
1

The page callback must be the name of the function that will return the data from database

You must create a function like this :

function my_callback(){
// here you will connect and pull up data from the database
return $your_data;
}

Also don't forget to clear the cache and read about the database API here

1
  • what your function above will do is paint the screen with $your_data, which I doubt is what he wants, unless it is html :)
    – Jimajamma
    Commented Apr 5, 2012 at 15:24

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.