2

I have a people channel that historically has used another field to decide if they're a "featured" person or not.

The field set for "featured" people has now diverged from what a normal person has, so I'm wanting to create a new entry type in the people channel (featuredPeople) and programatically shift these people to the new entry type.

The field set is additive to the old one, so I'm hoping due to there being no field/structural changes this isn't too hard?

I've got the bones of a script together to do this, but I'm struggling to find any info on how to actually change the entry type on the entry.

# Get all people
$peopleCrit = Entry::find();
$peopleCrit->section = "people";
$people = $peopleCrit->all() ?? [];

foreach ($people as $person) {

    # If this was a "featured" person, shift to new featuredPeople entry type.
    if (historical "is featured" check) {

        # Move to featuredPeople entry type
        ???

    }

}

Any help would be greatly appreciated.

1

Something like this should work. I've also cleaned up the code a bit to be correct PHP syntax as my assumption is that you''ll be doing this in a module/plugin.

// Get all people entries
$entries = Entry::find()->section('people')->all();

foreach ($entries as $entry) {
    // If this was a "featured" historical person
    if ($entry->historical == 'featured') {
        // Change to featuredPeople entry type
        $entry->typeId = $featuredPeopleEntryTypeId;

        // Save entry
        Craft::$app->elements->saveElement($entry);
    }
}
  • Hi Ben. That was pseudocode as the historical featured check is a complicated piece of logic on its own. However, just changing the typeId and re-saving does appear to have worked. The field data appears to be intact. I super didn't expect it to be that easy. Thanks. – JamesNZ Nov 21 '18 at 22:08
  • Ah I see. Yes, it's that simple :) – Ben Croker Nov 22 '18 at 8:04

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.