[Drupal] How to create a php script to add a new set of category by using existing taxonomy term as reference id in Drupal 6

| | 1 min read

In my previous article "How to create a php script to add a new set of category by using existing taxonomy term as reference id in Drupal 6 ?" I have explained upto replacing taxonomy term using term id. In this article replacing taxonomy term using term name and also how to create a crone job for executing the script in regular intervals.

In the part 1 section of this article "How to create a php script to add a new set of category by using existing taxonomy term as reference id in Drupal 6?" How to add new term by using term id as reference. Please go through this article first before you continue the second part.

Hope you are ready to continue this part....

In case if you doesn't know the term id, instead you know term name in that case you can use function taxonomy_get_term_by_name() and if we have multiple terms of same name we can filter this by category_id. Here also we could save the term in two ways

1. In this method first we will get all the values of the term using the function taxonomy_get_term_by_name() by passing term name as reference. Now if we have multiple terms of same name we filter it using corresponding category_id. Now we will replace the existing taxonomy term with the new. Once we replace the existing term there is no way of getting it back or can refer it.



  //Term Dance and all its subcategories - Stage
  else if (($list->name == "Dance")&&($flag==0))) {
    print "\n";
    $name = "Stage";
    $newvalueload = taxonomy_get_term_by_name($name);
    print "\n";
    foreach ($newvalueload as $newvalueloadvalue) {
      if($newvalueloadvalue->vid==96) {
        print $value->tid = $newvalueloadvalue->tid;
        print $value->vid = $newvalueloadvalue->vid;
        print $value->name = $newvalueloadvalue->name;
        print $value->description = $newvalueloadvalue->description;
        print $value->weight = $newvalueloadvalue->weight;
        print $nodefulload->field_test_category[0]['value'] = 1;
        $flag = 1;
        //node_save($nodefulload);
        print "\n";
      }
    }
  }

2. In this method after filtering using category id. We use the function taxonomy_get_term() by passing term id as refernce and we will add the new term to the node. So that we can use the old category for reference.



  //Term 'Jazz & Folk' and all its subcategories - Music
  else if (($list->name == "Jazz & Folk")&&($flag==0)) {
    print "\n";
    $name = "Music";
    $taxnameload = taxonomy_get_term_by_name($name);
    print "\n";
    foreach ($taxnameload as $taxnameloadvalue) {
      if($taxnameloadvalue->vid==96) {
        $newvalueload = taxonomy_get_term($taxnameloadvalue->tid);
        print "\n";
        print $nodefulload->taxonomy[$newvalueload->tid]=$newvalueload;
        print $nodefulload->field_test_category[0]['value'] = 1;
        $flag = 1;
        //node_save($nodefulload);
        print "\n";
      }
    }
  }

Here is the full script.



chdir(__DIR__);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$nodeevent = db_query("SELECT node.vid AS node_vid, node.nid AS node_nid, content_type_event.nid AS content_type_event_nid, content_type_event.vid AS content_type_event_vid, content_type_event.field_test_category_value AS field_test_category_value, node.type AS type FROM {node} node LEFT JOIN {content_type_event} content_type_event ON  content_type_event.nid = node.nid WHERE type IN ('event') AND content_type_event.vid = node.vid AND field_test_category_value IS NULL LIMIT 5");
  while ($nodeeventset = db_fetch_object($nodeevent)) {
    print "\n";
    print $nodeeventset->node_nid;
    $nodefulload=node_load($nodeeventset->node_nid);
    foreach ($nodefulload->taxonomy as $value) {
      print "\n";
      $categorynid = $value->tid;
      $categoryvid = $value->vid;
      if($categoryvid == 1) {
        $categoryname= $value->name;
        $taxtermload = taxonomy_get_parents_all($categorynid);
        foreach ($taxtermload as $list) {
          $flag=0;

          //Term 'Books and Literature' and all its subcategories - Books
          //Here the esiting category gets repaced
          if (($list->tid == 1)&&($flag==0)) {
            print "\n";
            $newvalueload = taxonomy_get_term(70039);
            print "\n";
            print $value->tid = $newvalueload->tid;
            print $value->vid = $newvalueload->vid;
            print $value->name = $newvalueload->name;
            print $value->description = $newvalueload->description;
            print $value->weight = $newvalueload->weight;
            print $nodefulload->field_test_category[0]['value'] = 1;
            $flag = 1;
            node_save($nodefulload);
            print "\n";
          }

          //Term 'Dance' and all its subcategories - Live Music
          else if (($list->tid == 7)&&($flag==0)) {
            print "\n";
            $newvalueload = taxonomy_get_term(70041);
            print "\n";
            print $nodefulload->taxonomy[$newvalueload->tid]=$newvalueload;
            print $nodefulload->field_test_category[0]['value'] = 1;
            $flag = 1;
            node_save($nodefulload);
            print "\n";
          }

         //Term Rock & Pop and all its subcategories - Stage
         //Here the esiting category gets repaced
         else if (($list->name == "Rock & Pop")&&($flag==0))) {
           print "\n";
           $name = "Music";
           $newvalueload = taxonomy_get_term_by_name($name);
           print "\n";
           foreach ($newvalueload as $newvalueloadvalue) {
             if($newvalueloadvalue->vid==96) {
               print $value->tid = $newvalueloadvalue->tid;
               print $value->vid = $newvalueloadvalue->vid;
               print $value->name = $newvalueloadvalue->name;
               print $value->description = $newvalueloadvalue->description;
               print $value->weight = $newvalueloadvalue->weight;
               print $nodefulload->field_test_category[0]['value'] = 1;
               $flag = 1;
               node_save($nodefulload);
               print "\n";
             }
           }
         }

         //Term 'Jazz & Folk' and all its subcategories - Music
         else if (($list->name == "Jazz & Folk")&&($flag==0)) {
           print "\n";
           $name = "Music";
           $taxnameload = taxonomy_get_term_by_name($name);
           print "\n";
           foreach ($taxnameload as $taxnameloadvalue) {
             if($taxnameloadvalue->vid==96) {
               $newvalueload = taxonomy_get_term($taxnameloadvalue->tid);
               print "\n";
               print $nodefulload->taxonomy[$newvalueload->tid]=$newvalueload;
               print $nodefulload->field_test_category[0]['value'] = 1;
               $flag = 1;
               node_save($nodefulload);
               print "\n";
             }
           }
         }
        }
        if ($flag == 1) {
          break;
        }
      }
    }
  }

So your script is ready. Before you start please check the how many nodes you have to update. If you have a lot of contents, its always better to do it by setting a cron job. In order to achieve this first install a crontab. Crontab file helps to schedule and run your cron job.

To view crontab you can use the following code


  corntab -l

To edit crontab file use the command


  corntab -e

Here I am setting the cron job for every 5min.


*/5 * * * * php location/filename.php

Now if you want to write the result of output of the script to a file, use following command. This will append result to the file each time when cron is executed.


*/5 * * * * php location/filename.php  >> location/filename.txt

Hey its just the theory comeon its time for you to try this. The more you try the more doubt will come to your mind and you may even find a better way to do this. Please fell free to share your thoughts and doubts regarding this.