4

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.

4
  • Adding your rule export here may help better understand your problem.
    – drcelus
    Commented Mar 5, 2013 at 9:44
  • create term pro-grammatically with rules using PHP code.
    – monymirza
    Commented Mar 5, 2013 at 11:03
  • I have pasted the exported rule i hope this gives a better understanding of what I am trying to do.
    – Sounique
    Commented Mar 5, 2013 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
    Commented Mar 5, 2013 at 16:08

3 Answers 3

2

Simply add a autocomplete taxonomy field to the content type and when node saves, rule to set data into this field. Taxonomy module will then create the new term if it does not exist.

0

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 [].

6
  • Thank you for your answer. I enabled the PHP filter module and then inserted the above code into the execute PHP command action.
    – Sounique
    Commented Mar 6, 2013 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
    Commented Mar 6, 2013 at 0:18
  • Updated the answer. Commented Mar 6, 2013 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
    Commented Mar 6, 2013 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. Commented Mar 6, 2013 at 15:43
0

Neograph's answer is close, but I had two problems with it:

this is correct to create a new term (in a custom php rules action):

$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

but the below results in 1) not setting a term field on the node and 2) a pdo exception where it tries to insert a new node twice.

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

so instead this works for me:

$node->field_myterm[LANGUAGE_NONE] =  array(array('tid' => $term->tid));

THEN instead of trying to save the node in php, add another action to the the rule: save entity (right after the php action).

Your Answer

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

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