I need a function for a plugin to check, if the shown account is the user's account or that from another one. I am using this function for that:
global $user; $account;
$account = user_load(array('uid' => arg(1)));
if ( $user->uid == $account->uid ) {
}
I am doing that inside a module but it does not work. When I go to my profile, I never see the output from the function.
Why?
Edit
Original context of this code:
function tpzclassified_menu() {
global $user;
$account = user_load(array('uid' => 1));
$account = user_load(array('uid' => 1));
if ($user->uid == $account1->uid) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
}
I need a function for a plugin to check, if the shown account is the user's account or that from another one.
What the reported code is doing is to verify if the logged in user is the Drupal super user (aka the user #1). If that is what you really need, then there is no need to call user_load() to load the user object of that user account. It's enough you use the following code:
global $user;
if ($user->uid == 1) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
In Drupal, there aren't two users with the same user ID, and 1 is the user ID for the Drupal super user (which is also called user #1 for the fact its user ID is 1).
The problem with this solution is that since Drupal 6, the menu callbacks are cached; conditionally adding menu callback doesn't have any effect, as the menu cache is cleared only when a new module is installed, or when a module is updated (and update.php is called). The only way to force Drupal to clear the menu cache is to use the following code:
if (!variable_get('menu_rebuild_needed', FALSE)) {
variable_set('menu_rebuild_needed', TRUE);
}
If you want to check if the current logged in user is accessing his own account you can use the following code:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
There is no reason to use a custom function, as user_view_access() already checks if the current user is looking his own account.
function user_view_access($account) {
return $account && $account->uid &&
(
// Always let users view their own profile.
($GLOBALS['user']->uid == $account->uid) ||
// Administrators can view all accounts.
user_access('administer users') ||
// The user is not blocked and logged in at least once.
($account->access && $account->status && user_access('access user profiles'))
);
}
Edit
Based on your comment, the issue is where you're putting this code: hook_menu() is only called when the menu is rebuilt: that's why you're not seeing anything happening.
You can't conditionally add items to the menu system like that: you have to register a menu item, then use the menu item's access callback to determine if the menu will show up for a specific user. The access callback works just like the page callback, but must return TRUE or FALSE. You might do something like this instead:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'tpzclassified_user_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
function tpzclassified_user_access($account) {
global $user;
if ($user->uid == $account->uid) {
return user_view_access($account);
}
}
Related
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.'),
),
);
}
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'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.
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.
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;
}