2

I'm using the Feeds (7.x-2.0-alpha9) and the Workflow modules. My imported nodes are automatically going to draft state and only go to done state when there is a second import of this node and the data is updated.

How can I automatically make the state on imported nodes be "saved" not "draft"?

I tried adding a rule upon feeds presave, but it never gets to the save since it is a draft.

2
  • Just a suspicion here, but does the node exist before the first import? It sounds like you are having this situation: The node is being created by the feed; the rule is firing on pre-save, so before there is actually a nid on the node; the workflow module is firing on hook_node_insert so it runs after the rule and initiates the workflow; initial flow state is draft. I suggest devising some flag or method for identifying an imported node after the feed has ran, and changing your rule trigger to an event that occurs after workflow initiation. If all your imports have one author, perhaps? Commented Jul 20, 2015 at 19:41
  • yes these are new nodes. which events occur after workflow initiation?
    – Diana
    Commented Jul 20, 2015 at 21:16

2 Answers 2

0

Here is the answer, thanks to another stackexchanger

function mymodule_feeds_after_save(FeedsSource $source, $entity, $item, $entity_id) {
    global $user;
    foreach ($entity->workflow_transitions as $key => $value) {
      $field_name = $key;
      $workflow = $value;
      break; // There should only be one workflow at this point
    }
   //$this_sid = $workflow->new_sid;
  $transition = new WorkflowTransition();
  $entity_type = 'node';

  $old_sid = 2; // sid of Draft status
  $new_sid = 3; // sid of Saved status
  $uid = $user->uid; // You can change with uid of author.
  $stamp = REQUEST_TIME;
  $comment = 'Created by upload';
  $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);
}
0
+50

Even though this question is related to the Workflow module, the solution you're looking for should come from the Feeds and/or Rules module, as further explained in my answer below.

1st attempt: Feeds Rules module

The Feeds Rules module might be what you are looking for. Excerpt from its project page:

... provides some Rules integration for the Feeds module.

It provides:

  • Feeds processor: a processor for Feeds to create content via Rules. (Currently only simple data types like text and integer are supported).
  • Rules Event: Feeds After Import
  • Rules Data: Feeds Source (properties: ID, imported)

Recommendation: this might work, however there is only a dev version available for this module ... my advice = no go.

2nd attempt: Native Feeds module

The Feeds issue about Rules integration & enable modules to customize imports adds a hook hook_feeds_before_import(). Here are some quotes from that issue (bold markup added here):

... such that a rules integration could use it to customize the imported entities before saving + to skip saving.

Based on the hook we could add an event for it + an action for skipping the import. The entity could be manipulated with the usual rules actions else too.

Of course, in the question here the goal is to not skip saving. But instead "customize the imported entities before saving" and/or "manipulate the entity with the usual rules actions". More specifically: set the value of "state" to "Saved".

Recommendation: much better already, but it is not exactly right yet (and requires using a hook, i.e. writing some custom module) ... There has to be a Rules based solution ...

3rd (and Final) attempt: A rule that should do the job

Inspired (challenged) by the "I tried adding a rule upon feeds presave, but it never gets to the save since it is a draft" part of this question, I created a rule. Here is the export that rule, which I believe should be a perfect answer to the question (also because it doesn't require custom PHP code ...):

{ "rules_update_entity_after_import_via_feeds" : {
    "LABEL" : "Update field imported via feeds",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "workflow_rules", "feeds" ],
    "ON" : { "feeds_import_node" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_workflow_status" } }
    ],
    "DO" : [
      { "workflowfield_field_set_state" : {
          "node" : [ "node" ],
          "field" : [ "node:field-workflow-status" ],
          "workflow_state" : { "value" : { "3" : "3" } }
        }
      }
    ]
  }
}

To get this to work, I enabled the Workflow API module (of course ...), and also it's submodules Workflow UI, Workflow Field and Workflow Rules.

My Workflow itself has only status "Draft" and "Done" (identical to the question here).

The rule above only applies to "node", and only for those "nodes" that have a field defined with machine name "field_workflow_status" (if you want to use another field name, change the rule accordingly and it should work also ...). In my case I used "Article" as the content type, though the "entity_has_field" prevented that I had to hardcode the content type in my rule ...

Obviously, and to be complete, Feeds and Rules are also part of the solution.

7
  • Hi Pierre, that is exactly what I want to do , But I can't figure out how to do it. The examples in that link don't tell me how to do it or how to make a rule to do ti.
    – Diana
    Commented Jul 22, 2015 at 19:16
  • @Dianacastillo : your own answer was too challenging to not try to change your mind again ... Hope you don't mind I further enhanced my answer anyway ... Commented Jul 24, 2015 at 17:19
  • Hi Pierre, I like the code answer better because i can easily modify it if necessary for any variations, basically I understand php much more than the rules language. but I am sure your answer will be helpful to other people who want to use rules ( I haven't got time to test it so not sure if it will work or give bugs.) But if you want the bounty, you can have it as long as you keep on helping me with these tricky questions! Merci beaucoup!
    – Diana
    Commented Jul 24, 2015 at 19:09
  • Merci beaucoup aussi ... But don't worry ... No need for a bounty to keep answering such (sometimes challenging) questions ... I personally disagree with your preferred coding answer, but hey that is what Drupal is often about (pick what you prefer / feel most comfortable with), n'est pas (isn't it)? Autres questions (other questions? Commented Jul 24, 2015 at 19:20
  • not now ! i already gave you the bounty anyways
    – Diana
    Commented Jul 24, 2015 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.