2

Steps to reproduce this use case:

  1. [Rule event] Submit data on a specific Webform (new submission)
  2. [Rule action] Create content node by type
  3. [Rule condition] Use 'content is of type' condition
  4. [Rule action] Access Webform submission data to set node content fields

How can 3 & 4 above be executed in this order and allow for access to both the created entity and webform submission? i.e. - by passing arguments or otherwise.

Related articles which get some of the way there but did not answer my question entirely:

2

Assuming you are using Webform Rules, you should move 3 and 4 to a Rules Component, but unfortunately Webform Rules does not allow to expose webform data (transfer submission between rules and components) without the patch from here https://www.drupal.org/node/2020149 Without it the data is only available using tokens, and you can not even set the webform data to the variable.

There is another solution, you can extract the Webform SID ([data:sid]) and send it to your Component together with newly created entity and webform itself, then inside your component you can convert received [data:sid] to integer and fetch the submission (Rule Action: Fetch Webform Submissions) and access it using PHP. You will also be able to use Entity:has field inside your component to access all the fields from your newly created entity.

Something like that (component): component actions

The component parameters has: component parameters

  • This got me almost there. I just create a component that accepted a Webform Node object and an SID integer argument. I then called this action set component conditionally in code using rules_invoke_component('rules_webform_submission_to_node', node_load($nid), (int) $sid);. I created the entity in the component then and had access to the specific fields, only problem is I couldn't set them with the freedom I needed. I had problems setting fields such as image, or list items, etc. As what seems to be the majority of cases like this, I will do this in code now as Rules doesn't seem to be able. – mccrodp Aug 15 '16 at 11:52
1

I assume your issue is to keep the order of the steps and run a Rule Condition (#3) after a Rule action (#2) within the same rule. @No Sssweat's solution is a possible way to do this. In the past I've use Conditional Rules to create conditions within the action portion of the Rules interface. This has allowed me to bring entities to scope without the need of creating additional Rules Components.

  • 1
    I was thinking the same way, also using Conditional Rules (but IMO the question is missing a lot of details to actually post a rule to do so) ... – Pierre.Vriens Aug 12 '16 at 15:06
  • Please let me know what details are missing in the question Pierre and I'll add them. I'm basically trying to convert a Webform submission to a node (with various field types) using Rules. The order of the steps doesn't matter. I just outlined what seemed as close as possible to rules logic. I will eventually attach the execution of the rule to a custom button "Copy to Node". Thanks. – mccrodp Aug 12 '16 at 15:22
  • Thank you, this is a solution I will investigate in future. Alexy's solution had screenshots and didn't need an additional module, so I accepted that. – mccrodp Aug 15 '16 at 11:56
0

To achieve your goal (= "... make entity fields appear in Rules when converting a Webform submission to node ...", as in your question title), you can use a technique similar to what is described in my answer to "How to do a data comparison to submitted webform data when using the Rules module?". For short:

  • Add an extra Rules Action to Add a variable (for each of the Webform components to transpose the information across, with a value assigned to it equal to your [data:ComponentName-value-raw] (and with a data type which equals the data type of your Webform component).
  • In your subsequent Rules Actions you can refer to the value of these variables you just added (that's the clue!).

Demo

Webform components

Assume a webform with form_id=191 and with these Webform Components:

  • Label = Title, Machine name = demoform_title, Data type = Textfield.
  • Label = Text, Machine name = demoform_text, Data type = Textarea.
  • Label = Number, Machine name = demoform_number, Data type = Number.
  • Label = Option, Machine name = demoform_option, Data type = Select options (single value).

Article content type

Assume an Article content type with these fields:

  • Label = Title, Machine name = title, Field type = Node module element.
  • Label = Body, Machine name = body, Field type = Long text and summary.
  • Label = Free Format Nr, Machine name = field_free_format_nr, Field type = Integer.
  • Label = OptionList, Machine name = field_optionlist, Field type = List(text).

Custom Rule

Here is a custom rule (in Rules export format), which will create a new node (of type Article), and which will use the data included in the above mentioned Webform components:

{ "rules_create_a_node_from_webform_data" : {
    "LABEL" : "Create a node from webform data",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "webform_rules", "rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "webform_has_id" : {
          "form_id" : [ "form_id" ],
          "selected_webform" : { "value" : { "webform-client-form-191" : "webform-client-form-191" } }
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "The webform submitted contains these fields:\r\n\u003C\/ul\u003E\r\n\u003Cli\u003Ethe field labeled \u0022[data:demoform_title-title]\u0022 which has value \u0022[data:demoform_title-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_text-title]\u0022 which has value \u0022[data:demoform_text-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_number-title]\u0022 which has value \u0022[data:demoform_number-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_option-title]\u0022 which has value \u0022[data:demoform_option-value-raw]\u0022.\u003C\/li\u003E\r\n\u003C\/ul\u003E" } },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:demoform_text-value-raw]" },
          "PROVIDE" : { "variable_added" : { "var_demoform_text" : "Form variable text" } }
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:demoform_number-value-raw]" },
          "PROVIDE" : { "variable_added" : { "var_demoform_number_text" : "Form variable number text" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "var-demoform-number-text" ] },
          "PROVIDE" : { "conversion_result" : { "var_demoformvar_number" : "Form variable number" } }
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:demoform_option-value-raw]" },
          "PROVIDE" : { "variable_added" : { "var_demoform_option" : " Form variable option" } }
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "article",
            "param_title" : "[data:demoform_title-value-raw]",
            "param_author" : [ "user" ]
          },
          "PROVIDE" : { "entity_created" : { "article_created" : "Created article" } }
        }
      },
      { "data_set" : {
          "data" : [ "article-created:body:value" ],
          "value" : [ "var-demoform-text" ]
        }
      },
      { "data_set" : {
          "data" : [ "article-created:field-free-format-nr" ],
          "value" : [ "var-demoformvar-number" ]
        }
      },
      { "data_set" : {
          "data" : [ "article-created:field-optionlist" ],
          "value" : [ "var-demoform-option" ]
        }
      },
      { "data_set" : { "data" : [ "article-created:comment" ], "value" : "0" } },
      { "entity_save" : { "data" : [ "article-created" ], "immediate" : "1" } },
      { "drupal_message" : { "message" : "A new article got created, with nid=[article-created:nid] and url=[article-created:url]" } }
    ]
  }
}

