Exists a function to get node info (object) passing the nid to a parameter?

Comments

marcuscavalcanti’s picture

A function like (this function not exists, its an assumption)

node_get_info($nid);

and this function returns:

Array
(
    [0] => stdClass Object
        (
            [nid] => 903
            [counter_forum] => 6666
            [tid] => 14
            [title] => Índice Agressivo (em edição)
            [sticky] => 1
            [name] => allan
            [uid] => 13
            [timestamp] => 1172258102
            [comment_mode] => 0
            [last_comment_timestamp] => 1172258102
            [last_comment_name] => allan
            [last_comment_uid] => 13
            [num_comments] => 0
            [new_replies] => 0
            [new] => 
        )

    [1] => stdClass Object
        (
            [nid] => 1090
            [counter_forum] => 3
            [tid] => 14
            [title] => 5456456
            [sticky] => 0
            [name] => admin
            [uid] => 1
            [timestamp] => 1182201969
            [comment_mode] => 2
            [last_comment_timestamp] => 1182201969
            [last_comment_name] => admin
            [last_comment_uid] => 1
            [num_comments] => 0
            [new_replies] => 0
            [new] => 
        )
)

Like a forum_get_topics() make in $topics array.

AjK’s picture

$node = node_load(array('nid' => $nid));

http://api.drupal.org/api/5/function/node_load

That what you are after?

nevets’s picture

If you have a nid it is better to call the function like this

$node = node_load($nid);

which if the node has already been loaded will use a cached copy.

marcuscavalcanti’s picture

Ok, its works .. but i need an array like array returned by forum_get_topics() in var $topics.

One array like example below:

Array
(
    [0] => stdClass Object
        (
            [nid] => 903
            [counter_forum] => 6666
            [tid] => 14
            [title] => Índice Agressivo (em edição)
            [sticky] => 1
            [name] => allan
            [uid] => 13
            [timestamp] => 1172258102
            [comment_mode] => 0
            [last_comment_timestamp] => 1172258102
            [last_comment_name] => allan
            [last_comment_uid] => 13
            [num_comments] => 0
            [new_replies] => 0
            [new] => 
        )

    [1] => stdClass Object
        (
            [nid] => 1090
            [counter_forum] => 3
            [tid] => 14
            [title] => 5456456
            [sticky] => 0
            [name] => admin
            [uid] => 1
            [timestamp] => 1182201969
            [comment_mode] => 2
            [last_comment_timestamp] => 1182201969
            [last_comment_name] => admin
            [last_comment_uid] => 1
            [num_comments] => 0
            [new_replies] => 0
            [new] => 
        )
)

Exists function to make this?

nevets’s picture

and node_load() returns an object for a single node/nid. So if you only have one node/nid you can do

$set = array();
$set[] = node_load($nid);

and that would give you an array with one element which is a node object. If you have more than one nid, you could loop through the set of nid's doing a node_load() for each need, adding the return to an array (like the example above).

marcuscavalcanti’s picture

Hi nevets, thanks for your answer.

But in the truth i dont want this .. what i want is get node information like forum_get_topics() (form.module) make.

if it was possible to pass as parameter from forum_get_topics() a nid array it would be more easily.

what I want I am accurately one equal Array to the returned one for forum_get_topics (), I am making a custom search in the forum, and in this search i have the results in array of nid and i need display this result in a page of my forum. You understand?

nevets’s picture

One thing that is confusing is the forum_get_topics does not simply return an array of nodes, it returns information about forum topics.
The following function is based on forum_get_topics but takes an array of nids instead of a tid (note this is untested)

function your_forum_get_topics($nids, $sortby, $forum_per_page) {
  global $user, $forum_topic_list_header;

  $forum_topic_list_header = array(
    array('data' => ' ', 'field' => NULL),
    array('data' => t('Topic'), 'field' => 'n.title'),
    array('data' => t('Replies'), 'field' => 'l.comment_count'),
    array('data' => t('Created'), 'field' => 'n.created'),
    array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
  );

  $order = _forum_get_topic_order($sortby);
  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
    if ($forum_topic_list_header[$i]['field'] == $order['field']) {
      $forum_topic_list_header[$i]['sort'] = $order['sort'];
    }
  }

	if ( is_array($nids) ) {
		$nid_str = implode(',', $nids);
	}
	else if ( empty($nids) ) {
		return array();
	}
	else {
		$nid_str = $nids;
	}
	
  $term = taxonomy_get_term($tid);

  $sql = db_rewrite_sql("SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node_comment_statistics} l, {users} cu, {users} u, {forum} f, {node} n WHERE n.status = 1 AND l.last_comment_uid = cu.uid AND n.nid = l.nid AND n.nid IN (%s) AND n.uid = u.uid AND n.vid = f.vid");
  $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
  $sql .= ', n.created DESC';  // Always add a secondary sort order so that the news forum topics are on top.

  $sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1 AND n.type = 'forum' AND n.nid IN (%s)");

  $result = pager_query($sql, $forum_per_page, 0, $sql_count, $nid_str);

  while ($topic = db_fetch_object($result)) {
    if ($user->uid) {
      $history = _forum_user_last_visit($topic->nid);
      $topic->new_replies = comment_num_new($topic->nid, $history);
      $topic->new = $topic->new_replies || ($topic->timestamp > $history);
    }
    else {
      // Do not track "new replies" status for topics if the user is anonymous.
      $topic->new_replies = 0;
      $topic->new = 0;
    }

    if ($topic->num_comments > 0) {
      $last_reply = new stdClass();
      $last_reply->timestamp = $topic->last_comment_timestamp;
      $last_reply->name = $topic->last_comment_name;
      $last_reply->uid = $topic->last_comment_uid;
      $topic->last_reply = $last_reply;
    }
    $topics[] = $topic;
  }

  return $topics;
}