drupal hook_menu_alter() for adding tabs - drupal

I want to add some tabs in the "node/%/edit" page from my module called "cssswitch".
When I click "Rebuild Menus", the two new tabs are displayed, but they are displayed for ALL nodes when editing them, not just for the node "cssswitch". I want these new tabs to be displayed only when editing node of type "cssswitch".
The other problem is when I clear all cache, the tabs completely dissapear from all edit pages. Below is the code I wrote.
function cssswitch_menu_alter(&$items) {
$node = menu_get_object();
//print_r($node);
//echo $node->type; //exit();
if ($node->type == 'cssswitch') {
$items['node/%/edit/schedulenew'] = array(
'title' => 'Schedule1',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_schedule',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>4,
);
$items['node/%/edit/schedulenew2'] = array(
'title' => 'Schedule2',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_test2',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>3,
);
}
}
function cssswitch_test(){
return 'test';
}
function cssswitch_test2(){
return 'test2';
}
Thanks for any help.

hook_menu_alter() is only called during the menu building process, so you can't do dynamic node type checks within that function.
However, to achieve what you want, you can do this with a custom access callback as follows:
// Note, I replaced the '%' in your original code with '%node'. See hook_menu() for details on this.
$items['node/%node/edit/schedulenew2'] = array(
...
'access callback'=>'cssswitch_schedulenew_access',
// This passes in the $node object as the argument.
'access arguments'=>array(1),
...
);
Then, in your new custom access callback:
function cssswitch_schedulenew_access($node) {
// Check that node is the proper type, and that the user has the proper permission.
return $node->type == 'cssswitch' && user_access('view cssswitch');
}
For other node types, this function will return false, thus denying access, and thus removing the tab.

Related

How to display table result on custom page webform in Drupal 7?

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).

Drupal - custom module - Generate dynamic URL alias

I have a custom drupal module. I have a demo pages for different APIs I have. (Like, host.com/demo/abc or host.com/demo/bcd or host.com/demo/def ...)
hook_menu()
$items['demo/%'] = array(
'page callback' => 'xxx',
'page arguments' => array(1),
'access callback' => 'xxx',
....
);
I want to change the URL to host.com/abc/demo for all the demo pages. Since I cannot a wild card at the beginning of the hook (Like %/demo) I am considering URL alias. But how can I generate url alias dynamically for all the demo pages?
The best solution :
http://www.zyxware.com/articles/3636/drupal-how-to-create-url-alias-for-custom-menu-implementation-of-hook-pathauto
Otherwise :
Before you need to know that all dynamical solution available is not totally safe , take care before using it.
With url_alias you can try this, but you have to restrict by defined path to avoid flood insertion :
function mymodule_menu(){
$items= array();
$items['demo/%'] = array(
'page callback' => 'xxx',
'page arguments' => array(1),
'access callback' => 'xxx',
);
return $items;
}
function mymodule_init(){
global $language;
$path_allowed = array(
'abc',
'dbe'
); // restrict to avoid flood
if(arg(1) == 'demo' && in_array(arg(0), $path_allowed)) { // detect demo mode
$path = arg(1).'/'.arg(0);
$alias = arg(0).'/'.arg(1);
$a = db_query('SELECT alias FROM url_alias WHERE source = :source AND alias = :alias', array(':source' => $path, ':alias' => $alias))->fetchField();
if($a == null) ) {
db_insert('url_alias')->fields(array(
'source' => $path,
'alias' => $alias,
'language' => $language->language
))->execute();
}
}
}
Otherwise, I'm not sure but you can try this too , you can handle it from hook_init but it can cause some issue form all module loading init
function mymodule_init(){
if(arg(1) == 'demo') { // detect demo mode
$mycallbacks = array( // define your callback related
'abc' => 'mymodule_callback1',
'dbe' => 'mymodule_callback2',
);
if(isset($mycallbacks[arg(0)]) && function_exists($mycallbacks[arg(0)])) { // if callback is available
call_user_func($mycallbacks[arg(0)]); // call it
}
}
}
function mymodule_callback1(){ //do what you want }
function mymodule_callback2(){ //do what you want }
So , mysite.com/abc/demo will execute mymodule_callback1() and mysite.com/dbe/demo will execute mymodule_callback2()
Etc..
Clear all caches and see :)

Drupal - Toggle Visibility of Menu Item Via Custom Permissions

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.

Replace the node-edit menu in Drupal

How can I change (unset or add) a new button in my edit-node menu? In this case, I would like to diable the 'Settings'-menu and add a new menu... I looked in the $form and the $form_state, but no luck there. At least, that's what I think...
EDIT
Module name: publication
Install: publication.install
File: publication.module
function publication_menu_alter(&$items) {
unset($items['node/%node/edit']);
}
EDIT 2
function publication_menu() {
$items['node/add/fiche'] = array(
'title' => 'New linked fiche',
'type' => MENU_LOCAL_TASK
);
return $items;
}
EDIT 3
What I'm trying to do is to allow my users to add some more content to existing content. So they are not allowed to edit the current content, only to add some details. So I thought, I delete the edit-button and replace it with an add-button and the add-button links to a page where he can create more content. That's it :)
You should use hook_menu_alter to unset menu.
function publication_menu_alter(&$items) {
// print_r($items);
// Find path you want to unset then unset it.
// Should be something like:
unset($items['your/menu/path']);
}
And hook_menu for defining new one. In your case I believe it should be menu type MENU_LOCAL_TASK since you want to add a new tab. Isn't it?
function publication_menu() {
$items['node/%node/something_else'] = array(
'title' => 'My title',
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_LOCAL_TASK
);
return $items;
}
function mymodule_abc_view($nid = NULL) {
return 'This node ID is '. $nid;
}

