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.
Related
I am new to Drupal development. I want to create a registration form for visitors of my site. Default registration page only has two fields: Username and Email address.
How can I add more fields to it, e.g password, picture and timezone. I also want to capture tow other information - Date of Birth and Gender. These two fields are not available in default users table. How can I tie these information with a user account? Do I have to create a new table and put these information there referencing the uid of user table? If it is possible how can I pull the joined record?
Is there any possibility that I create a new content type for this purpose but records still go to default users table and can be used for login?
If 2 above is not possible I probably have to use hook_form_alter but where should I put this function?
When creating a custom registration form shall I use default registration page i.e. /user/register and customize it?
I am sorry if above questions look very childish and silly! Hope you will consider my my newbie status. If possible please help me with a step by step solution.
Thanks!
UPDATE
To accomplish the requirement I created a custom module called user_signup and in user_signup.module file I have written the following code.
<?php
/*
Implements hook_menu()
*/
function user_signup_menu(){
$items = array();
$items['user/signup'] = array(
'title' => 'Sign Up',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_signup_registration_page'),
'access arguments' => array('access content'),
);
return $items;
}
function user_signup_registration_page($form, &$form_state){
$form['name'] = array(
'#title' => 'Username',
'#description' => 'choose a username',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['mail'] = array(
'#title' => 'Email',
'#description' => 'enter a valid email address',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['pass'] = array(
'#title' => 'Password',
'#description' => 'Enter a strong password',
'#type' => 'password',
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create Account'),
);
return $form;
}
function user_signup_registration_page_submit($form, &$form_state){
$new_user_id = db_insert('users')
->$fields(array( **//this is line number 45**
'name' => $form_state['values']['name'],
'mail' => $form_state['values']['mail'],
'pass' => $form_state['values']['pass'],
))
->execute();
drupal_set_message(t('New user created'));
}
Everything works perfectly but when I hit the submit button I am getting this error:
Fatal error: Method name must be a string in D:\xampp\htdocs\imdbind\sites\all\modules\user-signup\user_signup.module on line 45
I have marked line number 45 in above code snippet as **//this is line number 45**. I did not find any difference when comparing my code with theirs. What I am doing wrong?
Just change line 45 with following
From:
->$fields(array( **//this is line number 45**
To
->fields(array( **//this is line number 45**
You can add Fields here /admin/config/people/accounts/fields int eh People Account settings Manage Fields form.
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.
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 have created a custom module in drupal with entities. I have installed the entity api module. I have created my database schema with just two columns (employee_id, first_name) through the help of employee_management.install file (where as employee_management is my custom module name) and employee is my entity name.
I have also written the requisite functions employee_management.module but still it shows me the error , Whenever i tried to add a new entity in the admin/structure/employee it shows me the following error: "Not Found".
The requested URL drupal/employee/add/ was not found on this server.
function employee_management_entity_info() {
$employee_info['employee'] = array(
// A human readable label to identify our entity.
'label' => t('Employee Entity'),
// The controller for our Entity - extends the Drupal core controller.
'controller class' => 'EmployeeController',
// The table defined in hook_schema()
'base table' => 'employee',
// Returns the uri elements of an entity
'uri callback' => 'employee',
// Fieldable that we can attach fields to it - the core functionality will
// do the heavy lifting here.
'fieldable' => TRUE,
// The unique key of our base table.
'entity keys' => array(
'id' => 'employee_id',
),
// FALSE disables caching - caching functionality is handled by Drupal core
'static cache' => TRUE,
// Attach bundles - i.e. alternative configurations of fields associated with a main entity.
'bundles' => array(
'employee' => array(
'label' => 'Employee',
// Information below is used by the Field UI - they "attach" themselves here and lets us
// do the standard field management that all the core entities enjoy.
'admin' => array(
'path' => 'admin/structure/employee/add',
'access arguments' => array('administer employee entities'),
),
),
),
// View modes allow entities to be displayed differently based on context. We simply have one option
// here but an alternative would be to have a Full and Teaser mode akin to node.
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
)
);
return $employee_info;
}
EDIT
function employee_uri($employee) {
return array(
'path' => 'employee/' . $employee->employee_id,
);
}
And here is the complete list of function in the file employee_management.module
You don't automagically get the route and form to create your entity, you'll have to implement that yourself. See hook_menu and this guide.
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.