Hi,
I would like to share an action we are going to implement.
"Create a node of a specific content type"
and maybe discuss if we got it all covered.
This Action, IMO, should have two variables:
1. Show list off all node types that can be created (required) - only one can be selected. Of course this will not override the access control (i.e. user has no privilege to create this content).
2. Show Text box with new node title (Optional). Basically admin will be able to assign a title with the Token, however I think this field can be optional, since the Automatic node titles module can be used.
Fago - any tips on how to implement - as the developer that is going to do it is still a newbie ;)
Comments
Comment #1
fagoawesome!
I would require a title, if autonodetitle is used, it takes it over anyway.
Are you going to support any other fields too? Tokens are useful, but I can only for textfields. Other fields are difficult..
tipps? hm, perhaps make use of drupal_execute() to create the node, this makes sure that it looks to drupal like a usual form submission. E.g. the usernode module does also so.
Comment #2
mightyiam commentedMy community will be thankfull for this!
Comment #3
amitaibuwell, we are working on launching our site, so this feature will take some more time... If anyone else wants to grab this task...
Comment #4
amitaibuHi Fago,
well...hmmm... I've tried to do it myself. Please remember I'm a fashion designer not a developer, but hey I'm trying :)
I still didn't get out how to create patches, so please forgive me this time that I submit it as a separate module.
One problem I couldn't solve was that my title wasn't replaced by Token.
I guess you need to review it... Waiting to hear your comments, so next time it will be better :D
Cheers,
Amitai
Comment #5
amitaibuOk, I think I understand now how to create patches...
Note:
1. I've only added the action info in workflow_ng_user.inc, and not in workflow_ng_actions.inc. Not sure if it is ok, since I saw duplication in several cases (Add user role for example).
2. The Title is still not properly replaced by Token, please help me with this.
That's it, please send me comments so I can improve :D
Comment #6
fagoah, fine. :) Make sure to always create the patch against the latest code.
-> workflow_ng_actions.inc isn't there any more. The code has been moved over in the include files in the modules directory. So I think this code fits as addition to the node module's workflow-ng integration, so add it to it's include file and set the module to Node.
+function workflow_ng_action_add_node ($settings, &$arguments, &$log) {
the first argument of your action is the author's user object: ($author, $settings, ..)
@token: I recently updated http://drupal.org/node/156754. Have a look at it, it contains some descriptions to the recently improved workflow-ng token API. This should help you.
Comment #7
amitaibu1. Moved to node.inc
2. Fixed function to get $author, thus fixed also the Token replace.
Comment #8
fagoPlease use of the latest workflow-ng token API - as I suggested above.
Then your patch contains also other changes, which doesn't belong to this issue. Be sure to always generate patches per issue.
Why are you ising str_replace() on the node type? Is this really necessary? Then you shouldn't use user_access there, but use node_access(). Then be sure to check for the right user - so I think we should check it for the node's author or should we even check it? Perhaps it would be better to provide the access check as condition "User may create node" or integrated in the action "only create the node, if the author has access to so".?
Furthermore, I think you have some tabs in your patch. As to the coding styles use two spaces for indention instead.
Comment #9
amitaibuFago, I really appreciate your help on my first steps, hope it doesn't make you too crazy to see my newbie mistakes :P
1. fixed to use node_access.
2. Added a checkbox "Create the node only if the author has access rights to do so".
3. Replaced to new Token API, However I'm having problems with the
function workflow_ng_token_replace_all, can you tell me what I'm doing wrong there?4. I've changed my editor tabs to 2 spaces, I hope it applies also on this patch (not sure how to check).
Comment #10
fagonp. I'm doing the best to overlook nothing .. ;) I know all this minor things can stretch the nerves...
so first, code style:
+function workflow_ng_action_add_node ($author, $settings, &$arguments, &$log) {
->
+function workflow_ng_action_add_node($author, $settings, &$arguments, &$log) {
+ $form['node_access'] = array(
+ '#type' => 'checkbox',
-> Too much indention in the first line.
+ else $authorize = TRUE;
->
always use {}
->
else {
$authorize = TRUE;
}
Some times shortening code can make it even more readable:
+function workflow_ng_action_add_node ($author, $settings, &$arguments, &$log) {
+ //check if WF-ng should check for access rights on node creation
+ if ($settings['node_access']) {
+ if (node_access('create', $settings['nodetypes'])) {
+ $authorize = TRUE;
+ }
+ else {
+ $authorize = FALSE;
+ }
+ }
+ else $authorize = TRUE;
+ if ($authorize) {
+ $node = array('type' => $settings['nodetypes']);
+ $node_form = $settings['nodetypes'].'_node_form';
+ $title_from_token = workflow_ng_token_replace_all (array('title'), $settings, $arguments, $log);
+ $values['title'] = $title_from_token['title'];
+ $values['name'] = $author->name;
+ drupal_execute($node_form, $values, $node);
+ }
->
(untested)
+function workflow_ng_action_add_node ($author, $settings, &$arguments, &$log) {
+ //check if WF-ng should check for access rights on node creation
+ if (!$settings['node_access'] || node_access('create', $settings['nodetypes'])) {
+ $node = array('type' => $settings['nodetypes']);
+ $node_form = $settings['nodetypes'].'_node_form';
+ $title_from_token = workflow_ng_token_replace_all (array('title'), $settings, $arguments, $log);
+ $values['title'] = $title_from_token['title'];
+ $values['name'] = $author->name;
+ drupal_execute($node_form, $values, $node);
+ }
$node_form isn't a very suitable variable name, as it's no form, but a form_id
node_access('create', $settings['nodetypes']) checks the node access for the currently logged in user. Looking at node_access() it's not possible to check it for the author, so I suggest changing the checkbox message.
I'm not sure if the above code works for non-admins - non admins can't change the author so I think the author can't be set that way. Try setting the ->name (and perhaps ->uid) in $node too.
+function workflow_ng_action_add_node_submit($form_id, $form_values) {
+ //returns the needed settings
+ $used_args = workflow_ng_token_get_settings(array($form_values['title']), $form_values);
+ return array('nodetypes' => $form_values['nodetypes'], 'title' => $form_values['title'], 'node_access' => $form_values['node_access'], 'used arguments' => $used_args);
+}
workflow_ng_token_get_settings(array($form_values['title']), returns already $settings containg the title and the title args. use it that way
+function workflow_ng_action_add_node_submit($form_id, $form_values) {
+ //returns the needed settings
+ $settings = array('nodetypes' => $form_values['nodetypes'], 'node_access' => $form_values['node_access']);
+ return $settings + workflow_ng_token_get_settings(array($form_values['title']), $form_values);
+}
Comment #11
amitaibu1. Maybe we should go back to user_access, to check the author?
2. Shortening the code, I know this part I wrote is a bit ugly, but I think
if (!$settings['node_access'] || node_access('create', $settings['nodetypes']))isn't correct because if $settings['node_access'] is TRUE and node_access is FALSE it will still be executed.3. In the drupal_execute I already pass the $node->name, so I think this part is covered.
I'll create another patch.
Comment #12
amitaibuAttached patch.
1. Using user_access so content author OR acting user can be checked.
2. I kept the $authorize 'ugly' check, until a nicer way will come to mind.
3. Regarding the Indent lines, I've learned it from the drupal book (snap1) - isn't it the correct way?
Comment #13
fago1. user_access isn't appropriate in this case, as node access modules like content access and others wouldn't apply. So we can't use user_access here.
2. no, it won't. FALSE || FALSE == FALSE :)
3. ah well. Please give it also a test as non admin just to be sure.
the indentions in snap1 are fine, but if you look at your patch in the line
+ $form['node_access'] = array(
the indentions aren't like that.
Comment #14
amitaibu1. The checkbox says "Create the node only if the acting user (i.e. logged in user) has access rights to do so"
So If this is FALSE ans node_access is false the action still needs to happen.
that's why "FALSE || FALSE == FALSE" isn't good in this case.
2. In D6 node_access is changed - node_access($op, $node, $account = NULL), so it will be improved there (added comment in module)
3. Seems to work ok for non-admins.
Comment #15
fagogreat.
You still have some tabs in your code, e.g. in the line
+ $settings = array('nodetypes' => $form_values['nodetypes'], 'node_access' => $form_values['node_access'], 'title' =>
You can try to remove it by using Search & Replace in your text editor. (tab -> \t)
@if
->+ if (!$settings['node_access'] || node_access('create', $settings['nodetypes'])) {
It's not node access -> !$settings['node_access']
if access should be checked, the if would be FALSE || $access == $access
Comment #16
amitaibuOk, hopefully that's it :)
Comment #17
fagosry, for the late review.
I found some spelling issues:
* It's "acting" not accting.
* Again please make sure to don't use capital letters in the middle of the sentence, e.g. User, Title, ..
* Don't mention auto nodetitle, as this has nothing to do with workflow-ng itself.
Apart from that minor issues, the patch looks already good :)
Comment #18
amitaibuFixed comments.
Comment #19
fago+ '#description' => t('Enable this in order for the system to check if the user has access rights for creating the
+ required node. Note that even if the user arguments are set to \'content authort \' the system will still check the \'acting user\'' ),
this description doesn't apply to your patch.
Comment #20
amitaibuBut we are doing
if (!$settings['node_access'] || node_access('create', $settings['nodetypes']))i.e. checking on the acting user in any case.btw, I'll fix the typo there - 'author' not 'authort')
Comment #21
fagoah, sry now I understand. :) waiting for the type fix..
Comment #22
amitaibufixed.
Comment #23
amitaibuRTBC?
Comment #24
fagoindeed! However, I wasn't able to apply the patch? Could you reroll it?
Best always create a diff for the whole workflow-ng module directory.
Comment #25
amitaibuRerolled and checked on my system, so hopefully it's ok.
Comment #26
fagoI completely overlooked that you have used 'node' in the UI - I replaced this with 'content' as this is standard in workflow-ng.
Then I've changed the code to create the node by using node_save() instead of drupal_execute. It seems to work well, however I don't know if there are cases where this fails - I'm not sure with drupal_execute either. Anyway, time will show, if drupal_execute works better we can change this later on.
The cause, why I've changed this to node_save() is that I hope that I can integrate this with a new feature I've planned: allow actions to expose further arguments.
-> I've commited this to the 2.x branch.
Comment #27
amitaibuHooray! my first code commit :)
Fago, thanks so much for your patience!
Comment #28
fagoyou are welcome :)
Comment #29
summit commentedSubscribing, looks interesting! Will it be possible also do a term and node adding together?
greetings,
Martijn
Comment #30
fagoif there would be a "Add a term" action - yes. So feel free to provide a taxonomy integration.
Comment #31
summit commentedUnfortunately I am not a programmer, but may be somebody would like to add it?
Greetings,
Martijn
Comment #32
(not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #33
asak commentedHi there!
I've just downloaded the 2.x-dev version and trying to use the node creation action. I'm new to this module so it's possible i'm simply missing something here...
1. Created a new rule
2. Set it to trigger on "User has registered"
3. Click "Add an action" -> "Add new content"
Now here's the problem: All i get is "Label:" and "Arguments configurations" with "User, which is set as author of the node:" in it.
How do i set the content type to be created?
How do i set it's title (ideally that would be the user name of the user who registered)?
Who's the Author when set as "Acting user"? (ideally (but probably too much to ask for) that would be the user who invited the newly registered user to join)
What i'm trying to do here is to create a node which is an OG group node for every user who's invited to the site (intranet). I'm assuming workflow-ng is the most reasonable way to do this... and found this thread to be the most appropriate.
Thanks!
Comment #34
fagohm, looks like you got also hit by: http://drupal.org/node/226046
please post more about this there.