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);
Related
I have been working on creating a custom form with an autocomplete field that will search the database for a specific content type's field values and bring back the results so the user can select one for that input box.
My main problem is actually knowing how to target the specific field in the database.
The database information resides in a content type called 'Parts' and within that content type there is a field called 'cross_reference'.
We have thousands of parts and each one has many cross reference values as the 'cross_reference' field is set to unlimited. I need the autocomplete of the custom form to look through these 'cross_reference' values.
Menu and Page
function frontcross_menu(){
$items['frontcross'] = array(
'title' => 'Front Cross',
'page callback' => 'frontcross_page_callback',
'access arguments' => array('access content'),
'description' => t('Front cross'),
'type' => MENU_CALLBACK,
);
$items['cross/autocomplete'] = array(
'title' => 'Autocomplete for cross',
'page callback' => '_cross_autocomplete',
'access arguments' => array('use autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
function frontcross_page_callback(){
return drupal_get_form('frontcross_form');
}
Form
function frontcross_form($form,&$form_state){
$form = array(
'#method' => 'post','#default_value' => 'Status Message',
'#id' => 'frontcross_form',
'field_across_reference_value' => array(
'#type' => 'textfield',
'#required' => FALSE,
'#autocomplete_path' => 'cross/autocomplete',
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
'#name' => t('Submit'),
'#id' => t('Submit'),
),
);
$form["#submit"][] = 'frontcross_submit';
return $form;
}
Submit
This function puts the form information into a URL query string on the catalogue results page
function frontcross_submit($form,&$form_state){
$options = array('' => TRUE);
$options['query']['field_cross_reference_value'] = $frontcrossval;
$frontcrossval = trim($form_state['values']['field_across_reference_value']);
if($frontcrossval){
$options['query']['field_cross_reference_value'] = $frontcrossval;
}
$form_state['redirect'] = url($GLOBALS['base_url'].'/cross-reference',$options);
}
Autocomplete Function
This is the part that I am stuck on. It needs to look though the database for values that belong to content type 'Part' and its field 'cross_reference' which has multiple values.
I have left the below code as the example I found.
function _cross_autocomplete($string) {
$matches = array();
$result = db_select('cities', 'c')
->fields('c', array('city'))
->condition('city', '%' . db_like($string) . '%', 'LIKE')
->execute();
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
drupal_json_output($matches);
}
I hope my explanation is concise enough. Help will be greatly appreciated.
Thanks
Jo
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.
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);
}
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
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).