Injecting Code On Node Update

GreenLED - July 19, 2008 - 17:07

What code can I use to detect whether a node has been updated? I'd like to use this logic . . .

<?PHP

IF ($node has been updated)
{ do something }
ELSE { so something else }

?>

I cannot find out how to make this logic work.
How can I implement this. PLEASE give sample code!
I appreciate your help very much.

P.S. I want this code to run in node view... in other words it's going to be run when the node is viewed -- so keep that in mind.

Any ideas on this? »

GreenLED - July 19, 2008 - 17:33

Any ideas on this?

» Respectfully, GreenLED
» Stable Files . net

I don't have any sample code

WorldFallz - July 19, 2008 - 17:54

I don't have any sample code for you, but i can point you in the right direction. You want to use hook_nodeapi to grab the node at the point you wish to alter.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

Hmm. We'll you've pointed me

GreenLED - July 19, 2008 - 18:04

Hmm. We'll you've pointed me in the right direction, but I'm still not "catching any fish" as of yet. I still need something more. I don't quite understand how these hooks work and I have a little bit of time schedule. However, if I could get just an idea of how this hook works and whether or not to put it into my template.php file that would really help me a whole lot.

» Respectfully, GreenLED
» Download Spybot

Depends

yelvington - July 19, 2008 - 18:15

Depends on your definition of "updated."

$node->created is the creation date; $node->changed is the alteration date. Both are Unix timestamps. If they match, the item has never been edited.

If you're looking for "updated since I last logged in," I believe $user->login contains a Unix timestamp that should be useful.

So, something like this (untested):

<?php
 
global $user;
  if(
$node->changed > $user->login){
    if (
$node->created == $node->changed) print "This item is new since your last visit";
    else if (
$node -> created > $user->login) print "This item is old, but has been changed since your last visit";
    else print
"This item is new AND has been updated since your last visit";
  }
?>

You don't have to do this in a module; you can do it in the templating system.

Hmm. I appreciate the shot,

GreenLED - July 19, 2008 - 19:10

Hmm. I appreciate the shot, but not exactly what I'm looking for. Here's a better explanation. What I'm trying to do is run php code either right after a node is actually submitted (I'm the only one submitting updates right now) or right after the page says "This (content-type) has been updated." So for example, "IF" i'm submitting an edit to a node -- THEN and ONLY then will I do some php code. I tried editing the node.module and adding some code in, but it only runs that code and then haults. I KNOW -- bad thing to edit node.module, but it was the only alternative I could think of. See I have some code that will compress a file and also do some custom cropping. That's just beside the point though. I want to be able to run code on a node after it's been submitted so I have the "$node" array available to me. So, the bottom line is this -- to be able to inject some code (no matter what it is) and have it run right after a node has been "updated" that is to say actually submitted updates via the node/edit form. I ONLY want to run these if I am submitting -- NOT just viewing a node. I hope that clears things up a bit.

» Respectfully, GreenLED
» Download Spybot

Yep, definitely sounds like

WorldFallz - July 19, 2008 - 19:59

Yep, definitely sounds like you want hook_nodeapi. If you're going to be doing some customization of drupal beyond installing and configuring modules, you'll want to read up on it some-- it will greatly increase your options for customization.

for your current need, i would create a little custom module (i create a "custom.module" module for every site for just such little code snippets), and do something like this:

<?php
function custom_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch(
$op) {
    case
'insert':
     
//put code here for handling the node when it's inserted into the db
   
break;

    case
'update':
     
//put code here for handling the node when it's updated
   
break;
  }
}
?>

To see the available operations, see the nodeapi page i linked above. You can add cases for as many operations as you wish to handle.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

I tried injecting "ECHO

GreenLED - July 19, 2008 - 20:08

I tried injecting "ECHO 'Yes';" in the "update" case just to see if it was operational. Nothing is showing up as of yet. I ran this from my template.tpl.php, would that work from there or does it not event get validated from there? I only have one site currently that I'm running off that install and this is something I want for this site. Any ideas?

» Respectfully, GreenLED
» Download Spybot

Try using something like

nevets - July 19, 2008 - 20:35

Try using something like drupal_set_message("Op is $op"); instead of the echo.

Are you only allowed to use

GreenLED - July 19, 2008 - 20:43

Are you only allowed to use druapl functions in this function? Can I run just raw php code like ECHO or PRINT? Sorry if I seem not in tuned to this api. Here's some code I want to run . . .

{  // Create: Cropped Screenshot
   $w = 330; $h = 260; $x = 0; $y = 0;
   $source = '/home/handy7/public_html/stablefiles/' . $image['filepath'];
   $output = "/home/handy7/public_html/stablefiles/files/shots/screen/crop." . $image['filename'];
   $screen  = imagecreatefromjpeg($source);
   $crop   = imagecreatetruecolor($w,$h);
   imagecopy ( $crop, $screen, 0, 0, $x, $y, $w, $h );
   $run = imagejpeg ( $crop, $output );

It sounds like you can't just inject code like this, am I right?

» Respectfully, GreenLED
» Download Spybot

AFAIK, you get the best of

WorldFallz - July 19, 2008 - 20:48

AFAIK, you get the best of both worlds-- you can use any of the drupal api functions (as well as other module's functions you have enabled) as well as custom php.

if you're just trying to manipulate images, you might want to check out http://drupal.org/project/imagecache which has a whole host of other benefits (like views integration).
===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

What about that custom.php?

GreenLED - July 19, 2008 - 21:01

What about that custom.php? I really don't want to bother with a whole nother module as far as image cropping goes for a whole host of reasons. It would take me to long to explain why not, but basically in a nutshell, I have a variety fo things I want to do that require really a custom approach. Anyhow, can I use the code I printed above in this module or do I have to use custom.php -- how do you do this? Do you just create custom.php and use some drupal function to include it or something?

» Respectfully, GreenLED
» Download Spybot

Setting

GreenLED - July 19, 2008 - 20:47

Setting "drupal_set_message()" still does nothing when the node is updated.

» Respectfully, GreenLED
» Download Spybot

i'm pretty sure you have to

WorldFallz - July 19, 2008 - 20:44

i'm pretty sure you have to use it in a module and not template.php. To make it a module, you just need to create a folder under your sites/all/modules directory, put the function in custom.module (or whatever you want to call it), and add an info file:

1. create a file named custom.info (or whatever you named your module)
2. add the following:

; my custom module
name = Custom
description = "Module for my custom code"
package = Custom

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

Thanks a million! (more

GreenLED - July 19, 2008 - 22:36

Thanks a million! (more thanks later, gotta fix up other stuff).

» Respectfully, GreenLED
» Download Spybot

 
 

Drupal is a registered trademark of Dries Buytaert.