Call a function from a link in drupal 7 - php

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

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>');
}

Assist with Drupal Admin Blank page for custom functions or code

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',
),
);
}

How can I load a Drupal Form via ajax into a jQuery dialog?

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

How to get multiple parameters sent via drupal hook_menu

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
)
);
}

Drupal get parameters from url in edit form and populate the form

i have an employee table with fields like id, name, age and salary.
Iam showing the list of employee names in my custom module and when i click on an employee name, i have to show the edit form of that employee.
The employee names are given a link, as like:
<a href='".$base_url."/my_module/edit/".$employee->id."'>
and the corresponding menu path is configured as:
$items['my_module/edit/%'] = array(
'title' => t('My form'),
'description' => t('Edit employee'),
'page callback' => 'my_module_edit',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
And when i click on the 5th employee name, the url would be: ..../my_module/edit/5
So now my questions are:
How can i get the id (5) of the employee from the url in my my_module_edit function ?
How can i show the edit form of the employee, populating the fields (by querying with id) ?
For question 2: You have got it mixed up a bit, you don't create a custom 'page callback' menu item for forms, you should use 'drupal_get_form' as the 'page callback' with the name of your form() function (my_module_employee_form) as the 'page arguments'.
function my_module_menu() {
$items['my_module/employee/edit/%'] = array(
'title' => t('Edit employee'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_employee_form', 3),
'access arguments' => array('administer my_module'),
'type' => MENU_CALLBACK,
);
}
function my_module_employee_form() {
... read database and generate form..
}
function my_module_employee_form_submit($form_id, &$form) {
... read form and update database ...
}
Then in the function my_module_employee_form() you generate the form and populate it, and in my_module_employee_form_submit() you read the values back and store them in the database.
$request_parts = explode('/', $_SERVER['REQUEST_URI');
$employee_id = intval(array_pop($request_parts));
That's for your question #1. Not sure I understand #2
For your first question, you can get the arguments passed to a callback by using arg(0), arg(1), arg(2) etc...
<a href='".$base_url."/my_module/edit/".$employee->id."'>
For this href arg(0) == 'my_module', arg(1) == 'edit', arg(2) == '5', take a look at arg()'s documentation.
You can also parse the query string your self by accessing the $_GET['q'] variable.
For your second question, if your employee table is a Drupal table you can just make the link be the edit link to the Drupal node, like href="node/5/edit". If it is a custom table you will have to create your own form by implementing hook_form()

Categories