Hi, I'm creating my first module that will take a node ID passed as an argument in the URL and have the created node inherit the taxonomy terms of that parent. Also, there's two options: either inherit all terms, or just terms of a particular vocabulary.

The main reason for this is I have a site that uses Taxonomy Access Control Lite and Node Relativity. The problem is, that when a node belongs to a restricted vocabulary or term (by Tac Lite), the created child does not inherit any taxonomy from it's parent so is fully accessible to all users.

I tried adding a direct database insert into the node_access table via the Node relativity module which sort of worked but did not update the site permissions which I had to do manually after every node creation. So, I figured I'd make this a module in itself so it's not reliant on the Node Relativity module and try and add the taxonomy through the normal drupal process hoping that it would go through whatever process the Tac Lite module uses to make sure the permissions table is updated.... I could be wrong.

I have most of the groundwork done but I can't for the life of me find anything online or figure out how to code adding the taxonomy to the node being created.

The module admin settings, you define if all terms to be inherited, or if not, specify which argument should be used (1,2,3,4 etc $passtax_arg)

Here's the nodeapi code which I thought may work but gives me a white screen of death when I insert the node.... although, the node still gets created:

/**
 * Implementation of hook_nodeapi().
 * Not really sure how to do this but must suss it out :p
 * @param string $node holds current node data
 * @param string $op one of "list", "view", "save" and "configure"
  */
function passtax_nodeapi($node, $op, $arg = 0) {
    $passtax_arg = variable_get("passtax_arg", 0);
  switch ($op) {
    case 'insert':
      // dean - only apply this if argument exists
      if ($node->nid && is_numeric(arg($passtax_arg)) && $passtax_arg != 0) {
        $parent_node_array = node_load($passtax_arg);
        if (!empty($parent_node_array->taxonomy)){
          foreach ($parent_node_array->taxonomy as $term) {
            $node->taxonomy[] = array(
            'tid' => $term['tid'],
            'vid' => $term['vid'],
            'name' => $term['name'],
            'description' => $term['description'],
            'weight' => $term['weight']); 
          }
                // drupal_set_message(dpm($parent_node_array->taxonomy)); // debugging only
        }
      }
      break;
  }
}

Comments

matkeane’s picture

Hi,

That's interesting - I started looking at something similar a while back, but didn't get very far with development (more info here http://drupal.org/node/452472 if you're interested).

Quick thought - from memory, so I might be wrong - but isn't $node->taxonomy an object rather than an array? Or an array of term objects?

dean.p’s picture

I've been trying to figure out a way for Child nodes (using the Node Relativity module) to inherit the parent taxonomy terms.

After trying to get this to work for a couple of days without success, and realizing that I was having problems with Node Auto Term NG. I found the Taxonomy defaults module that allows you to specify default taxonomy terms for nodes which I thought was a good start.

hat I was able to do was edit this module to allow optional dynamic default terms via passing a nid in the node create URL although I've only tested using the Node Auto Term NG module which adds the parent nid in the URL.....

Read more: #757982

cibonato’s picture

Hello, I've been trying something like what dean.p did. It's very simple, but I think this "simplicity" brings more complex problems (what happens when I delete a node?). Ok, I do realize I'm inserting data in the tables by myself and it seems not to be a good idea at all, and I have also to say that MUCH MORE WORK have to be done (for instance, it does not care about updating a node).

Here is the code

  /**
   * Implementation of hook_nodeapi().
   *
   * The function is named tag_inheritance_nodeapi().
   */

  function tag_inheritance_nodeapi($node, $op) {

    switch ($op) {

      case 'insert':
        if ($node->type == "portada_secundaria_actividades") {
          $node_parent = node_load($node->field_portada_antecesora[0]['nid']);

          foreach($node_parent->taxonomy as $tag) {
            db_query("INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $tag->tid);
          }
        }
    }
  }

The module I'm using to test is called (by myself, obviously, tag_inheritance). Furthermore, it checks the node type for a reason specific to the problem I have to solve.

Greetings

pkej’s picture

Just subscribing, and asking for a pass_child_tree in addition to the pass_tax ;) See this forum post for info on why.

Paul K Egell-Johnsen

pkej’s picture

Might rules solve the problems? http://drupal.org/node/864674#comment-3252512

Paul K Egell-Johnsen