I've read through most of http://drupaldocs.com and the forums, trying to find an answer for this. I'm hoping I'm just missing and it's something simple.

Wrote a custom node module, widget.module. Users can create a "widget", no problem. Inside widget.module I'm trying to write a block hook that will show when that users page is displayed. I want to show the widgets for that particular user.

Question, within:

<?php
  function widget_block($op = 'list', $delta = 0) {
    global $user;
    ...
  }
?>

I want to reference the user being displayed, i.e. user/666, so I can show their widgets. When I reference global $user I get the currently logged in user, as I would expect. How do I get access to uid = 666?

-andrew

Comments

ahwayakchih’s picture

If so, then that should work:

$user->uid

so for example:

if ($user->uid == 666) {
  print 'Are You really The Beast?';
}

:).

logictude’s picture

If I'm looking at http://domain.com/user/666 I want to get the uid, in this case 666, from within the custom block I've written. When I reference $user->uid I get the uid for the person logged in, which isn't the uid I want.

Whenever I load http://domain.com/user/uid I want that uid to show up in my custom block.

I'm sorry I'm not making a whole lot of sense.
-andrew

nevets’s picture

First assuming your module is called widget normally the path would be something like http://domain.com/widget/666 or http://domain.com/widget/user/666 (i.e. the path normally would include the module name to avoid clashes with other modules.

So in the code that processes the URL I would set a global to reflect the uid requested. Then in the block code refer to that global (I think this would work since I think blocks are processed after the main content, if not you code reference the current url path and strip the uid off)

Note: a draw back to the global, unless you have the block resticted to show only for the proper path other pages will show the block with the last uid referenced.

nevets’s picture

Not sure what you mean by "How do I get access to uid = 666?"

If you are looking for the list of nodes owned by the user you need do an sql query with a where clause that includes node.uid = 666 (or as code 'node = ' . $user->uid) Depending on the query this could be different.

The full query will depend on what you want to achieve.

As an example you might do something like (Note: this is untested code)
This all goes in the else case for the block

/* Get the title and node id for nodes of type "widget" that belong to the current user */
$sql = "SELECT n.title, n.nid FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE n.type = '%s' AND n.uid = %d";

$result = db_query($sql, "widget", $user->uid);
	
if (db_num_rows($result) == 0 )
{
  /* you could either inform the user they have no widgets
      or produce no ouput so block does not show 
    */
}	
else
{
  while ( $node =  db_fetch_object($result) )
  {
    /* do something with node */
    
    $output .= l(t($node->title), 'node/' . $node->nid);
  }

  $block['subject'] = t('block title');
  $block['content'] = $output;
}
logictude’s picture

nevets: This is the logic I've written, we're on the same page. Here's the problem.

$user->uid returns the uid of the currently logged-in person. I want to run the query using the uid of the user whose page i'm viewing... user/666. I don't know how to get that uid though.

(I'm uid = 1. When I look at user/666 the block shows my "widgets" when I want it to show 666's "widgets".)

ahwayakchih’s picture

Something like nevets said here: http://drupal.org/node/18388#comment-30785

In Drupal You can get original URL query using:

$uri = request_uri();

so something like this should work (assuming You use clean URIs):

preg_match("/\/user\/([0-9]*).*$/", request_uri(), $matches);
print 'user ID is:'.$matches[1];