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.
Related
How does one limit "what" can access a page. I have little understanding on how to use hook_permission() to set user permission based on a role to control access to hook_menu() items. However, what if I have a hook_menu() item that is to be used solely by the system itself? For example, say I have a registration page in a custom module: module/register - Anyone can access that page. Then, say I have an another page which is only for admin. So, I register another item in hook_menu(), call it module/register/reg_user_details - I do not want anyone to be able to browse to module/register/reg_user_details
Instead I want page to be visible only when user is logged in, I have already created a hook_permission
/**
* Implements hook_menu()
*
*/
function video_subtitles_menu() {
$items = array();
$items['video_subtitles/upload'] = array( //this creates a URL that will call this form at "video_subtitles_test/upload"
'title' => 'Upload Subtitle', //page title
'description' => 'Uploading subtitle for videos',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, drupal_get_form need to be used
'page arguments' => array('video_upload_subtitles_form'), //Name of the Uplaod Form
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles module'),
);
$items['player/video_subtitle_status'] = array(
'page callback' => 'video_subtitle_status',
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles status'),
);
return $items;
}
/**
* Implements hook_permission.
*/
function video_subtitles_permission() {
return array(
'administer video_subtitles module' => array(
'title' => t('Administer video_subtitles module'),
'description' => t('Access the video_subtitles upload module Page'),
),
'administer video_subtitles status' => array(
'title' => t('Administer video_subtitles module status'),
'description' => t('Access the video_subtitles module status Page'),
));
}
So User is not able to access my page unless user is logged in, but
what I need user should be redirected to the login page when he is not logged in as admin , if he successfully login it should be redirected to my page.
visit url d-7/example/my-module
if already logged in show the page
if not logged in redirect to the login page
if user logged in successfully redirect to d-7/example/my-module page
What is best way to achieve this
Similar Question
using-hook-menu-with-hook-permission-access-denied
using-hook-menu-and-hook-permission-to-control-access
can-someone-explain-access-arguments-in-drupal
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
I have a ACL plugin, and I want to be able to redirect back to users/index from this plug in.. but I end up getting funny url's that don't exist. such as /cakephp/admin/acl/users/index
how can I make it go to cakephp/users/index
I have looked through the HTML helper and I'm stumped.
I will be in the cakephp chat room as well
Thanks
You can 'reset' the plugin by setting it to 'false' when creating a link;
e.g.;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false
);
update
My guess is that you're using both prefix routing and a plugin. To reset both the plugin and the prefix, do this;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false, // resets the plugin
'admin' => false, // resets the admin-prefix
);
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.
When i enter address: http://www.yourdomain.com/2 (without page:2)
It give you Missing View: (error)
Missing View
Error: The view for PagesController::display() was not found.
Error: Confirm you have created the file: /Users/username/Sites/mycakeapp/views/pages/2.ctp
Notice: If you want to customize this error message, create /views/errors/missing_view.ctp
In router config: (routes.php in config)
$chk = array('page' => '[0-9]');
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/:page/*', array('controller' => 'pages', 'action' => 'display'), array(
'page' => $chk['page'], 'pass' => array('page')
));
In pages_controller.php:
function display($on_page=1) {
$this->paginate = array(
'limit' => $this->Cookie->read('pagelimit'),
'page' => $on_page,
'order' => array(
'data.dateadded' => 'asc'
));
$data = $this->paginate('data');
$this->set('data', $data);
$this->render(implode('/', $path));
$this->set('title_for_layout', null);
}
Try adding all named parameters to the routes config manually:
Router::connectNamed(array('page'[, ...]);
Like the error message states, you need to have a file called 2.ctp in your pages folder.
Confirm you have created the file:
/Users/username/Sites/mycakeapp/views/pages/2.ctp
The display method in the pages_controller is generally used to display static pages. A file named with the param you send, in your case 2, followed by '.ctp' must exist in the view/pages folder, this is what the error message tells you.
If you where expecting something else, you are not doing it right.
I found problem solved. the answer is:
add in controller page:
$this->render('/pages/home');
no need to add Router::connectNamed