I am using drupal 7.
I have a link which goes to href="/mod/filter/1"
X
and i have a a hook_menu
function mod_menu () {
$menu = array(
'mod/filter/%' => array (
"title" => "Bare HTML for use in ajax.",
"page callback" => "mod_remove_filter_function",
"page arguments" => array(1),
"type" => MENU_CALLBACK,
)
);
return $menu;
}
Then the callback function
function mod_remove_filter_function($arg){
dsm('call back filter');
drupal_goto('/res/search');
}
To me this should work, It is the first time i've used the menu hook but this looks like it should work according to the documentation given.
Any ideas why is doesn't work?
function mod_menu () {
$menu = array(
'mod/filter/%' => array (
"title" => "Bare HTML for use in ajax.",
"page callback" => "mod_remove_filter_function",
"page arguments" => array(1),
"type" => MENU_CALLBACK,
)
);
return $menu;
}
hook is workds perfect. problem might be in the callback function
dsm function requires devel module and if you are using the drupal_goto('/res/search'); check for the "/res/search" path first .
:)
here is how I use hook_menu in custom modules.
$menu['mod/filter/%'] = array(
'title'=>t('look this is title'),
'page callback' => 'mod_remove_filter_function',
'access callback' => 'user_access',
'access arguments' => array('access_contents'),
'type' => MENU_NORMAL_ITEM,
);
Do not use t() function in your menu item. By defualt drupal will pass the title string into the function t(). You can change that behavior by setting a new 'title callback' in your menu item array
See hook_menu book from drupal.org
Related
I'm new to Drupal making a plugin that hooks into the ckeditor widget. I absolutely can't figure out why my implementation of a hook that is defined in ckeditor is never called.
Here are some details
my module is enabled
I'm able to use more basic hooks like exceltohtml_plugin instead of exceltohtml_ckeditor_plugin and reach my test statement.
I'm can't think of any more troubleshooting ideas to reveal the issue so any help would be greatly appreciated.
exceltohtml.module
<?php
error_log("TEST: this will print to log");
// implementation of hook_ckeditor_plugin()
function exceltohtml_ckeditor_plugin()
{
error_log("TEST: but this will never run");
return array(
'exceltohtml' => array(
'name' => 'exceltohtml',
'desc' => t('Excel sheet upload'),
'path' => drupal_get_path('module', 'exceltohtml') .'/plugins/exceltohtml',
'buttons' => array(
'excel_to_html' => array('label' => 'Insert spoiler','icon' => '/images/image.gif' ),
)
)
);
}
ckeditor.api.php (the file in ckeditor that Im basing my hook on)
/**
* Hook to register the CKEditor plugin
*/
function hook_ckeditor_plugin() {
return array(
'plugin_name' => array(
// Name of the plugin used to write it.
'name' => 'plugin_name',
// Description of the plugin - it would be displayed in the plugins management section of profile settings.
'desc' => t('Plugin description'),
// The full path to the CKEditor plugins directory, with the trailing slash.
'path' => drupal_get_path('module', 'my_module') . '/plugin_dir/',
'buttons' => array(
'button_name' => array(
'icon' => 'path to button icon',
'label' => 'Button Label',
)
)
)
);
}
If the function in your module is new, then the slightly older version of your module code might be cached.
Visit the module listing page in Drupal, that should reload the modules PHP code:
admin/modules
Drupal 7 Hook_menu access callback is not returning the correct boolean.
Before we begin. YES! Cache is cleared... a lot.
I implemented a simple function for testing:
$items['tutor_review_selection'] = array(
'title' => t('example'),
'page callback' => 'my_module_example_page',
'access callback' => my_module_access( array('administrator') ),
'type' => MENU_NORMAL_ITEM
);
function my_module_access( $roles ) {
global $user;
$check = array_intersect($roles, array_values($user->roles));
return empty( $check ) ? FALSE : TRUE;
}
This returns TRUE for logged in and logged out users.
Here is the important part:
I call 'my_module_access' function in 'my_module_example_page' function and it works correctly.
Can anyone shine some light onto why this would not work in access callback?
Maybe something to do with order of operation?
Cache is cleared.
If you check the Drupal 7 hook_menu documentation you will see the following code:
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
);
return $items;
}
'page callback' accepts a string, which is the callback function name. The arguments to be sent to that function are provided in the 'page arguments' array.
edit Note that you should probably create a permission and assign your roles to that permission, then check the permission instead of checking for specific roles.
I'm developing my module. For module I created special node type and add some nodes with aliases as 'events/my1', 'events/my2' and 'events/my3'.
In module I use hook_menu function
$items['events'] = array(
'title' => t('Events list'),
'access callback' => TRUE,
'page callback' => '_events_list',
'type' => MENU_CALLBACK,
);
$items['events/%'] = array(
'title' => t(''),
'access callback' => TRUE,
'page callback' => '_event_detail',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
);
On url site.com/events/ opened my page from _events_list() function
On url site.com/events/anyurl/ opened content from _event_detail() function
But when I open site.com/events/my1/ then opened default view for node. Not my code from _event_detail().
How I can fix it? I want for each urls from /events/ show code from my function, not default view.
hook_menu defines new URL paths. For existing URL paths, use hook_menu_alter.
In drupal 7 I'm trying to give permissions of using a node if you are member of the same group as the node.
I want to use hook_menu and define my custom access check function. To this function I sent the nid as a parameter.
This is what I have got now, and I realy don't see why it is not working:
function modulename_pdf_menu() {
$items['pdf/node/%'] = array(
'page callback' => '_modulename_pdf',
'access callback' => '_modulename_pdf_access_check',
'access arguments' => array(2),
'type' => MENU_CALLBACK
);
return $items;
}
function _modulename_pdf_access_check($nid) {
echo $nid;
die();
}
I assume this should print my node id to the screen and stop. But it is still running the logic defined in _modulename_pdf. Any idea what I'm missing here?
Thanks in advance for your reply.
You are right, clear the cache and check it.
In /drupal/admin/structure/menu/manage/main-menu, I have some links that I only want displayed for user's that have a specific permission.
How would I go about this?
In my module, I have
...
$items['resume/joblist'] = array(
'page callback' => 'ac_resume_job_list',
'access arguments' => array('view joblist'),
'title' => 'Job List',
'description' => 'Job List',
);
...
function ac_resume_permission()
{
return array("view joblist" => array("title" => "View Job List"));
}
When I go to "resume/joblist" under a user without the permission, I get the "Access Denied" as expected, however the link is still displayed.
hook_perm() was renamed to hook_permission() in Drupal 7, and there's a bit of a mismatch between view mylink that you define and view joblist that you declare as a permission.
You could change your code to look more like this:
function mymodule_menu() {
$items['mylink'] = array(
'page callback' => 'mymodule_mylink',
'access arguments' => array('view mylink'),
'title' => 'My Link',
'description' => 'My Link',
);
return $items;
}
function mymodule_permission() {
return array(
'view mylink' => array(
'Title' => 'View My Link'
)
);
}
After you clear Drupal's cache navigate to admin/people/permissions and assign your new permission to the role you want to be able to access the page you define in hook_menu().
Once you've done that users with that role will be able to access the page :)
I recommend using the following module: Menu Item Visibility, it does exactly what you need.