Theme not working for hook_menu page - php

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

Related

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

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

Render drupal field widget in custom page

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.

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

Categories