How to set permission in custom drupal module in Drupal7 - drupal

I have created a custom Drupal7 form module , but I want to show the form to the authenticated users only . I want to create the module likewise that it could have check-box options in the people->permission section from where I could set the permission for this module for all type of users .Here is the menu_hook
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('access administration pages'),
'access callback' => TRUE
);
return $items;
}
I am new in drupal so any help regarding this would be appreciable.If someone could write down the hook_permission rather than giving examples then it would be a great help.

Here is the implementation of hook_permission
/**
* Implements hook_permission().
*/
function form_example_permission() {
return array(
'administer your module' => array(
'title' => t('Administer permission for your module'),
'description' => t('Some description that would appear on the permission page..'),
),
);
}
And you have to give the key of the returned array (administer your module) to access arguments in the implementation of hook_menu
So, your hook_menu implementation would become:
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
Note that you'll have to flush the cache after you change anything in hook_menu. You can do it from admin/config/development/performace/

Try this. After adding this clear your cache and go to the people->permission and from there you could set permission for users .
function form_example_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}

Related

Drupal 7 theming : hook_theme or drupal_get_form

I'm wondering which is the best way to display a form on a page (for example : a register form for vip users ... and not in a block but as the main content).
The user.module way in user_menu (*hook_menu*) :
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'drupal_get_form',
'page arguments' => array('vip_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
Or by creating a theme via use_theme (*hook_theme*) (fictive) :
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'theme',
'page arguments' => array('vip_register'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
function user_theme() {
return array(
'vip_register' => array(
)
);
}
function theme_vip_register(){
return drupal_get_form('vip_register_form');
}
I'm wondering about this for theming purpose, because a designer will do the graphic integration afterwards.
Thank you for advices.
This is not an actual answer but I'm not quite sure what is your question at first place.
Drupal customs #1: Never hack core!
As its name, theme functions are just to theme something. So you need the form built first.
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'drupal_get_form',
'page arguments' => array('vip_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
When a user accesses example.com/vip/register page, drupal_get_form function will be called with argument vip_register_form.
So now you need to define a function to return this (vip user registration) form.
function vip_register_form($form, &$form_state){
..your FAPI stuff here.
return $form;
}
Now user who open the vip register page will see this form instead of the regular form. even password and username fields will not be available unless you add them. If you want to alter the existing form, just copy the menu hook to a new path:
$user_menu_routers = user_menu();
$items['vip/register'] = $user_menu_routers['user/register'];
Now you can alter your the form at vip/register page (which is same as normal user register page) using a form_alter hook. You can theme the form manually without affecting existing one as well.

user->uid alway is 1 for whatever user?

I'm new for Drupal , so I want to create a simple module that custom user profile, specically I want insert some tabs for user profile (the traditional user profile have 2 tabs "View tab" and "Edit Tab") so now I want add more 3 tabs.
Show History
Share profile
Shortcuts
but it only apply for user authenticated not admin profile (when admin show his profile ,the profile is traditional profile).
So it my code (my module's name is :tung_tab) and here is code of tung_tab.module:
function tung_tab_menu()
{
$items = array();
global $user;
//if (!user_access('administer'))
if($user->uid!=1)
{
$items['user/%/rvhistory'] = array(
'title' => 'Review History ',
'description' => 'Review History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_reviewh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sharehistory'] = array(
'title' => 'Share History',
'description' => 'Share History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sc'] = array(
'title' => 'Shortcut',
'description' => 'Shortcut',
//'page callback' => 'getDescription',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
It work properlly for admin tab, but when I log out and then login with other user
but not thing is changed, so if I comment the line //if($user->uid), everthing is ok
so when I add more code in this line
'title' => 'Review History '.$user->uid,
When I'm admin It show that Review History 1
When I loged in with another account , everything is the same , Review History 1
I can't understand why? I think my code get some mistake
I put my module in "mysite/sites/default/modules/"
You shouldn't define the menu items inside the if condition. You should control the access to the menu callbacks using access callback and access arguments instead.
WRONG:
function tung_tab_menu()
{
$items = array();
global $user;
if($user->uid!=1)
{
// defined menu items
}
return $items;
}
CORRECT:
function tung_tab_menu()
{
$items = array();
global $user;
// define the menu items here...
return $items;
}
Try to fix that then see if you still get the same results.

drupal_get_form page callback usage

function example_menu() {
$items['admin/config/example'] = array(
'title' => 'Example',
'description' => 'example configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('example_admin_settings'),
'access arguments' => array('administer example'),
'file' => 'example.admin.inc',
'file path' => drupal_get_path('module', 'example'),
);
return $items;
}
In the above code I am confused how it works. The page callback is drupal_get_form and the page arguments is example_admin_settings. My question is how exactly does this work?
I know that drupal_get_form probably ends up calling example_admin_settings which return system_settings_form. Could someone point me to the right docs?
You're on the right path.
When you access admin/config/example, drupal_get_form('example_admin_settings') is executed. example_admin_settings() itself will return an array which has its contents based on the form api, something like:
$form['name'] = array(
'#type' => 'input',
'#title' => 'Insert your name here: ',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
Drupal will render and output the form automatically for you.
Also, consider posting your next questions about Drupal at https://drupal.stackexchange.com/.
this is example is like making something like this
function example_menu() {
$items['admin/config/example'] = array(
'title' => 'Example',
'description' => 'example configuration',
'page callback' => '_my_page_content_function',
'access arguments' => array('administer example'),
'file' => 'example.admin.inc',
'file path' => drupal_get_path('module', 'example'),
);
return $items;
}
function _my_page_content_function(){
return drupal_get_form('example_admin_settings') ;
}
function example_admin_settings($form, $form_state) {
... return my form
}
in your example you asking drupal to create page and the content of this page is the content that returned by the function drupal_get_form when we pass the argument example_admin_settings to it.
SUMMARY:
page callback is the name of the function that return the content of our page when we pass the argument page aruguments to
sorry for my english
mmmm need more details ?

I am creating a page using hook_menu() but I get "You are not authorized to access this page."

I am trying to create a very simple page in my module using hook_menu(), but after I test it I get, "You are not authorized to access this page". I can't figure out what I am doing wrong. Following is the code that I used.
Note that I created this module under the existing module package. For instance the module folder is xyz and I created a folder as xyz_mobile for the module, and I added xyz in the info as the package. I don't know if that would have anything to do with it.
function xyz_mobile_menu() {
$items['mobile'] = array(
'title' => 'page test',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
I'm assuming Drupal 6 here. You need the 'access arguments' and 'page callback' elements in your $items array:
function mymodule_menu() {
$items = array();
$items['mobile'] = array(
'title' => 'page test',
'page callback' => 'mymodule_my_function',
'access callback' => 'user_access',
'access arguments' => array('access content'), // or another permission
'type' => MENU_CALLBACK,
);
return $items;
}
The 'access callback' element contains the name of the function (in this case, user_access) that will check if the user has the permission specified in the 'access arguments' element.
The 'page callback' element will run your custom function.
function mymodule_my_function() {
return 'this is the test page';
}
Lastly, don't forget to clear the menu cache before you re-test.

Acces/See settings page for a custom odule

I upgraded a module from 5 to 6. I only have one problem:
I can access the settings page for it, but can not see the contents of it.
This is my code:
function agbnagscreen_menu() {
global $user;
$items = array();
if (agbnagscreen_nag($user)) {
// var_dump($_GET['q']); die();
drupal_goto(sprintf('%s/%s', AGBNAGSCREEN_NAGURL, base64_encode($_GET['q'])));
die();
}
$items['admin/settings/agbnagscreen'] = array(
// 'path' => 'admin/settings/agbnagscreen',
'title' => 'AGB nagscreen',
'access callback' => user_access('Einstellungen von AGB aendern'),
//'access' => user_access('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
// 'path' => AGBNAGSCREEN_NAGURL,
'title' => 'Allgemeine Geschaeftsbedingungen',
'access' => TRUE,
'callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
I think the problem is cause by this line:
'page callback' => 'drupal_get_form',
Is that correct? How can I write it, that it works?
You might want to read through the Drupal menu system (Drupal 6.x) handbook page to understand the changes to the menu system: you have several problems in your hook_menu implementation.
The conditional at the top will never fire: Drupal 6 only calls hook_menu() when the menu is rebuilt, not on every page load.
There is no callback: use page callback.
The page callback accepts page arguments, not callback arguments.
There is access: use access callback.
access callback always a string containing the function name, not a function, and defaults to "user_access": you need to supply access arguments.
A modified version of your hook_menu implementation might be:
function agbnagscreen_menu() {
$items = array();
$items['admin/settings/agbnagscreen'] = array(
'title' => 'AGB nagscreen',
'access arguments' => array('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
'title' => 'Allgemeine Geschaeftsbedingungen',
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}

Resources