I have a site with the following relationship: for every user, there is one corresponding (node) profile. For the user/*/edit, I would like the user to actually edit their profile node, not the user account. I've thought about preprocessing the page template to make something available in order to then re-write the secondary menu in the template - but which variable can I use? It's not available in the $user array.
-
you are playing with fire here in a lot of ways, but what version of drupal are you using?– JimajammaCommented Dec 27, 2013 at 18:00
-
you have query the database to get the back reference, otherwise you can also keep the user node id on user entity also which will be udpated when a node is referred to a uid, that way you can have it on $user also.– arpitrCommented Dec 27, 2013 at 18:09
-
@Jimajamma, I'm using D7.– SteveCommented Dec 27, 2013 at 18:19
-
@arpitr - How would I keep the the user nid on the user entity? I'm not sure I follow.– SteveCommented Dec 27, 2013 at 18:19
-
1I was asking to make sure it wasn't already done for you. If you can pretty much guarantee that there is only one such profile node per user, I can map out the broad strokes here for you– JimajammaCommented Dec 27, 2013 at 19:02
3 Answers
So in broad strokes, basically what you want to do is grab the user's uid and then open for editing the corresponding profile type node owned by that user.
So, if you have that user's uid in $uid
, it would be something along these lines:
$nid = db_select('node', 'n')
->condition('n.type', 'YOUR_PROFILE_NODE_TYPE')
->condition('n.uid', $uid)
->fields('n', array('nid'))
->execute()->fetchField();
which, if I've not typo'ed anything will set $nid
to the node id of the node of type YOUR_PROFILE_NODE_TYPE that is owned by $uid
.
If there is only one, that's the node we are looking for.
So, now you just need to create a link to node/$nid/edit
and you're all set. (Somewhat obviously, you want to present this link only to admins and the user who owns the profile.)
So, where do you put all of this?
Of course, you could preprocess this into a variable that's used in a page template, but you could also create a new menu item as well, eg user/#/edit-profile
that grabs the user id out of the second argument and then does all of the above to grab the actual nid and then presents the node edit form, but now we are getting past the broad strokes :) (But hook_menu() is the place to look, with user.module
a great starting point for example code for user/#/edit
to modify)
-
This looks great, but I'm not getting anything usable if I put it in themename _preprocess. Here's what I'm doing
$uid = $variables['user']->uid; $nid = db_select('node', 'n') ->condition('n.type', 'fellow_profile') ->condition('n.uid', $uid) ->fields('n', array('nid')) ->execute()->fetchField(); $variables['profile_nid'][] = $nid;
– SteveCommented Jan 2, 2014 at 19:22 -
I doubt there is a
user
variable being sent in viapreprocess()
, but it is available in the second argument. Once again, broad strokes here, it would be1
if you were looking atuser/1/edit-profile
or just plain olduser/1
and can be found viaarg(1)
(no relation to user 1 and 1 here, arg(#) just returns the #th argument in the path). Commented Jan 2, 2014 at 20:45 -
You're right, there isn't, but I can user_load() and have it - but, that won't give me the reference to the profile node. My solution is to use an entity reference field on each (user and profile), which gives me just what I need. Thanks for all your brainstorming!– SteveCommented Jan 3, 2014 at 0:53
Consider making a new solution based on Profile2, otherwise spice you current solution up with the Rules module. :)
In this case, I added an entity reference field to both the the user and profile node. In the page template, I can user_load() the uid of the logged-in user and have the entity reference available for theming.