This my first try with drupal, I need to write a module to collect terms visits, and list the terms based number of visits, its very simple, but the $node array give null in myModule.module !!

why ?

Thanks for help!

Comments

nevets’s picture

What is the content in your module where you try and print $node. There is not global $node, so it would have to be passed to the function or else loaded by the function.

marvix’s picture

trying to call in hook_node_type(), hook_nodeapi() ... what function can call it ?

nevets’s picture

What are you trying to do, hook_nodeapi() has $node as an argument.

marvix’s picture

what am trying to, after creating DB table:
1. get node terms
2. add +1 to db
3. function list terms based the visits.

That is all !

now, i`ll get the node id from function "arg()", and then load_node via id!

is that right, am sure its will work, but is this the best way ?

avpaderno’s picture

It's still not clear in which function you put the call to var_dump().
Without to know that, nobody can give you more help.

-- Kiam
Kiam la luno renkontas la sunon

marvix’s picture

hmmm... didn`t explain well ..
I need to list top 10 visited terms as menu.
for that I need to log the visits for the terms into the db as statics.. ok
the db table :
id int(11)
term varchar(255)
tid int(11)
voc varchar(255)
vid int(11)
visits int(11)

I need to get the visited term from the node..

I wrote those lines upto now :

function taxonomy_statics_get($type=false, $id=false){
	if (!$type)
		$node_type= arg(0);
	if(!$id)
		$node_id= arg(1);
	$node_Class=node_load($node_id);
	$node = (object)$node_Class;
	return $node;
}
function taxonomy_statics_init(){
	$node=taxonomy_statics_get();
	
}
avpaderno’s picture

First at all, taxonomy_statics_init() is called for any page requests, when the requested page has not been cached; you cannot know if the value returned from arg(1) is really a valid ID for a node.
Second, there is no need to use $node_Class; the value returned from node_load() is already an object, so you could simply write $node = node_load($node_id);.

The correct code would call menu_get_object() which also takes in consideration the different revisions of a node.

-- Kiam
Kiam la luno renkontas la sunon

nevets’s picture

You can use hook_nodeapi with $op equal to view to "capture" all nodes being viewed. This way $node is already set and the function is only called/run when needed to capture the data.

avpaderno’s picture

I agree. hook_nodeapi() is the better hook to use, even because it is also called right before a node is being viewed.

Now the answer to why is the code completely wrong? is complete.
-- Kiam
Kiam la luno renkontas la sunon

marvix’s picture

"Now the answer to why is the code completely wrong? is complete."

Why ^^

my line in Drupal ... switch from Ez Publish to dropal ;)
I will test hook_nodeapi() when i get home!

marvix’s picture

its works .. but too much, its repeating the node 4 times ^^

why?

marvix’s picture

ahha .. works fine now, THANKS guys!!!

marvix’s picture

yachhhhh ... still repeating

function taxonomy_statics_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
	echo $node->nid."<hr>";
}
nevets’s picture

Change the function to

function taxonomy_statics_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
if ( $op == 'view' ) {
echo $node->nid."<hr>";
}
}

or

function taxonomy_statics_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
echo $op . ': ' . $node->nid."<hr>";
}

if you see what the various ops are.

marvix’s picture

I see .. but this will slow down the query, as I understand hook_nodeapi will query all the nodes, then will view the selected or viewed node!
I think to node_load will be faster .. what you think !?

avpaderno’s picture

hook_nodeapi() will not query all the nodes.
It's a hook, a function that gets called by Drupal core modules when an event happen. In the specific, an implementation of hook_nodeapi() is called before Drupal executes specific operations on a node.

All Drupal modules execute code or when their page has been request, or when a Drupal even happens. In the last case, they react to an event that is specific for the task they do.
In your case, the best way is to implement hook_nodeapi(), and check when $op is 'view'. In your case hook_init() is not the better hook to use, as it gets called for every page request; being this the case, how would you know which node is being viewed?

-- Kiam
Kiam la luno renkontas la sunon

marvix’s picture

I see ... but can nodeapi filter node type ?
In the hook_init I can load the node and check the type if its "Article" type or no.

nodeapi will load all the nodes, and will compare the node id with the viewed on throw $op=view ... $op do as filter for the whole node.

avpaderno’s picture

hook_nodeapi() doesn't load nodes; it gets called when any node is subject of specific operations.
If you want to know the content type of the node, you can always check $node->type, as $node is passed to hook_nodeapi().

hook_init() is called for every page request; using it, you could not even know if a node is going to be viewed. What is the purpose to use a function that gets called at every page request when you want to know when a node is going to be viewed? To notice that a page request could be for a settings page, that would not load any node at all.

Anyway, the initial question was about var_dump($node) returning NULL, and that already got an answer.

-- Kiam
Kiam la luno renkontas la sunon

marvix’s picture

Thanks again guys ...

BTW, the manual missing the "Examples" its very helpful !

avpaderno’s picture

It's enough you check at the documentation, and you will learn everything about drupal functions; in the case of hook_nodeapi(), the documentation is here.

If you want to find the documentation of other functions, it's enough you write the name (also partial) in the Function, file, or topic field you find at any documentation page.

If you try to make a module just asking to the others how to use all the functions, it will take you a year, before to complete a module that is a little more complicate.

I suggest you to read the functions documentation, if you want to really create a module.
-- Kiam
Kiam la luno renkontas la sunon

nevets’s picture

I am guessing by "after creating DB table" you mean after insert the data in which case hook_nodeapi() would be the likely spot but I do understand what either "add +1 to db" or "function list terms based the visits" or means. It would probably help of you posted the code in question, just remember to but <code> and </code> tags around the code.