1

Content type stock contains the term reference field field_producer. What is the rule for Rules in order to fill this field with the node author name on creation?

In detail

The form looks like:

-- Content type Stock (stock)

---- field Producer (field_producer); of field type Term reference; of widget Autocomplete term widget (tagging); number of values: 1; Vocabulary: Producer

The goal is:

When a user creates a node of the type Stock, his user name should automatically be filled/(pre-)populated in the field Producer.

Why Rules:

Since tokens aren't working for the autocomplete term reference field and a text field isn't available, rules might be a workaround. I've tried Prepopulate, Entityreference prepopulate, and other modules, but none of them supported the aim.

Where I am struggling with Rules:

  • Event: After saving new content, Restrict by type Stock
  • Condition: Entity has field, Data selector node:field-producer, Field Value field_producer
  • Action: Set a data value, Data (to be modified) node:field-producer, Value (to set for the specified data): HERE REMAINS THE PROBLEM. I can't choose node:author or other tokens that display the user name. I get the restriction: "Data types: Select data of the type Taxonomy term."

That's why I am not able to successfully create this rule.

What am I doing wrong? How should this rule look like?

2
  • Creation of a node from the type "Stock". I was able to successfully use "After saving new content of type Stock", but I can't make it work with the data types. I get a restriction like "Data types: Select data of the type Taxonomy term." or the added token [node:author] has no effect. What I am trying to do is to use Rules in order to automatically fill the name of a node author as a taxonomy term for the vocabulary "Producer" in the field "field_producer" (so that the author doesn't have to do it manually).
    – xinxin
    Sep 6, 2017 at 16:50
  • Please edit your question to integrate the prior comment (it clarifies your question a little bit). And also explain (via edit, not extra comments) these things: (1) how does that vocabulary look like (does the term "node author" exist already and is it a single value field) (2) what's the machine name of that field of your content type that you want to "automatically fill", and is it a single or multi value field (3) include an export of the rule you already have so far (even if it doesn't work yet) Sep 6, 2017 at 17:41

1 Answer 1

0

... I can't choose node:author or other tokens that display the user name. I get the restriction: "Data types: Select data of the type Taxonomy term."

1. Switch to the direct input mode

You should click the Switch to the direct input mode button (near the bottom left), which looks like this:

enter image description here

After clicking that button, it will bring up a selection list of the available terms (= possible values) that you can use in your taxonomy field.

2. Example

Consider this example, in Rules Export format, of a rule (actually a Rules Component, but the idea is the same) to illustrate how such Rules Action to "Set a data value" should look like and which will set the value of the term reference field field_sample_tags to term id = 31:

{ "rules_replace_a_term_field_by_another_term_field" : {
    "LABEL" : "Replace a term field by another term field",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node" : { "label" : "Node", "type" : "node" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_sample_tags" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_demo_tags" } },
      { "data_is" : { "data" : [ "node:field-demo-tags" ], "value" : "1" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-sample-tags" ], "value" : "31" } },
      { "drupal_message" : { "message" : "Term updated in node with id = [node:nid]" } }
    ]
  }
}

Some more details to explain the above prototype:

  • It uses a node as a parameter (to execute the Rules Component).
  • These are the Rules Conditions:

    • check if the entity (= node) has a field field_sample_tags.
    • check if the entity (= node) has a field field_demo_tags.
    • check if the value of the field field_demo_tags corresponds to the term we want to replace (in this example, the term has id = 1).
  • These are the Rules Actions:

    • Set the value of field field_sample_tags equal to the term with term id = 31 (which is the term in the vocabulary for which we want to add the term reference).
    • Display a message on the site (only used for demo / testing).

3. Improvement - Pick the existing term that fits

Your question doesn't say "How to pick the appropriate term (i.e. its term id) to be used to perform the 'Set a data value' for a given node author (how can you derive that term id from the data about the node author)?".

If you only have a few node authors (say "Xin" and "Pierre"), you could use the Conditional Rules module to improve that Rules Action to 'Set a data value', so that the value to be used for the term id depends on the value of the node author.

4. Open question

Your question doesn't say "What should happen if no term exists yet for a specific node author?". If such scenario is impossible in your site, then you're lucky (and good to go). Otherwise you'll have to further improve the Rules logic you're implementing to:

  1. get the appropriate (new) term created on the fly.
  2. go find out what the term id is for the newly created term.
  3. improve the Rules Action to 'Set a data value' to actually use the term id of this newly created term.

If you wonder how to do that (also), use this link ... 3 times (one for every bullet).

2
  • Thank you very much for your answer, Pierre. To get to your points: Using "Selected data: node:field-producer" in Action, I just get "Taxonomy term identifier" when switching to direct input mode (without any possibility to add tokens). I still want the functionality of the Autocomplete term widget (tagging), meaning choose existing term or add new one. I just don't want the users, old ones with an exiting term and new ones without, to fill their username manually in this field. I want the equivalent of node:author or site:current-user in this field and let taxonomy/term reference work.
    – xinxin
    Sep 8, 2017 at 1:43
  • @XinNod most of my answer is an answer to "What am I doing wrong?" (as in your question). The additional answer to your "How should this rule look like?" (as in your question also) is like: even though I do NOT consider myself as a Rules expert (I'm not Fago ... or Johan Falk), to my knowledge there is no way you can make that to happen with Rules. And the same applies to most of your previous comment. So with that, maybe you do want to consider marking this answer as "accepted", and then post a followup-question (linked to here) like "if Rules cannot do this, how else can I do it?". Sep 8, 2017 at 12:30

Your Answer

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

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