Closed (fixed)
Project:
Comment driven
Version:
6.x-1.0-unstable9
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
13 Mar 2010 at 01:47 UTC
Updated:
19 Feb 2012 at 18:20 UTC
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
Comment #1
obrienmd commentedFrom 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());
Comment #2
arhak commentedthis 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
Comment #3
arhak commentedcommitted 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)
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
Comment #4
arhak commentednot yet comment_driven_save_with_ac
Comment #5
obrienmd commentedWould 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.
Comment #6
arhak commentedfeel 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
Comment #7
arhak commentedI 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
Comment #9
obrienmd commentedI'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.
Comment #10
arhak commentedyou'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)
Comment #11
arhak commented@#3 another example using CaseTracker Plus Features module
http://codekarate.com/blog/using-commentdrivensave-function-comment-driv...