1

I'm trying to create an jQuery/Ajax script to import data from a Wordpress API service and want to import the WP tags into my database.

Everything works as expected when I loop through the data and send it to /actions/tags/createTag but I'd like to keep the original tag IDs in order to be able to import the posts later and keep the relationship with their tags (Craft automatically assigns a length+1 ID to new tags).

Any way I can do this?

4
  • Crafts shares all ids for elements so when you create an entry it starts with id 1, when you create a category it has id 2, your tag has id 3 and so on. You won't be able to keep your ids because of that. I would suggest you to save the old/new ids and map them into an array. I could explain you how if you want Commented Mar 14, 2018 at 21:32
  • @RobinSchambach That would be great! Thanks
    – brunouno
    Commented Mar 14, 2018 at 21:38
  • Do you use Craft 2 or Craft 3? Commented Mar 14, 2018 at 22:27
  • Right now I'm using Craft-2.6.2991
    – brunouno
    Commented Mar 15, 2018 at 14:53

1 Answer 1

0

You'll need to create a new table/record to store the data

class IdRecord extends BaseRecord
{
    /**
     * Returns the name of the database table the model is associated with (sans table prefix). By convention,
     * tables created by plugins should be prefixed with the plugin name and an underscore.
     *
     * @return string
     */
    public function getTableName()
    {
        return 'ids';
    }

    /**
     * Returns an array of attributes which map back to columns in the database table.
     *
     * @access protected
     * @return array
     */
    protected function defineAttributes()
    {
        return array(
            'oldId'   => AttributeType::Number,
            'newId' => AttributeType::Number
        );
    }
}

Create a new Controller and store with the record

public function actionCreateTag()
{
    $this->requireLogin();
    $this->requireAjaxRequest();

    $tag = new TagModel();
    $tag->groupId = craft()->request->getRequiredPost('groupId');
    $tag->getContent()->title = trim(craft()->request->getRequiredPost('title'));
    $oldId = craft()->request->getRequiredPost('oldId');

    if (craft()->tags->saveTag($tag))
    {

        $tagMap = new IdRecord();
        $tagMap->oldId = $oldId;
        $tagMap->newId =  $tag->id;
        $tagMap->save();
        $this->returnJson(array(
            'success' => true,
            'id'      => $tag->id
        ));
    }
    else
    {
        $this->returnJson(array(
            'success' => false
        ));
    }
}

And when you are saving your entries you'll do

$entry = new EntryModel();
// ... set your content
$oldId = 'number of your old WP tag id';
$tag = IdRecord::model()->find('oldId = :oldId',[
    'oldId' => $oldId
]);
$entry->getContent()->setAttribute('yourTagFieldHandle', $tag->newId);

You can use pluginfactory.io to create your boilerplate.

Edit

Another approach. If you receive the title of the tags as an array like ['tag1', 'tag2', 'tag3'] would be something like

$ids = [];
foreach($tagTitles as $tagTitle){
    $criteria = craft()->elements->getCriteria(ElementType::Tag);
    $criteria->title = 'your Tag Title';
    $tag = $criteria->first();
    if($tag){
        $ids[] = $tag->id;
    }
}
$entry->getContent()->setAttribute('yourFieldHandle', $ids);

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.