I am attempting to create my first module. I need to manipulate the NID, BID and TID of a node. At this point I just want to be able to print the NID, BID and TID independently from each other.

I am working with Drupal 7. I am triggering off of a rules event that triggers after a node is published. I know the function can trigger because I can see the Hello World statement.

I took a look at taxonomy hooks but I am not sure how to use them. I get some weird errors I do not understand.

<?php
function view_NID_action() {
$terms = taxonomy_get_parents($nid);
rsort($terms);
$myterm = $terms[0]->nid;
echo $myterm;
print_r($terms);
drupal_set_message(t('Hello world!'));
}

1. How do I print out the NID, BID and TID of a node?

2. I do not know how Drupal knows which node to look at. I am assuming that the rules module knows that I want the current node rules is working with to be evaluated. Is this true? Can someone explain this a little to me?

Thanks,

Spineless

Comments

nevets’s picture

Whats your actual goal here?

Rules relies on standard Drupal hooks which in this case are for a node be inserted/updated, so the rule in this case is working on the node just saved.

spineless’s picture

My goal in the end is to add a child page to a book. To get there I need to identify the current node and make it a book.....(this is done with book made simple)... Then I need to publish a new node of a different content type....(this can be done with rules easy enough)....Now I need to make the last node create a child of the book that was just created.

So my first step is to identify the parent and child pages. Then change the BID of the child page so it points to the parent page.....or something like that.

I am not completely sure how Durpal manages these pages so I was thinking the first thing I need to do is get the NID, BID etc of the two nodes I am working with, compare them to other books in the database and then change the appropriate elements to do what I want. Of course to be able to do this I should first be able to just print out the value of the element I am working on. .... This is my problem. How do I extract the NID, BID, TID etc and print them to the screen?

As you maybe thinking.... I am rather new at PHP and Drupal coding. :)

Spineless.

nevets’s picture

How does view_NID_action() get called, I would expect it to be passed a node (or at least a nid).

spineless’s picture

It is called from a hook_rules_action_info()

/**
 *    Implement hook_rules_action_info()
 *    The action function displays the rule to use and returns $act which will display the NID.
 */
function view_NID_rules_action_info() {
  $act = array(
       'view_NID_action' => array(
          'label' => t('NODE-NID'), // Name displayed to admins
          'group' => t('MY rules'), //group displayed in the rules selection
         ),
      );
  return $act;
}
/**
 *  The action function selects the NID and displays it.
 */

function view_NID_action() {
  $terms = taxonomy_get_parents($nid);
  rsort($terms);
  $myterm = $terms[0]->nid;
  echo $myterm;
  print_r($terms);
  drupal_set_message(t('Hello world!'));
}
nevets’s picture

Having not done this, this may not be completely correct, but it looks you want to add something like

      'parameter' => array(
        'node' => array('type' => 'node', 'label' => t('Content')),
      ),

to you action. And the callback would then expect $node as an argument, ie the function declaration becomes view_NID_action($node)

spineless’s picture

I added the parameter... so my code looks like this now.

/**
*    Implement hook_rules_action_info()
*    The action function displays the rule to use and returns $act which will display the NID.
*/
function view_NID_rules_action_info() {
  $act = array(
       'view_NID_action' => array(
          'label' => t('NODE-NID'), // Name displayed to admins
          'group' => t('MY rules'), //group displayed in the rules selection
              'parameter' => array(
               'node' => array('type' => 'node', 'label' => t('Content')),
             ),
         ),
      );
  return $act;
}
/**
*  The action function selects the NID and displays it.
*/

function view_NID_action($node) {
  $terms = taxonomy_get_parents($nid);        //line 55
  rsort($terms);
  $myterm = $terms[0]->nid;                         //line 57
  echo $myterm;
  print_r($terms);
  drupal_set_message(t('Hello world!'));
}

However.... I now get an error message.

Notice: Undefined variable: nid in view_NID_action() ...line 55.....
Notice: Trying to get property of non-object in view_NID_action() ... line 55..
Notice: Undefined offset: 0 in view_NID_action() (line 57.....
Notice: Trying to get property of non-object in view_NID_action() (line 57....

I think maybe the hook taxonomy_get_parents($nid) might not be the hook I need to pull the nid off of a node. Do you have any suggestions on what I should be using?

nevets’s picture

Nodes have a nid, so to get the nid you can use $node->nid.

But taxonomy terms have a tid (which is not the same as a nid) and taxonomy_get_parents() takes a tid. In D7 there is no direct way to get all the terms. But if you want the term(s) for a particular vocabulary then there must be an associated field in the content type, so as a start you would want to look in $node->field_your_field['und'][0] (replacing 'field_your_field' with your actual field name). I believe each element is a term array (or maybe object).

spineless’s picture

I was able to figured out how to capture the Nid. Here is a forum post I was able to use to get help.

http://drupal.org/node/160921

Thanks for you help. It provided me the key I needed to solve my issue. :)