1

I need create a node from one table of my Database of drupal with all rows that it have.

That is, I need to extract data from my table and insert them automatically and make it appear as a node. Everything from my own module (That is, when I turn on my module see my table data were converted to node).

I thought of 2 solutions for this. The first is to create the content type in my drupal site and from my module implement "form_submit". (Can you do this?). The second (which I am trying to do) is to create my type of content and form from my module. And since I can do the integration and transformation node me on my table.

Does anyone can help me on how I can do this? A code example of how to extract data and insert them into my form and make a submit directly?

thanks

1 Answer 1

1

From the information you provided I would handle this the following way:

  1. Create a custom content type via the GUI with all the fields that you need.
  2. Create a custom module and register a path via hook_menu(). I would set 'page callback' => 'drupal_get_form', 'page arguments' => array('mymodule_create_content_form') and 'access callback' => 'mymodule_admin_restriction'. mymodule_admin_restriction would be a function to allow access to user 1 only. mymodule_create_content_form would be a custom form builder function with just a submit button.
  3. In the form submit handler I would then write code to extract data from the database and create new nodes from it. The easiest way to get data out of your database would probably be with db_query().
  4. For programmatically creating nodes you could use either node_save() or drupal_execute(). The first one is a quicker option while the second one more closely mimics normal node creation by invoking additional hooks. You should be able to find a solid amount of information about them on the web (for example here, here and here).

This approach should be fairly good to get started with. Once you get the whole thing working without errors (in your development environment) you could move the code to your module's install file's hook_install() and maybe even programmatically create the new content type there if needed.

4
  • Do you have a example in drupal 6? (for node_save) Commented Feb 13, 2012 at 4:17
  • @Eduardo I changed my answer for Drupal 6 so you can check out the new links. This one might be the best to get started with node_save(). You can also try googling 'programmatically create nodes in drupal 6' or similar sentences to find more.
    – Madis
    Commented Feb 13, 2012 at 17:43
  • thanks a lot for you help :D .. I found that page yesterday. But thanks anyway. And I succeeded. However, just running into one small detail: Every time you update (F5), active or disable the module or anything to refresh the page, automatically re-created the nodes from the database. I am looking for ways how to solve that problem. Commented Feb 13, 2012 at 21:10
  • @Eduardo Not sure how you coded the whole thing so can't say much about that. You could try to implement it in a way that requires specific steps to be taken for it to run and/or implement a check to see if the nodes have already been created and if so don't run the script again.
    – Madis
    Commented Feb 14, 2012 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.