[support] Import data into existing nodes

Victor Kane victorkane at gmail.com
Mon Dec 28 16:56:29 UTC 2009


Well...

First you have to have your data in something like a CSV file...

Then you need a script which is going to iterate over each line of the CSV
file, see if it can find an existing node to add data to, or else create a
new node.

For PHP code iterating over each line of a CSV file, check out the code in
http://drupal.org/project/node_import module.

Something like this:

        $article_nid = get_nid('title', $article_name, 'article');
        if ($article_nid) {
         $article_node = node_load($article_nid);
       } else {
          $article_node = new StdClass();
        }
        $article_node->type = 'article';
        $article_node->name = 'Article';
        $article_node->format = 3;
        $article_node->status = 1;
        $article_node->promote = 0;
        $article_node->sticky = 0;
        $log = 'Imported by import-issue ' . date('g:i:s a');
...
// cck fields handled a little differently

        $article_node->field_article_publication_issue = array (
          0 => array(
            'nid' => $issue_nid,
          ),
        );
...
        // save node object
        $article_node = node_submit($article_node);
        node_save($article_node);

The get_nid function would be something like this (only works for fields
like 'title' that are in the {node} table itself) or a simplified version of
it:

   /**
   * Return the nid of a node if it exists
   *
   * @param $field_name
   *   The field name upon which the query is based.
   * @param $field_name_string
   *   The string uniquely (in this case)
   *   identifying the node we are searching for.
   * @param $type
   *   The identifier of the content type.
   * @return
   *   $nid or false
   */
  function get_nid ($field_name, $field_name_string, $type) {
   return db_result(db_query("SELECT nid FROM {node} WHERE %s = '%s' AND
type = '%s'",
       $field_name, $field_name_string, $type));
  }

Hope that helps...

Victor Kane
http://awebfactory.com.ar
http://projectflowandtracker.com



On Mon, Dec 28, 2009 at 1:11 PM, Dani Matielo <dacamat at gmail.com> wrote:

> Hello all,
>
> I was wondering if anybody knows a way (a module or an already existing
> script) to import data into new fields for existing nodes. In other words, I
> have already several nodes, and what I wanted to do is to create a new field
> on them and import data from a cvs to them.
>
> Thank you in advance,
>
> Dani
>
> --
> Daniela de Carvalho Matielo
> dacamat.com.br | weblab.tk | lixoeletronico.org
> twitter: @danimatielo
>
> "The only people for me are the mad ones, the ones who are mad to live, mad
> to talk, mad to be saved, desirous of everything at the same time, the ones
> who never yawn or say an uncommon-place thing, but burn, burn, burn like
> fabulous yellow roman candles." ~ Jack Kerouac
>
> --
> [ Drupal support list | http://lists.drupal.org/ ]
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20091228/f5e64e66/attachment.html 


More information about the support mailing list