I created an action in Rules, which generates a duplicate of a node. If I am creating a node of the type "basic", it will duplicate the node for every taxonomy term. So when I have a node with three taxonomy terms, the action will generate three nodes.

The only difference are the taxonomy terms, because the created nodes should get the URL '/taxonomy-term1/node-title', '/taxonomy-term2/node-title', ...

My problem: If I have the taxonomy terms Taxonomy_Term1, Taxonomy_Term2 and Taxonomy_Term3, I get the following result:




I am thinking the problem lies in the array which was created from the latest nid. But when I am printing each term_name (to know what it looks like), the results are the Strings Taxonomy_Term1, Taxonomy_Term2 and so on.

function autocreatenode_rules_action() {
$nid = db_query_range("SELECT nid FROM {node} ORDER BY nid DESC", 0, 1)->   fetchField();
$string = token_replace('[node:field_tags]', array('node' => node_load($nid)));
$terms = explode(',', $string);
$i = 0;
foreach ($terms as $term_name) {
    if($i >= 2) {
        foreach ($terms as $term_name) {
            $node = new stdClass(); 
            $node->type = 'basic';  
            $node->language = 'de';  
            $node->title = 'Title';
            $node->field_body[LANGUAGE_NONE][0]['value'] = 'Something in the body';
            $menu = array();
            $menu['link_title'] = 'Menu Link title';
            $menu['description'] = '';
            $menu['menu_name'] = 'main-menu';

            switch($term_name) {
                case 'Taxonomy_Term1':
                    $menu['plid'] = 5;
                case 'Taxonomy_Term2':
                    $menu['plid'] = 6;
                case 'Taxonomy_Term3':
                    $menu['plid'] = 7;
                    $menu['plid'] = 1;

            $menu['enabled'] = 1;
            $node->menu = $menu;
            $node->status = 1;
            $node->promote = 0;
            $node->sticky = 0;
            $node->comment = 2;
            $node->uid = 1;
            $node->date = 'complaint_post_date';
            $node->created = strtotime('complaint_post_date');
            $node = node_submit($node);
  • What does your pathauto configuration look like? This is where the URLs are generated, right? – Stefanos Petrakis Mar 29 '16 at 12:06
  • I am using tokens in my pathauto [node:menu-link:parent:url:path]/[node:title] – Meito Mar 29 '16 at 18:46
  • Hey @Meito, is this question resolved? Did the answer below resolve it? – Stefanos Petrakis Apr 7 '16 at 17:51
  • Yes, my question is resolved – Meito Apr 10 '16 at 5:21

The code you posted seems a bit frail at places, but if it works for you, we can focus on your specific question.

The main problem is that you are not adding the $menu array to your node object before saving it. You would only need to add it to the object before you save it, similar to this:

$node->menu = $menu;

Cheers and good luck!

P.S.: You can check out this thread for some code examples https://stackoverflow.com/q/2603289/5875098 P.S.2: To get all the terms directly from the $node object:

$terms = array();
$language = 'und'; // or will be provided by some Drupal hooks
foreach ($node->field_tags[$language] as $delta => $value) {
  $termObj = taxonomy_term_load($value['tid']);
  $terms[] = $termObj->name;

You can replace the $terms = explode... in your code with this snippet.

| improve this answer | |
  • I edited my post with the proper use of $node->menu, but the problem still occurs. – Meito Mar 30 '16 at 11:14
  • Then the next possible problem is the plid numbers you use, 5,6,7 should be existing menu items ids in your main-menu. Can you check this? – Stefanos Petrakis Mar 30 '16 at 11:28
  • The plid numbers exist. When I am changing $terms to something simpel like $terms = array('Taxonomy_Term1','Taxonomy_Term2','Taxonomy_Term3'), it works just fine. – Meito Mar 30 '16 at 12:26
  • That means that the token_replace/string manipulation part could be causing the problem. If you get a nid, you could also, as an alternative, load the node and then its terms through the field_tags field. This would be more reliable codewise, you will have a direct access to the node's values, instead of hiding things inside token replacements and exploding strings. :-) – Stefanos Petrakis Mar 30 '16 at 12:30
  • I am using the token_replace/string, because I do not know how to get the terms through the field_tags field. I tried $terms = $node->field_tags[LANGUAGE_NONE][0]['tid'], but that is probably the wrong way – Meito Mar 30 '16 at 14:56

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.