1

In this code I try to create a new Entry and set a Parent to it. The sectionId and the typeId are actually the same.

$navigationItem = new Entry();
$navigationItem->typeId = 4;
$navigationItem->sectionId = $navigationSection->id;
$navigationItem->title = $entry->title;
$navigationItem->setParent($herbsNavigationEntry);

$navigationItem->setFieldValue('navLink', '/tags/' . $entry->slug);

if (!Craft::$app->elements->saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry "' . $entry->title . '"', __METHOD__);
}

Before saving I get this:

enter image description here

But after the Craft::$app->elements->saveElement($navigationItem), the parent disappears.

What did I wrong?

2

You need to use newParentId:

$navigationItem = new Entry();
$navigationItem->typeId = 4;
$navigationItem->sectionId = $navigationSection->id;
$navigationItem->title = $entry->title;
$navigationItem->newParentId = $herbsNavigationEntry->id;

$navigationItem->setFieldValue('navLink', '/tags/' . $entry->slug);

if (!Craft::$app->elements->saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry "' . $entry->title . '"', __METHOD__);
}
| improve this answer | |
  • That works fine. Anyway PHPStorm tells me: Field 'newParentId' is marked as @internal. But it works. Thank you. – Slowwie Sep 26 at 17:22

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.