Skip to content

Extract Matrix's entries to Section's entries with the same fields: how? #16774

Closed Answered by brandonkelly
davidwebca asked this question in Q&A
Discussion options

You must be logged in to vote

There’s no built-in functionality for this, but you could make it possible yourself via a custom module + CLI command, using the following code:

use craft\elements\Entry;
use craft\helpers\Db;

$entries = Entry::find()->field('myMatrixField');
$section = Craft::$app->entries->getSectionByHandle('mySection');

foreach (Db::each($entries) as $entry) {
    Craft::$app->elements->duplicateElement($entry, [
        'fieldId' => null,
        'owner' => null,
        'primaryOwner' => null,
        'sectionId' => $section->id,
    ]);
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@davidwebca
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants