When I execute an arbitrary comment_save in php (say, using a rule) - can I add variables to the array passed to comment_save to modify cck fields using comment_driven? For instance, in the example:

$edit['subject'] = "I'd like to change a field!";
$edit['comment'] = "Specifically, would like to change field X to value Y....";
$edit['uid'] = 1;
$edit['nid'] = 23;
comment_save($edit);

Could I add variables to the $edit array to take advantage of comment driven?

Comments

obrienmd’s picture

From arhak (I, obrienmd, need to test this):

to get an approximated idea of what would be provided by comment_driven
try the following code into "Execute PHP"
taking into account your config
in this case I tested with a content type with machine name "cck_uid"
having a CCK userreference (non-multiple) named "field_manager"

Note: I like your use case, since it exposes access control issues
the code bellow isn't supposed to be able to change the node title unless being a driven property, but it does succeed
I haven't checked whether if it is a comment_driven issue or a drupal_execute one, but in my testing site the comment subject has no access and it's being changed as well and it is not touched by comment_driven in any way

// what would go into $edit for a comment_form
$comment['subject'] = 'the subject';
$comment['comment'] = 'the comment body';
$comment['uid'] = 1; // an existing uid
$comment['nid'] = 12; // an existing node of the addressed type

// this is done by a special function, but currently won't support this (hacked) use case
$comment_driven = array();
foreach ($comment as $key => $value) {
$comment_driven[COMMENT_DRIVEN__DISGUISE_PREFIX . $key] = $comment[$key];
}

// what would go into $edit for a node_form
$node['title'] = 'the node title';
$node['field_manager']['uid']['uid'] = 4; // a valid CCK userreference field

// the $op = t('Save') is required otherwise the form won't be saved
$form_state['values'] = $comment_driven + $node + array('op' => t('Save'));
drupal_execute('comment_form', $form_state, $comment);

// if !empty(form_get_errors()) then it wasn't saved
var_export(form_get_errors());

arhak’s picture

Status: Active » Needs work

this should go into the next commit

BTW, drupal_execute doesn't take care of re-validating access control
FAPI is responsible for guarding access control from posted data (avoiding it to reach $form_state['values'])
but drupal_execute is receiving a manually provided $form_state['value'] therefore, access control should be honored by the caller function (if desired)

comment_driven_save will provide an automatic way to guard for this, to avoid misusage leading to access control violations

arhak’s picture

Status: Needs work » Fixed

committed to HEAD

Remember that faked $comment/$node variables should match $form_state['values'] format
which is easy for $comment, but not so easy for $node (note how complex a CCK field might be structured)

// comment properties
$comment['subject'] = 'the subject';
$comment['comment'] = 'the comment body';
$comment['uid'] = 1; // the author of the comment
$comment['nid'] = 12; // the node to which this comment belongs

// node properties
$node['title'] = 'the node title';
$node['field_manager']['uid']['uid'] = 3; // a valid CCK userreference field

if (comment_driven_save($node, $comment, $errors)) {
  print 'Saved OK';
}
else {
  var_export($errors);
}

pending the ACP version of this function: comment_driven_save_with_ac
which will take care of proposal at #2

PS: ACP stands for Access Control Policies

arhak’s picture

Version: 6.x-1.x-dev » 6.x-1.0-unstable9

not yet comment_driven_save_with_ac

obrienmd’s picture

Would it be possible to add this to documentation or a FAQ - How to use comment_driven programmatically? If you do not have the time or resources, I will attempt to do this as soon as possible, which might not be for a week or two.

arhak’s picture

feel free to open a "Documentation" task
right now I won't start writing documentation beyond what is in commented code plus FAQ issues
http://drupal.org/project/issues/comment_driven?text=FAQ&component=Docum...

if you wan't to provide your own FAQs, step by step, or whatsoever, then I'll review them

arhak’s picture

I found some troubled when trying to use this functionality, and it turned out that I was using userreference with a different widget, which implies that the sample code didn't worked properly (i.e. the node wasn't updating)
to check whether the faked $node is right built or not, it can be submitted to its TYPE_node_form using the same drupal_execute and check if the intended fields are properly updating
that way it avoids lots of misreporting to comment_driven_save when the issue is actually in the provided data

EDIT: typos

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

obrienmd’s picture

Status: Closed (fixed) » Active

I'm not sure I understand this well... But when I have been using this, date fields seem to act strangely, shifting between 0010-01-01 and 2010-01-01 every time I use comment driven programmatically, even when they are not the targets of the programmatic comment driven changes.

arhak’s picture

Status: Active » Closed (fixed)

you're right describing the symptoms, but it is not due to the "programmatically" use of comment_driven
#792198: unmodified dates get shifted (having time zone)

arhak’s picture

@#3 another example using CaseTracker Plus Features module
http://codekarate.com/blog/using-commentdrivensave-function-comment-driv...