简体   繁体   中英

Drupal 7 API + Taxonomy

I'm attempting to build a small tool with PHP to import content from my current CMS into Drupal 7 because I have about 10k+ articles to bring in. So far I've gotten the title, summary, body, author and published date to come through, but when it comes to categories (tags), I am completely baffled.

Each of my current categories/tags are stored in a database table, each having their own ID, name and description. I can pull this out per article and sort it however I'd like (string, array, etc).

During my import, I'm guessing I should do something like this:

$node->field_tags = array(
    'und' => array(
        array(
            'Update',
            'News',
            'Report'
        )
    )
);

I've also tried:

$node->field_tags = array(
    'Update',
    'News',
    'Report'
);

But these nor feeding in a comma separated string of words doesn't work. The Drupal 7 API documentation doesn't seem to explain this anywhere that I've found.

What's the format for sending tags through or what's the documentation page I haven't been able to locate? Thank you in advance!

Term fields in Drupal 7 are related to physical taxonomy terms, so you'll need to create a term for each category, and then add that reference as the field's value.

This code might help:

// Load the tags vocabulary
$vocab = taxonomy_vocabulary_machine_name_load('tags');

$term = new stdClass;
$term->vid = $vocab->vid; // Attach the vocab id to the new term
$term->name = 'Category Name'; // Give the term a name
taxonomy_term_save($term); // Save it

// Add the tags field
$node->field_tags = array(
  LANGUAGE_NONE => array(
    'tid' => $term->tid // Relate the field to the new category term using it's tid (term id)
  )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM