craftcms 3 plugin which imports channel entries

321 Views Asked by At

I try to finish a plugin which imports data into my craftcms project. I already created a console based method/service, which I trigger/run in my shell. Inside my method(s) I receive data (XML or JSON) I parse my data and try to create and fill an entry of a specific channel I already created.

I tried "saveElement()" which doesn't work.

I found some tuts and informations for craftcms v2 (for example: https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods)

Now i am stuck and i can not find any informations on how to solve this with craftcms v3.

Here is my last version of code after hours of different trys :(

$section = Craft::$app->sections->getSectionByHandle('testentry');
$entryTypes = $section->getEntryTypes();
$entryType = $entryTypes[0];
// Save Entry
//$entry = new EntryModel();
$entry = new \craft\elements\Entry();
$entry->sectionId = $section->id;
$entry->typeId = $entryType->id;
//$entry->locale = Craft::$app->i18n->getPrimarySiteLocaleId();
//$entry->authorId = 1;  // TODO:  Set author
$entry->enabled = true;
$entry->postDate = $post['post_date'];
$entry->slug = $post['post_name'];
//      $entry->getContent()->title = $post['post_title'];
//      $entry->setContentFromPost(array(
//          'body' => $postContent,
//          'categoryCareer' => NULL,
//      ));

if (Craft::$app->elements->saveElement($entry)) {
    $result = true;
} 
else {
    echo 'Could not save the Job entry.'."\n";
    $result = false;
}
0

There are 0 best solutions below