1

I want to execute some custom code when going from state A and B. I'm using the rules module and workflow module and trigger when changing state.

This works but the custom code that I want to run generates a PDF file and attaches the new file to the node which executes the state change. But the node is never saved? I have verified that my code works by calling it from devel, which works like a charm.

Is there any behavior in rules or workflow that prevents me from saving my node?

This is a short version of what my code does:

function my_module_my_function($workflow) {

$node = node_load($workflow->nid);

//////////////////////////////////////////////////
//////// Create PDF here
//////////////////////////////////////////////////

// Save generated invoice into drupal
$file_content = file_get_contents($pdf_filepath);
$file = file_save_data($file_content,'private://pdf-file.pdf', FILE_EXISTS_RENAME);

// Prepare invoice struct
$prepare_field_file_doc = array(
               'fid' => $file->fid,
               'filename' => $file->filename,
               'filemime' => $file->filemime,
               'uid' => $workflow->uid,
               'uri' => $file->uri,
               'status' => 1,
               'display' => 1,
               'description' => t('Some text here'),
               );

$node->field_bestallning_faktura['und'][0] = $prepare_field_file_doc;

node_save($node);
}

Lastly, here is my rule

{ "rules_test2" : {
    "LABEL" : "test2",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php", "workflow" ],
    "ON" : { "WorkflowTransition_presave" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "WorkflowTransition:workflow:name" ],
          "value" : "workflow_test"
        }
      },
      { "data_is" : {
          "data" : [ "WorkflowTransition:old-state:name" ],
          "value" : "statea"
        }
      },
      { "data_is" : {
          "data" : [ "WorkflowTransition:new-state:name" ],
          "value" : "stateb"
        }
      }
    ],
    "DO" : [
      { "php_eval" : { "code" : "my_module_my_function($WorkflowTransition);" } }
    ]
  }
}
  • I suggest posting your code here, it might help. – Aram Boyajyan Mar 6 '16 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.