I am trying to write my first Drupal custom module (using drupal 6). I got the basic module working but I would like to add another page to the project. For example, right now my module's path looks like this: www.mysite.com/my_custom_module. I would like to add another page like this: www.mysite.com/my_custom_module/my_sub_page. I've tried adding a new .module and .info file but this does not work. I've also tried adding new items to the menu hook, like this:
my_custom_module.module:
function my_custom_module_menu(){
$items = array();
$items['my_custom_module'] = array(
'title' => "My Custom Module",
'page callback' => "my_custom_module_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
$items['my_custom_module/my_sub_page'] = array(
'title' => "My Sub Page",
'page callback' => "my_custom_module_sub_page_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_module_info(){
$result = 'My Page URL was hit';
return $result;
}
function my_custom_module_sub_page_info(){
$result = 'My Sub Page URL was hit';
return $result;
}
In this example, if I go to .../my_custom_module it works fine. But, if I go to .../my_custom_module/my_sub_page, it still load, and displays my_custom_module. When I debug with a break point in each function, only my_custom_module_info is hit. Not the sub page. What am I doing wrong? I this even the correct way to create multi pages in a module? Just an FYI, each of these pages will have different audiences. The first page is to allow a user to submit some form data. The second is to allow an elevated user view the data.
Thanks
jason
Related
How to display table result in webform on custom page? Only result page for custom role.
Create custom module and code in it :
create a hook menu with role authorisation :
function MYMODULENAME_menu(){
$items['my/custom/path'] = array(
'title' => t('Results'),
'page callback' => 'mycallback',
'access arguments' => array('role_name'), // 'access callback' => user_has_role('role_name'),
'type' => MENU_CALLBACK
);
return $items;
}
create callback :
function mycallback(){
// get your data from database or what you want
// your code ... $datas = [...]
// construct lines
$rows = array();
foreach($datas as $data){
$rows[] = array($data['title1'], $data['title2']);
}
return theme('table',
array(
'header' => array('Title 1','Title 2'),
'rows' => $rows
));
}
Annexes :
https://www.drupal.org/docs/7/creating-custom-modules
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x
Use views module:
https://www.drupal.org/project/views
When you are creating the view for "Show" option select "Webform submissions" instead of default "Content" value. Then you can tune your view as usual - create page view or block view, what ever you need.
And later, when you continue editing that view for "Format" option select "Table" (if it's not already set, as default value).
I have added a tab to the node edit form in Drupal 7 like this (code snippet from hook_menu implementation, irrelevant lines removed):
'node/%/products' => array(
'title' => t('Products'),
'page callback' => 'some_function',
'page arguments' => array(
1
),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK
)
The tab shows up and works, however, the page is shown in the site's default theme, not in the admin theme. Also the other tabs are missing from the page that is displayed.
I tried fixing this by including this inside an implementation of hook_admin_paths, but it didn't make a difference:
return array(
'node/%/products' => TRUE,
);
How can I enforce my page to show in the admin theme and display the other tabs for the node edit form (such as "Edit", "Revisions" etc.) ?
EDIT: The box Use the administration theme when editing or creating content at admin/appearance is ticked, and system-defined pages such as node/%/edit display in the admin theme, but my new page does not.
I found out what I was doing wrong. In hook_menu % is used to denote an argument; in hook_admin_paths, these have to be replaced by asterisks. The following change to my implementation of hook_admin_paths solved it:
return array(
'node/*/products' => TRUE,
);
On /admin/appearance page, at the bottom of the page where you set the administration menu, check the value of the tickbox "Use the administration theme when editing or creating content".
I had the same problem and I solved with the following hook_menu :
<?php
function <mymodulename>_menu() {
$items = array();
$output['node/%node/mypath'] = array(
'title' => t('Title'),
'type' => MENU_LOCAL_TASK,
'page arguments' => array('node', 1),
'page callback' => 'callback_function',
'theme callback' => 'variable_get',
'theme arguments' => array('admin_theme'),
)
}
function callback_function() {
return 'My New Page.';
}
I think you need to have %node inside the path because that's the right way to "auto load" the node (Drupal take care of this) and pass it as an argument to callback_function where you can use the loaded node.
I'm not really sure what direction to go in.
I have a box on multiple pages that displays the status of various items. I want it easily update-able and would prefer it to be updated via a module. I'm not sure what direction to go in and just need a gentle push.
I have created a table in the drupal sql db but not sure how I would go about creating an admin tool in the drupal control panel to make changes to it.
Does anyone have any ideas of how I should go about this?
p.s. I'm using drupal 7
Custom admin pages can be defined as follows:
function mymodule_menu() {
$items['admin/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
'access arguments' => array('administer nodes'),
'title' => 'Foo settings',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Where mymodule is the name of your module and mymodule_abc_view is the function that returns the markup for your admin page
I am currently using Drupal 7 and I am writing a custom code such that users with a certain permission("use business dashboard") should see a menu item in their main menu. The problem is that only I(admin) can see this menu item. I have been able to create a custom permission on the permissions page and have set it to give access to "admin" and my user-specific role and have implemented the following code(nevermind the "xxxxxx" that is inplace of the module name, I would rather keep it anonymous for now, but just know that they are all in place of the machine-readable module name):
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access arguments' => array('use business dashboard'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function xxxxxx_permission(){
return array(
'use business dashboard' => array(
'title' => t('Have access to business dashboard'),
'description' => t('Allow user to send out SMS messages via database query forms'),
),
);
}
When I log in as my test user which has the role-specific permission of "use business dashboard" I cannot see the menu item. I am sure this is incredibly simple, but I have been Googling and prodding at the code for hours. Any help would be greatly appreciated!
Can't figure this out either. Can you try to break down the access callback, if it didn't work, at least it'll give you a tip about what's going on.
Your code can go like this:
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access callback' => 'my_custom_access_callback',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_access_callback()
{
if(user_access('use business dashboard'))
return TRUE;
return FALSE;
}
Till me if this works... Muhammad.
I would like to use the node-menu (see image) on a page in Drupal.
Is this possible and, if yes, how?
If the page you are referring is a custom page output from your module, and "mymodule/page" is the path for that page, for which you want the tabs "View" and "Edit," then you should implement hook_menu() using code similar to the following one:
function mymodule_menu() {
$items = array();
$items['mymodule/page'] = array(
'page callback' => 'mymodule_page_view',
'access arguments' => array('view mymodule page'),
);
$items['mymodule/page/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['mymodule/page/edit'] = array(
'title' => 'Edit',
'page callback' => 'mymodule_page_edit',
'access arguments' => array('edit mymodule page'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
If the page you are referring is a page that is shown at example.com/mymodule/page, and that should show what you see at example.com/node/7, then you can implement the following code, in Drupal 7:
function mymodule_url_inbound_alter(&$path, $original_path, $path_language) {
if (preg_match('|^mymodule/page|', $path)) {
$path = 'node/7';
}
}
The equivalent for Drupal 6 is writing the following code in the settings.php file:
function custom_url_rewrite_inbound(&$result, $path, $path_language) {
if (preg_match('|^mymodule/page|', $path)) {
$result = 'node/7';
}
}
I didn't write the symmetric code for hook_url_outbound_alter(), and custom_url_rewrite_outbound() as I suppose you are not interested in rewriting example.com/node/7 to make it appear as example.com/mymodule/page, but you are interested in making appear example.com/mymodule/page the same as example.com/node/7.
Okay; a node page usually has those View and Edit tabs. So my next question is to wonder why they aren't appearing on your node page already. Have you by chance created a custom page template for this node type and removed the code that prints the tabs? Or is there a chance you're logged in as a user that doesn't have permission to edit this type of node?
There must be a reason why you're not getting those tabs; they should be there, by default.
If you do have a custom page template for this node type, look for code that looks something like this:
<?php if ($tabs): ?>
<div class="tabs"><?php print $tabs; ?></div>
<?php endif; ?>
If you don't see code like that, try adding it.
If you DO see code like that, try to isolate what's different about this content type compared to other content types where you DO see those tabs.