How do i get the data?(Drupal 6.x)

I have this form which accepts user’s input. What I like to do is, base on that user input, I’d like to retrieve data and display it back to user.
So far, I have implemented hook_menu and registered respective url of the form, and implemented a submit function referred by “#submit” attribute of submit button. I’ve also implemented data retrieval code and works great.
Here’s my problem – I don’t know how to display retrieved data. I’ve tried several approaches in an attempt to find the solution.
First, with theme function, hoping that printing the return value of it would display the data. Second, setting “#action” element of form array with newly registered url, as I thought using the same url as form would only cause drupal to return that form instead and not my data. So, I creates a static variable and stores all the retrieved data in it;this is done inside submit function by the way. When I checked this variable inside menu callback, this variable is not set.
To summarize my problem, form has different access url than form submit, such as
Form url – http://....?q=mymodule/form
Submit url (value of ”#action”) – http://....?q=mymodule/execute
, and the data I’ve set inside submit function to static variable is not available in menu callback. How do I make the data available?
Here’s part of my code -
static $retrieved_data;
function mymodule_menu() {
$command = array();
$command['mymodule/form'] = array(
'title' => 'user input',
'page callback' => 'response',
'page arguments' => array('form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$command['mymodule/execute'] = array(
'title' => 'Search',
'page callback' => 'response',
'page arguments' => array('execute'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $command;
}
function _response($paRequest){
switch($paRequest){
case "form":
return drupal_get_form("_myform");
break;
case "execute":
return $retrieved_data;
break;
}
}
function _myform(&$form_state) {
$form['#action'] = url($base_path)."?mymodule/execute";
.....
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('_data_retrieve'),
);
return $form;
}
function _data_retrieve($form, &$form_state){
/*data retrieval code*/
........................
$retrieved_data = db_fetch_object($result);
}
Thanks a bunch
Your method seems a bit complicated there. When I make systems with a form, I tend to do it this way. In your MYMODULE_menu() I would change the 'page arguments' => array('form'), to 'page arguments' => array('NAME_OF_FORM_FUNCTION'), where NAME_OF_FORM_FUNCTION would be _myform in this case. I would rename it to MYMODULE_MYFORMNAME.
then create a function:
MYMODULE_MYFORMNAME_SUBMIT($form, &$state) {
// Enter code here to save the data from the form that is stored in $state
// to the database with an SQL query or a node_save($node) if you are
// creating a node.
}
After that you can retrieve the data from the database in your _data_retrieve function and call that on the page where you want to retrieve the data.

Resources