4

We're working on an XML import plugin to migrate content from another CMS.

What's the best way to create a new category (with a name, slug etc.) and assign it to the entry when running $entryModel = new EntryModel(); ?

6

To create a category, you'd have to use the craft()->categories->saveCategory() method, and feed it a CategoryModel. Having done that, you can use the ID of your new category to create an array for the entry's category field (in this example its handle is categoryFieldHandle):

$category = new CategoryModel();
$category->groupId = 2; // Or whatever ID your new category's group has.
$category->getContent()->title = 'My new category title'; // Slug will be automagically created

if(craft()->categories->saveCategory($category)) {
    $entryModel = new EntryModel();
    $entryModel->title = 'My entry's title';
    $entryModel->categoryFieldHandle[] = $category->id;

    if(craft()->entries->saveEntry($entryModel)) {
        ...
    }
}
  • 1
    Assuming that more than one entry will have the same category, you might also need to add a conditional to check whether the category already exists. – Douglas McDonald Jan 26 '15 at 18:15
  • +1 to what @DouglasMcDonald said - I'm working through that right now. – Adam McCombs Jan 27 '15 at 1:29
  • @DouglasMcDonald, how would I do that? Is there a specific utility method for that purpose? Thanks! – Floriskoch Nov 21 '16 at 16:53
  • @Floriskoch No special utility methods. You would just need to test for the existence of the category using the groupId and category title. I would probably use an ElementCriteriaModel for this. – Douglas McDonald Dec 4 '16 at 13:21
2

Adding to Paul Verheul's excellent answer (and to answer question by Floriskoch) — to test for the existence of a category before creating it, you might try something like the following (untested).

// Get category group from known id
$categoryGroup = craft()->categories->getGroupById(2);

// Find categories matching group and title
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = $categoryGroup;
$criteria->title = "My Category Title";
$criteria->limit = null;
$categories = craft()->elements->findElements($criteria);

// 1 or more matching categories exist; select first category
if($categories) {
    $categoryId = $categories[0]->id; // only want first category if more than one with the same title

// no categories exist; create category
} else {
    $category = new CategoryModel();
    $category->groupId = 2;
    $category->getContent()->title = 'My new category title';
    if(craft()->categories->saveCategory($category)) {
        $categoryId = $category->id;
    }
}

// double check we have a category id
if($categoryId) {
    $entryModel = new EntryModel();
    $entryModel->title = 'My entry's title';
    $entryModel->myCategoryFieldHandle[] = $categoryId;

    if(craft()->entries->saveEntry($entryModel)) {
        ...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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