[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
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY thomas.john
5 years ago
Drupal-Technical
0 comments comment

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.


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 01st January 2008 / by webmaster
We have added a few new modules to our site recently. Drupal Technical StumbleThis Leave a reply Your email address will not be published. Required fields are marked * Sean (not verified) access_time 27 Nov 2020 - 22:06 Hi there, I uploaded the files, enabled the modules to use SU, and now I see the link, but there is no icon... How can I fix this? Thanks, Sean webmaster access_time 27 Nov 2020 - 22:06 In reply to Icon doesn't show by Sean (not verified) What do you see when you view the source? Search for stumblethis_button and you should be able to see the code for the image and the URL. Then you should be able to troubleshoot from there. Juicy Couture Addict (not verified) access_time 27 Nov 2020 - 22:06 thanks for the post. would love to hear more of you. by the way, drupal's really popular nowadays as it has easy and fast features. you agree with me? thanks. Add new comment
/themes/custom/zyxpro_light/images/placeholder.png
close

on 16th January 2008 / by webmaster
Most webmasters do not realize this, but a lot of the content on lot of websites can be accessed from multiple URLs. A simple example would be where www.example.com and example.com leads to the same page. This is a fatal mistake in Search Engine Optimization and search engines penalize you for duplicate content. The correct configuration would be where the above two urls will lead you to the same page but example.com will redirect you to www.example.com with a 301 (Moved permanently) status which will not result in search engines penalizing the page. It is very easy to configure 301 redirects using Apache .htaccess file and the process is the same for a Drupal installation also. Drupal Technical Apache htaccess SEO Leave a reply Your email address will not be published. Required fields are marked * Anonymous (not verified) access_time 27 Nov 2020 - 22:06 Hello. I'm trying to make example.com show as www.example.com, and I'm running into difficulties. I'm on Apache 2.0 and using the following lines in my httpd.conf file: RewriteEngine on RewriteCond %{HTTP_HOST} ^xxxxxxxxxx\.com$ [NC] RewriteRule ^(.*)$ http://www.xxxxxxxxxx.com/$1 [L,R=301] When I go to http://xxxxxxxxxx.com, I get http://www.xxxxxxxxxx.com (as expected). However, when I go to http://xxxxxxxxxx.com/node/1 (it's a Drupal site), I get a 404 thrown and the URL changes to xxxxxxxxxx.com/var/www/drupal/node/1. Same thing with www.xxxxxxxxxx.com/node/1. Any suggestions? I want to run without Drupal's .htaccess file (instead incorporating these calls into my httpd.conf file). webmaster access_time 27 Nov 2020 - 22:06 In reply to Rewrite including filesystem path by Anonymous (not verified) I think the problem is with the base path which results in the redirection to /var/www/ part. The best approach I would think is to start with drupal htaccess and then strip out parts and move to httpd SNVC (not verified) access_time 27 Nov 2020 - 22:06 This is definitely a good guide. Thanks for this. wellyson access_time 27 Nov 2020 - 22:06 This is really nice and helpful. Add new comment
/themes/custom/zyxpro_light/images/placeholder.png
close

on 08th February 2008 / by webmaster
Attachment Size slashdotit.tar.gz 173.55 KB Similar to the StumbleThis module that we had created earlier we have created another bookmarking Drupal Technical Slashdotit Leave a reply Your email address will not be published. Required fields are marked * Shlomi Fish (not verified) access_time 27 Nov 2020 - 22:06 Hi! I'm interested in this module, but Drupal 6.2 complains that it's incompatible with it. Can you please update it or tell me how? Regards, Shlomi Fish shlomifish.org webmaster access_time 27 Nov 2020 - 22:06 In reply to Can you update it for Drupal-6.x? by Shlomi Fish (not verified) Sure we are working on this currently and will post an update soon. Also we will post this module on www.drupal.org as well Cheers Anoop John Team Zyxware Anonymous (not verified) access_time 27 Nov 2020 - 22:06 Hello! Nice Article very interesting, thanks Add new comment
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.