Drupal 7 - menu user access for logged in users? - drupal

i'm building membership website, and i want to build pages only for users that logged in.
What i'm writing in the access callback to give access only to logged in users?
'access callback' => '?'
Thank you.

You can use the user_is_logged_in() function to check if a user is logged in. Like this:
$items['custmomenu'] => array(
'title' => 'yourtitle',
'page callback' => 'yourcallback function',
'access callback' => 'user_is_logged_in',
);

You need to use the following access callback as shown below:
$items['mypage'] => array(
'title' => 'My Page',
'page callback' => 'mypage_callback',
'access callback' = > 'user_is_logged_in',
);
More info about checking if a user is logged in:
http://oliverdavies.co.uk/blog/2013/01/09/checking-if-user-logged-drupal-right-way

Related

Drupal - Access to Path for anonymous users

I have created a path with a hook_menu function which is intended to be accessed anonymous users. Definition is :
##generic access point for API calls #
items['api'] = array(
'title' => 'api access point',
'description' => 'Pass all api calls thru single access point to simplify code',
'page callback' => 'xxxxxxx_utility_api',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK, );
The path www.mysite.com/api/aaa/bbb/...
returns JSON
and functions as long as I am logged in, but I need to allow anonymous access to the path
How can I configure drupal to allow anonymous access to a path that returns JSON
your need to have 2 levels and to delete access arguments and next clear cache menu (drush cc menu)
items['api/custom'] = array(
'title' => 'api access point',
'description' => 'Pass all api calls thru single access point to simplify code',
'page callback' => 'xxxxxxx_utility_api',
// 'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK, );

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.

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.

Drupal permissions are not being applied

Why are my permissions not being applied?
$items['admin/mymodule'] = array(
'page callback' => 'mymodule_admin',
'access arguments' => array("admin mymodule"),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'file' => 'mymodule.admin.inc',
'title' => 'mymodule',
);
function mymodule_perm(){
return array("admin mymodule", "earnings_report");
}
When I go to Mysite/admin/mymodule, I am able to access it WITHOUT being logged in.
The permissions on admin/user/permissions are correctly set to only give access to "site developer" and "store administrator", and the anonymous user are not part of those roles.
I tried going to /admin/content/node-settings/rebuild and /admin/build/modules, but it didn't help.
The permission "earnings_report" is working as expected, but "admin mymodule" is not.
Thanks!
The 'access callback' => TRUE, line defines who can access admin/mymodule
You've set it to always be TRUE which means it can always be accessed. I think you need to change it to something like:
'access callback' => 'user_access',
'access arguments' => array('admin mymodule'),

Add a tab to Drupal node

I want to add a tab to Drupal node like in the following picture:
The picture has 3 tabs, Views, CVS Instructions, Revisions. I want to add another tab "Translation". What module should I use?
The picture was taken from http://drupal.org/project/panels_tabs
Thank you.
I would create a simple small module that has a hook_menu implementing the tab.
See the example here:
http://drupal.org/node/678984
As for the rest of your implementation, I don't know what you are trying to achieve, but this will add tabs.
Not sure if this is relevant but if you are actually looking to translate node content then have you investigated the Internationalization module?
The translation tab is handled by the module "Content translation" that depends from "Locale"; once you enabled the module, you need also to set which content types can be translated, and other settings that change the way a node of that content type is translated.
Not quite what was asked, but here is code for hook_menu in a custom module that sets up an administration menu option with 2 tabs.
/***************************************************************
* hook menu
*/
function acme_viewer_setup_menu(){
$items = array();
// administration setting - call from URL
$items['admin/settings/acme_viewer_setup'] = array(
'title' => 'Acme Misc Setup - viewer and Blog', // title in Admin menu
'description' => 'Acme Misc Setup: acme viewer & Blog',
'page callback' => 'drupal_get_form', // Retrieves form 'acme_viewer_setup_admin'
'page arguments' => array('acme_viewer_setup_admin'),
'access arguments' => array('access administration pages'), // only users who can access admin pages
'type' => MENU_NORMAL_ITEM,
);
// tab 1 - viewer
$items['admin/settings/acme_viewer_setup/viewer'] = array(
'title' => 'Configure viewer', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('acme_viewer_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
// tab 2 - blog
$items['admin/settings/acme_viewer_setup/blog'] = array(
'title' => 'Configure Blog', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('blog_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}

Resources