As far as I can tell, there isn't an existing method for mapping content imported via Feeds to workflow states. (There is, of course, the ability to map to the core workflow statuses like published/ not published). It would be a great feature to have. Or, perhaps I'm wrong, it exists, and I just couldn't find it.

Comments

nancydru’s picture

Status: Active » Postponed

Patches always welcome

johnv’s picture

Issue summary: View changes
Status: Postponed » Active

Patches are still welcome..

nwom’s picture

Version: 7.x-1.0 » 7.x-2.x-dev

This functionality would be amazing. I would also be extremely grateful if this was somehow possible.

johnv’s picture

A workflow_field should be settable as if it was a 'normal' field.
You cannot set the comment then, but you may try function hook_workflow_comment_alter() for that. (See file workfow.api.php)

As Nancy said: Patches, scripts and experiences are welcome.

nwom’s picture

For those looking for a dirty workaround:

  • Import the workflow transition in a temporary hidden field (hide via Panels or Display Suite)
  • Set up a Rule via Rules to set value of the Workflow field based on the temporary field.
renzomb’s picture

Im working in a better way to do this.

For now i have a MVP that works. This is my workaround:

Declare the mapper:

function mymodule_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  $targets['workflow'] = array(
    'name' => t('Workflow State Name'), 
    'description' => t('What editorial state the node is in.'), 
    'callback' => 'mymodule_mapper_set_target', 
  );
}

Get the value:

function mymodule_mapper_set_target($source, $entity, $target, $value, $mapping) {
    $entity->workflow = $value;
}

Make the transisiton, after the the node has been created.

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
    }
    // get workflow object: Name, label, sid, etc.
    $states = array();
    $workflows = workflow_get_workflows();
    foreach ($workflows as $workflow) {
        $options[$workflow->name] = array();
        $states = workflow_get_workflow_states_by_wid($workflow->wid);
        foreach ($states as $state) {

//Is hardcoded the position 24, where the json parser  have the value of the workflow that has been imported, in this case the Label.
//You can recieve the sid, the name machine, the label. You can processs with feeds tamper to normalize and then make the conditional.

	          if($item['jsonpath_parser:24'] == $state->state){  
	              $new_sid = $state->sid;			
	          }
        }
    }
  $transition = new WorkflowTransition();
  $entity_type = 'node';

  $old_sid = 1; // sid of created status
  $uid = $user->uid; // You can change with uid of author.
  $stamp = REQUEST_TIME;
  $comment = 'Migration to new D7';
  $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);
}

There is a lot of work to do, and some border case to check. But with only a workflow of 5 states and with the same label on the 2 servers its working like a charm.

This is a work in progress so if needed i can keep updating this code in my search to the better way.

sagar ramgade’s picture

I used the following code for workflow field, which works fine:

/*
 * Implements hook_feeds_processor_targets_alter().
 */
function my_module_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
if ($entity_type == 'node') {
    $targets['workflow_state'] = array(
      'name' => t('Set the workflow state'),
      'description' => t('Set the workflow state'),
      'callback' => 'fml_pdf_feeds_set_workflow_state',
    );
  }
}

// Callback function to set workflow state.
function my_module_feeds_set_workflow_state($source, $entity, $target, $value) {
  $field  = workflow_get_field_name($entity, 'node');
  if (!empty($field)) {
    $field_info = field_info_field($field);
    $workflow_id = $field_info['settings']['wid'];
    $states = workflow_get_workflow_state_names($workflow_id);
    if (!empty($value[0])) {
      $sid = array_search($value[0], $states);
      if (!empty($sid)) {
        $entity->{$field}[LANGUAGE_NONE][0]['value'] = $sid;
      }
    }
  }
}
johnv’s picture

Status: Active » Closed (outdated)

New features for D7 are not processed anymore.