Many times while searching for solutions for my Drupal problems I see snippets of code that do various things. How do I run them?
There are several option:
- Create a custom module
- Bootstrap Drupal and then run the code
- Use template.php
…
What I suggest is much easier – if you have the “Devel” module installed and enabled you can use:
http://mydomain.com/devel/php there you’ll get a textarea field to paste and run your code.
Here is some PHP code I used to add an FAQ item to each product node:
1 2 3 4 5 6 |
$nodes = node_load_multiple(array(), array('type' => 'product', 'language' => 'en')); //echo count($nodes); - check how many results foreach($nodes as $node) { $node->field_faq['und'][]['target_id']='11458'; node_save($node); } |
Another similar code – this time checking the subfamily the product belongs to before updating FAQ items:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$i=1; $nodes = node_load_multiple(array(), array('type' => 'product', 'language' => 'en')); //echo count($nodes); //how many results? //print_r($nodes); //here we can find the structure of the node - fields arrays etc. foreach($nodes as $node) { if ($node->field_sub_family['und'][0]['target_id']=='2687') { //find products under this sub family echo (' '.$i.".".$node->title); //show what products you got $i++; //just to ease reading the results $node->field_faq['und'][]['target_id']='9192'; //add new faq $node->field_faq['und'][]['target_id']='9919'; //add 2nd new faq node_save($node); //save changes } } |
The code below is used to go over all products and replace an accessory or cart accessory entity with another:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php // looking for a specific accessory/cart accessory and replace with a different entity $i=0; // replace counter $find = '2614'; $replace = '9337'; $nodes = node_load_multiple(array(), array('type' => 'product')); foreach($nodes as $node) { for ($x = 0; $x <= 20; $x++) { //max number of possible items if (!empty($node->field_accessories_['und'][$x]['target_id'])) { if ($node->field_accessories_['und'][$x]['target_id']==$find) { $node->field_accessories_['und'][$x]['target_id']=$replace; node_save($node); //save changes $i++; } } else break; } for ($x = 0; $x <= 20; $x++) { if (!empty($node->field_cart_accessories['und'][$x]['target_id'])) { if ($node->field_cart_accessories['und'][$x]['target_id']==$find) { $node->field_cart_accessories['und'][$x]['target_id']=$replace; node_save($node); //save changes $i++; } } else break; } } echo ("Done - replaced $i times"); ?> |
Another code showing how to add an accessory (to both product accessories and cart accessories) if it doesn’t exist already:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// looking for a specific accessory/cart accessory and adding if doesn't exist $i=0; // replace counter $add = '3064'; // entity to add $nodes = node_load_multiple(array(), array('type' => 'product')); foreach($nodes as $node) { $found=false; // intial state if ($node->field_sub_family['und'][0]['target_id']=='2117') { // check sub family of node for ($x = 0; $x <= 20; $x++) { //max number of possible items if (!empty($node->field_accessories_['und'][$x]['target_id'])) { if ($node->field_accessories_['und'][$x]['target_id']==$add) $found=true; } else break; // no need to continue for loop if no more assigned values } if (!$found) { $node->field_accessories_['und'][]['target_id']=$add; node_save($node); //save changes $i++; } $fount=false; // we start again for another field for ($x = 0; $x <= 20; $x++) { //max number of possible items if (!empty($node->field_cart_accessories['und'][$x]['target_id'])) { if ($node->field_cart_accessories['und'][$x]['target_id']==$add) $found=true; } else break; // no need to continue for loop if no more assigned values } if (!$found) { $node->field_cart_accessories['und'][]['target_id']=$add; node_save($node); //save changes $i++; } } } echo ("Done - added $i times"); |
Same as above, but add accessory only if the product has a price (product for sale):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$i=0; // replace counter $add = '3064'; // entity to add $nodes = node_load_multiple(array(), array('type' => 'product')); foreach($nodes as $node) { $found=false; // intial state if ($node->field_sub_family['und'][0]['target_id']=='2117' || $node->field_sub_family['und'][0]['target_id']=='2690') { // check sub family of node $price=(int)$node->sell_price; if ($price>0) { for ($x = 0; $x <= 20; $x++) { //max number of possible items if (!empty($node->field_accessories_['und'][$x]['target_id'])) { if ($node->field_accessories_['und'][$x]['target_id']==$add) $found=true; } else break; // no need to continue for loop if no more assigned values } if (!$found) { $node->field_accessories_['und'][]['target_id']=$add; node_save($node); //save changes $i++; } } } } echo ("Done - added $i times"); |
The next code shows how to remove an item from an array of values for a specific field:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$i=0; // replace counter $remove = '3067'; $nodes = node_load_multiple(array(), array('type' => 'product')); foreach($nodes as $node) { $found=false; // intial state if ($node->field_sub_family['und'][0]['target_id']!='2117' && $node->field_sub_family['und'][0]['target_id']!='2690' && $node->field_sub_family['und'][0]['target_id']!='13825' && $node->field_sub_family['und'][0]['target_id']!='13826' && $node->field_sub_family['und'][0]['target_id']!='13827' && $node->field_sub_family['und'][0]['target_id']!='13808' && $node->field_sub_family['und'][0]['target_id']!='13809') { // check sub family of node, exclude PM, PCI, LED and LO for ($x = 0; $x <= 20; $x++) { //max number of possible items if (!empty($node->field_accessories_['und'][$x]['target_id'])) { if ($node->field_accessories_['und'][$x]['target_id']==$remove) { // Remove the field value. unset($node->field_accessories_['und'][$x]); // Reset the array to zero-based sequential keys. array_values($node->field_accessories_['und']); node_save($node); $i++; break; } } else break; // no need to continue for loop if no more assigned values } } } echo ("Done - deleted $i times"); |
The following example check if a product is for sale, if so check its weight. If set to 0, change to 1:
1 2 3 4 5 6 7 8 9 10 11 |
$nodes = node_load_multiple(array(), array('type' => 'bp_product')); foreach($nodes as $node) { $price=(int)$node->sell_price; if ($price>0) { if ($node->weight==0) { $node->weight=1; node_save($node); echo ($node->title." weight changed\n\r"); } } } |
Here is a code that goes over all nodes of type “blog_post” and changes the link structure for the blog post (blog moved to a sub-domain):
1 2 3 4 5 6 7 8 9 10 |
$nodes = node_load_multiple(array(), array('type' => 'blog_post')); foreach($nodes as $node) { $link=$node->field_post_link[und][0]["url"]; //echo(" before: ".$link); //use to test $link=str_replace("https://www.domain.com/blog/","https://blog.domain.com/",$link); //echo(" After: ".$link); //use to test //if the above works well and prints the desired result, add the below 2 lines $node->field_post_link[und][0]["url"]=$link; node_save($node); } |