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);
}
Related
I am trying to learn Drupal. What I want to do is create a backend page (that is on main menu) where I can run my own functions and code. I have been doing research and found out that to do this I need to create a module. And if I run the "hook_menu" function - i can get that backend page to be on the menu. I found code for a drupal module that does this, and it loads a form for a "config settings" page. Here is the code:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'drupal_get_form',
'page arguments' => array('add_game_admin'),
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function add_game_admin() {
$form = array();
$form['add_game_maxdisp'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('add_game_maxdisp', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block."),
'#required' => TRUE,
);
return system_settings_form($form);
}
I modified it a little but it does work. What I want to do is do this but run my own functions and code on here and not the "drupal_get_form" function.
I tried to do this and just created a function to echo text and then put the function name in the "page callback" field of the array. This did work, it did execute my function on the page instead of the drupal form function, but the page was a blank white page with none of the "drupal backend styling or menus or anything"; it was literally just completely blank white webpage with just my text printed on it.
So I am thinking the "drupal_get_form" function not only puts a form on the page, but it also makes it so it is drupal backend page with proper header, footer, menus etc.
So I am thinking that i need a function like "drupal_get_form" but it has a "blank slate" where I can run whatever code or functions that I want.
Would anybody know anything about this or how to approach doing this?
Thanks so much...
Everything you need is to create a template, register it and use theme function in your code. You can take a look at this Quick Introduction to Drupal's hook_menu() and hook_theme()
So your code might look like:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'my_function',
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_function(){
// Call theme() function, so that Drupal includes the custom-page.tpl.php template
return theme('my_custom_template');
}
/*
* Implementation of hook_theme().
*/
function add_game_theme(){
return array(
'my_custom_template' => array(
// template file name will be custom-page.tpl.php
'template' => 'custom-page',
),
);
}
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.
I a stupid problem but nothing is working.
Lets say i have a url : something/4
Then i use a function users_view to display output.
Menu for users_view
$items['users/%'] = array(
'title' => 'your info',
'page arguments' => array(1),
'page callback' => 'users_view',
'access arguments' => array('view user'),
'type' => MENU_NORMAL_ITEM,
);
Now i wont to get to it ...
I used another function to get to it via a link in main menu. I created a function user_created that gives me the user_id(this is just an example)
function users_menu (){
$items=array();
$items['users/'] = array(
'title' => 'Your info',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array ('view user'),
'page callback' => 'user_created',
);
return $items;
}
function user_created(){
global $user;
$itemid=array();
$temp_user= $user->uid;
drupal_set_title('Your data');
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
foreach($result as $item) {
$itemid = $item->user_id;
}
REDIRECT to url users/$itemid
}
I have used drupal drupal_goto , $form['redirect'] but nothing happens ...
The problem is it just says users/
You have an error, you have to fetch the result like this :
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
$itemid = $result->fetchField();
drupal_goto('user/'.$itemid);
I have my module called mymodule.
In mymodule.module I have:
function mymodule_menu() {
$items['mymodule/ship/%node'] = array(
'title' => t('Shipment details'),
'page callback' => '_mymodule_addr',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'weight' => 0,
);
return $items;
}
I want to render the addressfield widget inside the page. Then I want to read form values.
Can you help me?
What about using the Field API:
Within your callback function use:
$node = node_load($parameter);
$my_field_value = '<front>';
$my_field_items = field_get_items('node', $node, 'field_my_field');
if ($my_field_items) {
$my_field_first_item = reset($my_field_items);
$my_field_value = $my_field_first_item['value'];
}
return l($my_field_value, 'Link name');
Have a look at: Drupal Field API and find the right function for you.
Also at: Drupal Examples are some useful examples that you might use.
As least, don't forget that you can use a custom .tpl file in your theme where you put custom markup to your field.
I have a module in which I build a form. I can hit form via a menu item that was also created in that module. When I try to load the form via ajax I get the entire page (header, form and footer) instead of just the form. Here's the menu item:
$items['sendmessage'] = array(
'title' => 'Send Message',
'description' => 'Send a message',
'page callback' => 'drupal_get_form',
'page arguments' => array('rmessages_message_form', 1),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
Here's the javascript:
$('.send_message').click(function(){
$('.send-message-dialog').dialog('open');
$('.send-message-dialog .dialog-content').load('/sendmessage/7');
}
);
If I load the URL via a browser, the form loads. Is there some way to get drupal to just render the form and return the HTML rather than trying to load the entire page.
This seems to be working...gotta remember the drupal_render function ;)
$items['sendmessage'] = array(
'page callback' => 'rmessage_send_message_form',
'page arguments' => array('rmessages_message_form', 1),
'access callback' => TRUE
);
Using drupal_build_form to get an array of items, which gets rendered by drupal_render()
function rmessage_send_message_form($form_id, $nid) {
$form_state = array();
echo drupal_render(drupal_build_form('rmessages_message_form', $form_state, $nid));
}
I think the nicest way to do it would be to have your form accessible from the normal URL (for those without JavaScript) and available to AJAX. You can do that like this:
function rmessage_menu() {
$items['sendmessage/%node'] = array( // Using the '%node' load argument ensures that the nid attempting to be accessed belongs to an existing node.
'title' => 'Send Message',
'description' => 'Send a message',
'page callback' => 'rmessages_message_form',
'page arguments' => array(1),
'access callback' => TRUE
);
return $items;
}
function rmessages_message_form($node) {
$form = drupal_get_form('rmessage_send_message_form', $node->nid);
// Just print the form directly if this is an AJAX request
if (isset($_GET['ajax'])) {
echo render($form);
// Halt page processing
drupal_exit();
}
// Otherwise return the form as normal
return $form;
}
Then in your JS you would just need to add the query string:
$('.send_message').click(function(){
$('.send-message-dialog').dialog('open');
$('.send-message-dialog .dialog-content').load('/sendmessage/7?ajax');
}
);
Hope that helps