As discussed in http://drupal.org/node/18207 I have prepared a patch to include support for taxonomy and flexinode in node_import.
DocumentationList support for all columns
If you add "[]" to the end of a fieldname (first row), the module will convert the data in this column to an array of values. Example:
"title","name","type","list[]"
"List test","admin","page","1|2|3"
results in:
stdClass Object
(
[title] => List test
[name] => admin
[type] => page
[body] =>
[list] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
...
)You can override the used seperator (defaults to a vertical bar "|"), but inserting it inside the square brackets. For example, to use a semicolon (";") instead of vertical bar ("|"), you can do:
"title","name","type","list[;]"
"List test","admin","page","1;2;3"
I'm not sure if this is a feature of "CSV" or just of "fgetcsv", but you can specify a newline as seperator too:
"title","name","type","list[
]"
"List test","admin","page","1
2
3"
These last two examples will create the same node as the first example does.
Special handling of taxonomy field
The initial reason for list-support was to import taxonomy terms. For example:
"title","name","type","taxonomy[]"
"Taxonomy test","admin","page","1|2|3"
Looking up the numbers ("tid") for each term is probably error-prune, so the module handles a column with name "taxonomy" specially: if the value of the field is not numeric, we check if the string is not a term defined in a vocabulary. If it is, the value is replaced by the "tid" of this term. For example:
"title","name","type","taxonomy[]"
"Taxonomy test","admin","page","An example term|Another term"
results in:
stdClass Object
(
[title] => Taxonomy test
[name] => admin
[type] => page
[body] =>
[taxonomy] => Array
(
[0] => 5
[1] => 6
)
...
)BTW: if a term is not found, the string is kept as is. The taxonomy module ignores it. For example:
"title","name","type","taxonomy[]"
"Taxonomy test","admin","page","An example term|Another term|No such term"
becomes:
stdClass Object
(
[title] => Taxonomy test
[name] => admin
[type] => page
[body] =>
[taxonomy] => Array
(
[0] => 5
[1] => 6
[2] => No such term
)
...
)And you can mix numbers with strings too:
"title","name","type","taxonomy[]"
"Taxonomy test","admin","page","An example term|1|No such term"
creates:
stdClass Object
(
[title] => Taxonomy test
[name] => admin
[type] => page
[body] =>
[taxonomy] => Array
(
[0] => 5
[1] => 1
[2] => No such term
)
...
)Special handling of flexinode types
With the node_import module you could import flexinodes, you just had to know how. For example, if we define a flexinode ("flexinode-1" = "link") with one field ("flexinode_1" = "url"), we need to create a CSV file as:
"title","name","type","ctype_id","flexinode_1"
"Flexinode test","admin","flexinode-1","1","http://example.org"
I find it easier to write something like:
"title","name","type","url"
"Flexinode test","admin","link","http://example.org"
This patch enable this. The node that gets imported is:
stdClass Object
(
[title] => Flexinode test
[name] => admin
[type] => flexinode-1
[body] =>
url:
http://example.org
[url] => http://example.org
[ctype_id] => 1
[flexinode_1] => http://example.org
...
)Observe that:
- "$node->type" is set to "flexinode-1"
- "$node->ctype_id" is set to the correct value automagically
- both the original "url" and the new "flexinode_1" are present
BTW: the field "flexinode_1" is only added to "$node" when the "type" is a flexinode type that has a field with this label. So when "type" is not a flexinode type or when "type" is a flexinode type without an "url" field, then no "flexinode_1" field is added.
ConclusionThe patch is probably not perfect. If there is something wrong with it (either bug or "Just Plain Wrong(tm)") I like to hear about it.
Some things that might be added (maybe by me):
- if the term is not found, issue a warning?
- if the term is not found, create it?
- if "taxonomy_get_term_by_name" returns more than one term... maybe set them all? maybe issue a warning?
- if the node type is not found, issue a warning?
- cache the lookup of valid fields, so no lookup of "field_ids" is done for each row, but only once for each flexinode-type.
moshe, can you let me know if this is likely to be applied? or what is needed so it can be applied?
Kind regards,
Robrecht
| Comment | File | Size | Author |
|---|---|---|---|
| node_import-list+taxo+flex.patch | 3.39 KB | Robrecht Jacques |
Comments
Comment #1
Robrecht Jacques commentedOops. Just saw you need to apply this patch too: http://drupal.org/node/23644. My patch uses the "node_name_list()" function.
Comment #2
Robrecht Jacques commentedAhum. I meant: http://drupal.org/node/23696.
Comment #3
moshe weitzman commentedlooks great. please apply it yourself to 4.6 and HEAD ... I would appreciate more clear documentation on "Special handling of flexinode types". I read that section several times and still can't work out what you are doing there.
Comment #4
Robrecht Jacques commentedI have applied for a CVS account and will patch node_import.module as soon as I get approved. I'll extend the documentation so it is more clear (flexinode). Maybe I'll add the "add term if not present to a selected vocabulary" too, coz it seems to come in handy for me too. I believe the "Preview" output should be a bit more helpfull in determining errors in the CSV file (eg: non-existant users, non-existing terms, non-existing types, ...). I'm wiling to spend some time to integrate this so node_import.module is more useful / more user friendly.
I'm not sure how long it takes to get approved for a CVS account, but will let you know as soon as I get it.
Kind regards,
Robrecht
Comment #5
Robrecht Jacques commentedApplied for a CVS account, but haven't gotten an answer yet. Is it normal it takes so long (+1 week)? It doesn't look I will be able to apply this patch myself soon. So be it. Too bad.
Comment #6
Robrecht Jacques commentedRemoving this issue from the list:
- the patch was for an old version of "node_import"
- CVS-HEAD supports easy importing of flexinode types already (and in a much cleaner way)
- there is another patch for taxonomy support for CVS-HEAD that I think works nicely (well, I wrote it ;-)) although maybe a bit overcomplex: http://drupal.org/node/24910 .
So the flexinode part of this issue is "fixed" and the taxonomy part is a "duplicate". Hmm, what new status to use.... "Won't fix" ?