I'm using Drupal 6.16: I think I have a pretty simple question. How can I get the current user id and put it in a menu tab. What I would like to happen is when the user logs in and wants to change their name, email etc to click a menu tab. I image it would look something like this: http://domain.com/user/{userid}/edit
Thanks in advance!
msindle
That's more difficult than you would think, because menu items are cached. There is not a straightforward way to create dynamic menu items with the userID in it.
What you can do, is write a custom module and imitate the behavior of the 'user' path. With an implementation of hook_menu you create a menu item, with the path 'user/edit' (just like user_menu() creates $items['user']). Next you create a menu callback user_edit_page(), similar to user_page(), which gets the id of the current user and returns the user edit page:
function user_edit_page() {
global $user;
if ($user->uid) {
menu_set_active_item('user/'. $user->uid .'/edit');
return menu_execute_active_handler();
}
else {
return drupal_get_form('user_login');
}
}
Related
On my website, The user can add the content and edit the content. On the edit page, there is one delete button to delete the content but I want to use that button to just hide/unpublish the content from the user and the public.
I have tried the below code but it is deleting the content.
function test_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
$nid = $entity->id();
$node =Node::load($nid);
$node->setPublished(FALSE);
$node->save();
}
You can try to overwrite action button using hook_form_alter().
$form['actions']['delete']
Instead standard action use your own.
Firstly, do not allow users to delete nodes - by the proper
permissions settings.
Secondly add Publish/Unpublish button or just
show users Publish checkbox on edit form and instruct them how to
use it and how it works.
Maybe combination of modules can be helpful:
https://www.drupal.org/project/publishcontent
https://www.drupal.org/project/view_unpublished
https://www.drupal.org/project/override_node_options
I want to display different navigation links and data to a user depending upon their role.
What module(s) could I use to achieve this?
Use hook_menu_alter()
/*
* Implementation of hook_menu_alter()
*/
function MYMODULE_menu_alter(&$items)
{
$items['your/path']['access callback'] = _custom_access_callback_for_this_page();
}
And inside the access callback function (here: _custom_access_callback_for_this_page), write whatever the validation you want.
Don't forget to clear the cache after any changes inside your `hook_menu_alter() implementation.
Hope this helps.
I really like this module for Drupal 6, you should check it out:
Menu per Role
I have two questions, both related to the same view: so there is view called ProductDetails which shows the details of a product.
Each product can have the status:
Available - in this case, two button are available "edit" and "remove"(which will change the status of the product to "Not available" but will not remove it from DB)
Not available - in this case, the page displays the product but no options to edit or remove are
visible.
The controller ProductsController has an action Details that shows that view.
The problem is that I don't know how to implement the two buttons (Edit and Remove) because:
Edit sends to another action method (Edit which display another view) <- this works
Remove should do (IMO) a post on the current page. In the post action, the status of the product is changed and the view is shown again.
I want both button to look like links. If I put a form for remove, then it will be displayed as a button. I would like to avoid making the button look like a link with css. Or... at least I want to use the same HTML element for both 'buttons'.
This is more an issue of displaying the elements so I have added the CSS tag to your question as some alternative answers may rely on this.
Personally I think trying to make a button look like a text link would be quite awkward, even once you turn off the border and background you have issues with lining up the text etc.
I'd say you have 2 "simple" options.
Firstly you could make the delete not post a delete request but link to a delete confirmation page (or bring up a JS modal window with your delete form and button).
Secondly you could make them both look like buttons, while you requested that it looks like a link I figured that the main point was consistency in UI than the link look specifically. You could use JQueryUI and invoke .button() on both elements, invoking JQueryUI for 1 feature is a bit overkill but it's a quick change, of course you could replicate the same idea of styling the link like the buttons but would have to spend time dealing with browser CSS issues.
the Remove link should post to the Remove action, which should in turn (after validation and DB update) redirect to the details action.
public ActionResult Details(int productId)
{
// Your current action method
return View(model);
}
public ActionResult Remove(int productId)
{
// Validate productId
// Update DB
return RedirectToAction("Details", new { productId = productId } );
}
You easily can solve your link vs button problem by using a GET instead of a POST. Don't be blinded by best practices.
Or you can use a Remove link that executes a one-liner Javascript function that posts the form:
Remove
I need to change the menu links on my website (and leave the same items names) depending on the user is a guest, or authenticated user.
What's the standard way to do it ?
thanks
You cannot dynamically change a menu item's path, because menu items are cached.
Still, AFAIK, there are two ways to get what you want. Both methods require you to create your menu items with hook_menu in a custom module (not from the Menu UI).
The first method is to create two menu items with identical names and set the access rules so that one is only available for logged guests, the other for authenticated users. Since Drupal will only show menu items that the user is allowed to access, only one will show up at any given moment. In Drupal core, you can see how the user module creates a menu item for anonymous users by looking at the /user/login path in user_menu().
The second method is to create a single menu item and check in the menu callback if the user is logged in. If the user is logged in, you serve one page, if not you serve another. In Drupal core, the /user path works like this. See user_page to see how the code works.
You can dynamically change a menu item's path - see hook_translated_menu_link_alter.
This hook is called before every menu item is rendered IF it has the property ['options']['alter'] = TRUE.
You can set this property to menu items using hook_menu_link_alter.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['link_path'] .= 'my-new-path';
}
}
Instead of altering the link, you coudl create the menues twice: once with the links for regular users and once with the links for registered/admin/... users
You can put a menu into a block and set it to only allow registered users to see the one block and non-registered users the other block. Either by selecting the proper radio button from the Drupal menu within the block creation form or via PHP that will evaluate and depending on it's return value (TRUE/FALSE) displays it. I suggest to go with the first approach.
You can change the menu by using a combination of nodeaccess module and linking to the corresponding pages.
For example, by default guest users cannot access /logout. If you create a link in a menu to logout, it will only display if a user is logged in. With nodeaccess, simply create a node, visit the grant tab and uncheck/check "authenticated users" or "anonymous users" for that node.
http://drupal.org/project/nodeaccess
Cheers,
when an user login in Drupal, the login block disappear from my pages.
I would like instead to keep it there with a message stating the user is logged in.
Something like this (with logout link): "Welcome Patrick. Logout"
thanks
The login-form will not work for not logged in-users.
What is easiest, is to make a new block (admin » build » blocks » new(tab))
Then set that block to show only for role "authenticated". That way, the block will work exact opposite from the user-login block, which shows only for role: anonymous.
You can add any custom text in there, as long as it is static text. If you want to include dynamic text, such as "Hello $username, you will have to go for the tip by Jeremy French: create a module with its own hook_block.
The login block is created by the follwing code from the user_block() function.
if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
$block['subject'] = t('User login');
$block['content'] = drupal_get_form('user_login_block');
}
There is no code to display a message if you are logged in.
One simple way to do what you want would be to create a module which implements the above code in hook_block, and has an elese statement which displays what you want.
A quick way to solve your problem, is to create a new block with whatever you want in it, and only show it when the user is logged in. If you arrange the placement and style, it will look like the same block, even though they are different.
You can create the new block with a custom module or views.