Thursday, September 13, 2012

Adding Drupal nodes with a specific ID

As part of a drupal upgrade/migration I had a requirement to create a few thousand nodes with specific IDs. The internet said it could not be done but I did not believe it.

After diving into the bowels of the drupal code I found the spot which did the creating and it seemed to be doing something quite reasonable with the new node ID.

With just a small amount of convincing I managed to get it to do what I wanted.

diff -ru vanilla/drupal-7.12/modules/node/node.module web/drupal/modules/node/node.module
--- vanilla/drupal-7.12/modules/node/node.module        2012-02-02 09:03:14.000000000 +1100
+++ web/drupal/modules/node/node.module     2012-05-28 16:37:36.827171000 +1000
@@ -1095,6 +1095,12 @@
     if ($node->is_new) {
       // For new nodes, save new records for both the node itself and the node
       // revision.
+if(isset($node->request_nid))
+{
+       echo "Requested save new node with nid {$node->request_nid}\n";
+       $node->nid = $node->request_nid;
+       //print_r($node);
+}
       drupal_write_record('node', $node);
       _node_save_revision($node, $user->uid);
       $op = 'insert';


Creating a node can now be done like this:
$node = new stdClass();
$node->type = ...;
node_object_prepare($node);
// fill in node details here...
$node->request_nid = $my_nid;
node_save($node);


That seems quite trivial now. I don't know why so many people say it can't be done.

30 comments:

  1. Great post! Thanks a lot for sharing with us.
    Kevin Right

    ReplyDelete
  2. Your site has a lot of useful information for myself. I visit regularly. Hope to have more quality items.
    shell shockers unblocked

    ReplyDelete
  3. -Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.

    gmail login

    ReplyDelete
  4. Manga Panda is a popular Web site for Manga. You will also find a series of the latest manga comics on this web site. These latest manga are alphabetically arranged.
    There's 1000 free English translation comic series available at Mangapanda. Popular Manga, Manga List, and Latest Releases tabs are available on the Manga Panda website. The most popular web site on Manga.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I haven't tried doing it yet but I guess I have to try. Thanks!

    https://www.seo-tampa.org

    ReplyDelete
  7. We have formally relaunched YesPornPlease with a large number of recordings, channels for pornography adoring network. In the event that you love free full-length HD pornography recordings, that YesPornPlease can offer, at that point this cylinder site is for you. Snap and appreciate most sultry XXX videos for nothing!

    ReplyDelete
  8. Brazil is a member of the United Nations (UN). Brazil is a member of the World Trade Organization (WTO). Brazil is a member of the International Monetery Fun (IMF). http://www.confiduss.com/en/jurisdictions/brazil/politics/

    ReplyDelete
  9. Our feet are often overused and receive the most beating from our daily activities. More often, people tend to think that pedicure treatment is a luxury indulgence.

    Wrong! You need to understand that getting pedicure provides several advantages for you and your health. They keep your feet healthy and looking young.

    Now, the challenge comes where to buy the best Pedicure Kit Online . In today’s cosmetic world, various products promise to work for your feet only to make your situation worse.

    Some of these products are infused with harmful chemical elements that might be harsh on your skin. Other products might not work at all. It can be painful to lose your money and, worst, harm your skin.

    ReplyDelete
  10. Hello there! I could have sworn I’ve been to this blog
    before but after reading through some of the post I realized
    it’s new to me. Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and checking back
    frequently!

    카지노사이트
    바카라사이트
    홈카지노

    ReplyDelete
  11. Attractive section of content. I just stumbled upon your web site
    and in accession capital to assert that I acquire in fact enjoyed account your blog posts.
    Anyway I will be subscribing to your feeds and even I achievement you access consistently
    fast.


    바카라사이트
    온라인카지노
    카지노

    ReplyDelete
  12. I want to to thank you for this good read!! I absolutely loved every bit of it.

    I’ve got you book-marked to look at new things you post…

    스포츠토토
    토토사이트
    먹튀검증


    ReplyDelete
  13. I think the admin of this site is actually working hard in favor of his web site, because here every data is quality based
    information.


    토토사이트
    토토
    안전놀이터

    ReplyDelete
  14. This type of article will be able 야설 to think and analyzed to the readers thats leads them in a bright future.

    ReplyDelete
  15. I always like finding a site that gives you very good ideas because I like learning new stuff. Happy that I found your site because I greatly liked it and I anticipate your following po st. A fantastic blog and i’ll come back again for more useful content…
    일본야동

    ReplyDelete
  16. I think this is one of the most significant information for me. And i’m glad reading your article.국산야동 But should remark on some general things, The web site style is perfect, the articles is really great.

    ReplyDelete
  17. Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

    Educative.io
    Information
    Click Here
    Visit Web

    ReplyDelete
  18. Glad to see this topic, I gained new knowledge because of this. Thanks. https://cincinnatiseo.io/

    ReplyDelete
  19. Glad to found this post. Click here for Driveway caulking

    ReplyDelete
  20. Hello! Nice to meet you, I say . The name of the community I run is 안전놀이터추천, and the community I run contains articles similar to your blog. If you have time, I would be very grateful if you visit my site .

    ReplyDelete
  21. Excellent read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch as I found it for him smile Therefore let me rephrase that: Thank you for lunch. 메이저사이트

    ReplyDelete
  22. Very detailed and high quality instructions, thank you! I want to add that to find a good developer nowadays it is not enough to look through various sites with resumes and the like on your own. The market is very saturated and you need to entrust this job to a professional HR specialist. If you don't have such an expert on your staff, the best thing to do is to contact a company that offers hr outsourcing solutionsfor outsourcing.

    ReplyDelete
  23. I am impressed. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. fnf

    ReplyDelete