How to get multiple parameters sent via drupal hook_menu - php

I've got this menu hook below by which I'm sending two parameters to the function.
But in the function I am only receiving the first parameter.
Does any one know how to send and get multiple parameters using the Drupal menu system?
function drupal_menu(){
$items = array();
$items['drupal/%/%'] = array(
'title' => t('Welcome to the Hello World Module'),
'page callback' => 'drupal_page',
'page arguments' => array(1,2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function drupal_page($arg1, $arg2) {
return drupal_json(array('mess1'=>$arg1,'mess2'=>$arg2));
}

You're already doing it exactly the right way, if it's not working try flushing your caches. It's possible they haven't been cleared since you added the second argument, and Drupal caches items return from hook_menu() so it doesn't have to be called on each page.

You are on the right way anyway ... If it is not working for you, then try the following
function drupal_page($arg1, $arg2) {
$arg1_new = arg(1) ;
$arg2_new = arg(2) ;
return drupal_json(array(
'mess1'=>$arg1_new,
'mess2'=>$arg2_new
)
);
}

Related

Theme not working for hook_menu page

I have created a simple module with a hook_menu
function course_list_menu() {
$items['course-list'] = array(
'title' => 'Example Page',
'page callback' => 'course_list_page',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function course_list_page() {
print '<h1>WILL BE UP SOON </h1>';
print '<h3>this page is getting build<h3>';
}
As i saw in examples on youtube and some other sites, this text should come in the content region when i visit the link ( with header and footer ). But in my case it is coming in a blank page
Am i missing some thing ?
How can i display this content int the content region.
My current output is like http://prntscr.com/bpff9q
using bootstrap theme
You need to return something that Drupal can work with and apply to the template files.
Try this:
function course_list_page() {
return array('#markup' => '<h1>WILL BE UP SOON </h1><h3>this page is getting build<h3>');
}

Drupal redirect when access denied

We use a module in our projekt named "certificate". There is one Function in the *.module file which contains this:
function certificate_menu() {
$items['node/%node/certificate'] = array(
'title' => 'Certificate',
'description' => 'Display earned certificate for this node',
'page callback' => 'certificate_node_certificate',
'page arguments' => array(1),
'access callback' => 'certificate_can_access_certificate',
'access arguments' => array(1),
'file' => 'certificate.pages.inc',
'type' => MENU_LOCAL_TASK,
);
}
There is a "certificate_can_access_certificate" callback to check if the User has Access to download a certificate.
Whan I now try is to make a redirect to page "/my/another/access/denied/page/for/certificate" when this callback returns false.
What is now the recommended way to solve this ?
1) Manipulate the callback function and everytime when its returned "False" I just write an exit; there and redirect before with location() ?
2) Is there a way to create a function in my own custom module to make this redirect possible ?
3) Do I have to manipulate the function certificate_menu() in a special way ?
I do not know much about Drupal so I dont know whats the best way to do and how I have to do this ...
You can use "drupal_goto" function within your access callback to redirect.
Here's an example, where if you add ?doredirect=true it will redirect from the access function.
function certificate_menu() {
$items['mytestpage'] = array(
'title' => 'Certificate',
'description' => 'Display earned certificate for this node',
'page callback' => 'certificate_testpage',
'access callback' => 'certificate_access',
);
return $items;
}
function certificate_testpage() {
return 'testing!';
}
function certificate_access() {
if(isset($_GET['doredirect'])) {
drupal_goto('', array(), 301);
}
return 1;
}
Also, please note, you need to return $items within your hook_menu, otherwise your page callback won't register.

Call a function from a link in drupal 7

I'm creating a table with a delete field in every row.
The delete fields are links. Now I'm wondering; how can I call a function (to delete the data from the database) when you click the link.
the code for the the table:
$header = array("booking day" , "where" , "nr of people", "cancel");
$rows = null;
foreach ($res as $booking) {
if(isset($venues[$booking->nid]->title)){
$rows[] = array(
date("d-m-Y",
$booking->date),
$venues[$booking->nid]->title,
$booking->num_guests,
l('delete', "LINK HERE" ),
);
}
}
I've found an example online that dictates me to do make a new menu page like this:
$items['node/%node/delete_slot'] = array(
'title' => 'Delete slot',
'page callback' => 'bookings_delete_booking',
'page arguments' => array(2),
'access arguments' => array('access content'), // whatever see above
'type' => MENU_CALLBACK
);
And a new function like this:
function bookings_delete_booking($identifier_for_what_percent_is) {
dsm('test');
}
When I click the link it goes to a 404 page.
Does anyone have any idea how to make this work?
-Thanks
How is the table being generated? Could you not generate it via a view as this has node[delete] option already included?
You will need to flush the cache (page registry) as it will not pick your code up.
Also you want the page arguments line to be the following
'page arguments' => array(1)
as your argument is positioned second in the URL

Drupal dynamic internal redirect

What I want is pretty simple. I have registered a path
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'title arguments' => array(2),
'page callback' => 'taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
When this menu item is triggered, I want to redirect (without changing the url) to a taxonomy page, of which the term is chosen in a function that runs when this function is called.
How can I do this (especcially without changing the url) ?
You can't call taxonomy_term_page directly as your page callback as you'd need to provide a load function to load the term, which is just going to be too difficult with the setup you've got.
Instead, define your own page callback as an intermediary and just return the output from taxonomy_term_page directly:
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'page callback' => 'spotlight_taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function spotlight_taxonomy_term_page() {
// Get your term ID in whatever way you need
$term_id = my_function_to_get_term_id();
// Load the term
$term = taxonomy_term_load($term_id);
// Make sure taxonomy_term_page() is available
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
// Return the page output normally provided at taxonomy/term/ID
return taxonomy_term_page($term);
}

Drupal drupal_get_form

I trying to get the twitter_admin_form and twitter_user_settings form in a div.
/**
* Get twitter form for user
* #param $account
* #type user object
*/
function getTwitterForm($account){
//module_load_include('inc', 'twitter');
module_load_all();
$twitter_form = drupal_get_form('twitter_admin_form');
return $twitter_form;
}
I get a get a drupal error.
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'twitter_admin_form' was given in .../includes/form.inc on line 372.
twitter.module
/**
* Implementation of hook_meu()
*/
function twitter_menu() {
$items = array();
$items['admin/settings/twitter'] = array(
'title' => 'Twitter setup',
'description' => 'Twitter module settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_admin_form'),
'access arguments' => array('administer site configuration'),
'file' => 'twitter.pages.inc'
);
$items['user/%user_category/edit/twitter'] = array(
'title' => 'Twitter accounts',
'page callback' => 'twitter_user_settings',
'page arguments' => array(1),
'access arguments' => array('add twitter accounts'),
'load arguments' => array('%map', '%index'),
'weight' => 10,
'file' => 'twitter.pages.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
I'm not sure what I'm doing wrong. The twitter_admin_form doesn’t have any arguments hence I thought it would be simple to get and display.
I’m new forms/menu so I’m not 100% sure what %user_category, %map and %index are and how to pass them in.
How do you know what the valid forms are?
When you call drupal_get_form you supply a form id, which is the function that Drupal needs to call. The problem you are experiencing is that Drupal cannot find the function: twitter_admin_form.
Either it's located in an include file, and you need to include it, or you have named it something else.
The error you get stems from the line:
$twitter_form = drupal_get_form('twitter_admin_form');
It expects 'twitter_admin_form' to be a valid callback function, but can't find it. This is probably because the related file 'twitter.pages.inc' is not included at the time of your call.
You could fix that via a:
module_load_include('inc', 'twitter', 'twitter.pages');
(Given the commented line in your code sample, you seem to have tried something like this, but forgot to give the name of the file to include).

Categories