Some more details about the Rules Actions that were added:

  • Show a message on the site: This is just for debugging/demo purposes (remove it you don't want/need it anymore). Here is a sample message that gets displayed after submitting a webform:

    The webform submitted contains these fields:

    • the field labeled "Title" which has value "Some interesting title of my webform".
    • the field labeled "Text" which has value "Just some text, with some additional text and yet another line and another one".
    • the field labeled "Number" which has value "842316".
    • the field labeled "Option" which has value "A".
  • Add a variable (twice): To make the value of the Webform components demoform_number and demoform_option available in subsequent Rules Actions. Note that for demoform_number an additional Rules Action Convert data type is needed to get it to work.

  • Set a data value (3 times): To populate the node's body and the 2 node fields, using the variables created in a preceeding Rules Action.

  • Set a data value: To set "Comments closed" for the node being created (this is an optional Rules Action, just an illustration of other things you can do with such node being created).

  • Save entity: To actually save the node.

  • Show a message on the site: This is just for debugging/demo purposes (remove it you don't want/need it anymore). Here is a sample message that gets displayed after submitting a webform:

    A new article got created, with nid=123 and url=http://www.example.com/node/123

Final result

After submitting the webform with data as in the sample above, a new article (with node id = 123) got created, and with these details about that node:

  • Title: Some interesting title of my webform
  • Body:

    Just some text,

    with some additional text

    and yet another line

    and another one

  • Free Format Nr: 842316

  • OptionList: Advanced (the label corresponding to key-value "A")

Notes

  • The custom rule in this answer does not require the (dangerous) PHP filter to be enabled (as opposed to what is mentioned in the currently accepted answer). Nor does it require the patch mentioned in the currently accepted answer.

  • This answer should address your "Step 4" (in your question). While it seems to me that you don't need "Step 3" (in your question). Unless you want to create multiple types of nodes, e.g. based on some value specified in any of your webform components. If that's the case, all you need to do is to further improve the set of the Rules Actions in my sample rule, by also using the Conditional Rules module.

Your Answer

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

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