Blue
Blue

Reputation: 1

How can I send an email to a certain role when a specific cck field is edited (changed value)

As the title explains my question. I want to send an email to the users having a certain role whenever a node is edited and the value of a specif field is changed. Are there any modules to help me ?

Since I am new to drupal module development I tried to write my own module but how can I get the old field value before editing so that I can compare and see whether the field is changed or not ?

I also want to provide a simple method to change that field value without going to the edit page. Since I change that field value a lot and a lot of other unexperienced users do. I was going to use flag module to implement that feature but this field has three states not two. So i can't use flag module.

Thanks in advance

Upvotes: 0

Views: 834

Answers (2)

g_thom
g_thom

Reputation: 2810

A non-custom-module way would be to use the Rules module, which allows you to send an email to a users of a certain role when a CCK value is changed. It's written to do the kind of task you describe (and pretty much any other kind of task based on an event trigger).

If you're looking for a solution to a specific problem, Rules should do exactly what you need. If this is a personal project and you want to dig into the code, by all means, writing a custom module would help you understand how Drupal works.

Upvotes: 0

DefiniteIntegral
DefiniteIntegral

Reputation: 579

What version of Drupal?

In Drupal 6, you can attach to changes on nodes using hook_nodeapi and use the optional params of node_load to compare fields in the old node and the new node.

i.e.

<?php
/**
 * Implementation of hook_nodeapi().
 */
// fires mymodule_send_email() when the field 'specialfield' on nodes
// of type 'specialnodetype' is updated
function mymodule_nodeapi($node, $op, $a3, $a4) {
  if ($node->type == 'specialnodetype' && $op == 'update') {

    // $node stores the "old node" and $new_node stores the "new node"
    // we're about to save
    $new_node = node_load($node->nid,null,true);

    // if a particular field doesn't match, fire a particular action
    if ($node->field_specialfield[0]['value'] !=
        $new_node->field_specialfield[0]['value']) {
      mymodule_send_email();
    }

  }
}
?>

You can use a similar implementation of hook_nodeapi to update the field automatically and without having to manually re-invoke node_save.

Note that stuff stored in a CCK field isn't always retrieved as 'value', since CCK fields come in many flavors and can have multiple parts. You can always look at the parts using print_r, which will always be retrieved/assigned like this:

<?php
// Displays an associative array with keys and values of the first instance
// of the CCK field 'fieldname'
print_r($node->field_fieldname[0]);

// Displays an associative array with keys and values of the third instance
// of the CCK field 'fieldname'
print_r($node->field_fieldname[2]);
?>

(The second example is for a field that takes multiple values in a node.)

Feel free to reply with more details if needed.

Upvotes: 1

Related Questions