Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am using the rules module to automatically create a taxonomy term when i create content of a certain type. I would also like to automatically populate the taxonomy term reference in the the created content. I have tried many different configurations using the set data function in rules however it does not ever set the taxonomy term to the the new-term which is created.

Is there any way that this operation can be carried out using rules.

I have exported the rule for you to take a look at.

/**
 * @file
 * advertising_region_creator.rules_defaults.inc
 */

/**
 * Implements hook_default_rules_configuration().
 */
function advertising_region_creator_default_rules_configuration() {
  $items = array();
  $items['rules_auto_taxonomy'] = entity_import('rules_config', '{ "rules_auto_taxonomy" : {
      "LABEL" : "Auto Taxonomy",
      "PLUGIN" : "reaction rule",
      "REQUIRES" : [ "rules" ],
      "ON" : [ "node_insert" ],
      "IF" : [
        { "node_is_of_type" : {
            "node" : [ "node" ],
            "type" : { "value" : { "savings_viewer" : "savings_viewer" } }
          }
        }
      ],
      "DO" : [
        { "entity_create" : {
            "USING" : {
              "type" : "taxonomy_term",
              "param_name" : "[node:title]",
              "param_vocabulary" : "2"
            },
            "PROVIDE" : { "entity_created" : { "new_term" : "New Term" } }
          }
        },
        { "data_set" : {
            "data" : [ "node:field-savings-viewer-region" ],
            "value" : [ "new-term" ]
          }
        }
      ]
    }
  }');
  return $items;
}

I apologize if this is posted in the wrong format I am new to all of this but will keep trying to get better.

share|improve this question
Adding your rule export here may help better understand your problem. – drcelus Mar 5 at 9:44
create term pro-grammatically with rules using PHP code. – monymirza Mar 5 at 11:03
I have pasted the exported rule i hope this gives a better understanding of what I am trying to do. – Sounique Mar 5 at 16:08
I am unsure on how to create the rule using PHP at this point. if there is any resources or help you could provide on using PHP to create the rule it will be greatly appreciated. the only concept I could think of is to hard code it in the database using an edit command. However I something tells me that this is not what you had in mind monymirza – Sounique Mar 5 at 16:08
Thank you both for your help in this matter – Sounique Mar 5 at 16:12

1 Answer

If you enable 'PHP filter' module (in the Core package) you will unlock an additional action rule 'Execute custom php code'. There you can use drupal API commands to both create and attach the terms. (Better to do the creation as well, since then you can get the ID of the created term.)

From here it depends if you have drupal 6 or 7. The code is for 7, but should get you started with 6 as well.

The final code would be something like this, but I have not tested it.

$term = new stdClass(); // Make a new term object
$term->name = $name; // New term name
$term->vid = $vid; // Term's vocabulary ID
taxonomy_term_save($term); // Save the term

$node->taxonomy[] = $term->tid; // Attach the taxonomy term ID of saved term
node_save($node); // Save the node again with the term

Update

  • Make sure that you try to run this code on the event 'after saving a new node', otherwise there won't be a node id yet.

  • If that does not work, try using only $node->taxonomy without the [].

share|improve this answer
Thank you for your answer. I enabled the PHP filter module and then inserted the above code into the execute PHP command action. – Sounique Mar 6 at 0:12
sorry about the last comment... I entered the code into the execute php script action and changed the vid to my vocabulary ID. It adds the taxonomy term however does not attach it to the node content. – Sounique Mar 6 at 0:18
Updated the answer. – Neograph734 Mar 6 at 10:11
I do have token enabled and i can save the taxonomy as the [node:nid], which tells me that rules is reading the [node:nid] string. However it still will not update the newly saved node content to attach the term. – Sounique Mar 6 at 14:08
I see... I just remembered that rules automatically gives you access to the $node variable. Try running with only the last 2 lines as in the edited example. – Neograph734 Mar 6 at 15:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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