3

Is there a way I can convert all my imported nodes into "saved " status instead of "Draft" programmatically? Can this be done in a hook and what would the code for doing this be? If it can be done with Rules how exactly would this rule be created? This has to be something that is done without any user interaction, so Views Bulk operations is not the answer.

I am using workflow module, and it has two states, the first one is draft, the second one is "Done"

Thanks,

7
  • Despite the 2 upvotes this question has, I'm not sure what "draft" you mention exactly. If you use any custom modules, please mention them as well.
    – AKS
    Jul 20 '15 at 15:21
  • I am using "Workflow" (drupal.org/project/workflow) , and have two states, the first one is draft, the second one is "Done"
    – Diana
    Jul 20 '15 at 15:24
  • 2
    Do you definitely need to do it programatically? It sounds like you might just need a one-time effort, in which case you can install VBO and enable the workflow_vbo module
    – Clive
    Jul 20 '15 at 16:51
  • I do need to do it programmatically, because there will be uploads on going. and every time there is an upload they are coming in as draft state.
    – Diana
    Jul 20 '15 at 17:32
  • what version of Workflow module do you use?
    – Andrew
    Jul 22 '15 at 17:31
1
+100

You can use function:

// If have error you need uncomment line bellow 
//module_load_include('module', 'workflow', 'workflow');

// To get array nid you can use funtions
//$array_nid = workflow_get_workflow_node_by_sid(3);

function update_workflow_transition($nid) {
  // insert your node id
  // you can get array node id with db_select or function workflow_get_workflow_node_by_sid($sid)
  $entity = node_load($nid);
  $transition = new WorkflowTransition();
  $entity_type = 'node';
  $field_name = '';
  $old_sid = 2; // sid of your Draft status
  $new_sid = 3; // sid of your Saved status
  $uid = 1; // You can change with uid of author.
  $stamp = REQUEST_TIME;
  $comment = 'Your comment';
  $transition->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $uid, $stamp, $comment);
  workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force = TRUE);
}

You can call this function by hook_form_alter() when you use module feed to import node. Or you can create simple form and when submit form call this function.

Update: You can use hook_feed_after_save

function YOURMODULE_feeds_after_save(FeedsSource $source, $entity, $item, $entity_id) {
  $transition = new WorkflowTransition();
  $entity_type = 'node';
  $field_name = '';
  $old_sid = 2; // sid of your Draft status
  $new_sid = 3; // sid of your Saved status
  $uid = 1; // You can change with uid of author.
  $stamp = REQUEST_TIME;
  $comment = 'Your comment';
  $transition->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $uid, $stamp, $comment);
  workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force = TRUE);
}

Version test Feeds 7.x-2.0-beta1

7
  • thanks , but wouldnt I do this on hook_feeds_presave instead? it doesnt make sense to put it into the form before the feed has been imported. do you agree?
    – Diana
    Jul 23 '15 at 14:50
  • 1
    Yes, I agree. You can't use hook_feeds_presave but you can use hook_feeds_after_save.
    – MrD
    Jul 23 '15 at 14:58
  • See my update answer.
    – MrD
    Jul 23 '15 at 15:44
  • its not working its still saving as "Draft" , perhaps because its happening on create, the old_sid still did not exist . maybe we have to do this after it has first been created. so what hook could that be?
    – Diana
    Jul 23 '15 at 17:09
  • 1
    Ok. I hope my help save your time.
    – MrD
    Jul 24 '15 at 2:16
1

You can change Workflow status for all nodes using Views Bulk Operations module Here is a little manual how to accomplish this task:

  1. Download and install Views Bulk Operations module
  2. Create a view that will show content of necessary content type in table format.

enter image description here

  1. Add in this view next fields: Content: Title, Workflow history: New state name, Workflow history: Old state name and Bulk operations: Content.

enter image description here

  1. Open created view page, select all nodes, choose "Change workflow state of post to new state" operation and click on the "Execute" button

enter image description here

  1. Choose new node state and click on the "Next" button

enter image description here

  1. On the next page press on the "Confirm" button
1
  • 1
    That is a manual operation. I want this to be done behind the scenes, not manually. Thanks anyways.
    – Diana
    Jul 22 '15 at 19:27

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .