2

I have a site we are importing into Craft (using https://github.com/boboldehampsink/import/) and it has about 10 fields setup called feature1, feature2, ... feature10 that are Plain Text. After I import them, rather than leave them all as individual text fields, is there a way to merge them into a matrix block or table field? I have the data in a CSV as well that I am using for the import.

2

You can obviously perform your import directly from the CSV files (or remote database), reformat the data and then import; or import the data into temporary craft text fields and then in a separate operation, move the data in craft. Personally I would opt for the first solution to avoid the extra work of creating temp fields (and allow me to import again later if needed).

This example is for importing into a table field (importing into a matrix field would be quite different and slightly more complicated).

// loop through each row in your CSV file or results on external database query
foreach($result as $row)
{
    $entry = new EntryModel();
    $entry->sectionId   = 1;
    $entry->typeId      = 1;
    $entry->authorId    = 1;
    $entry->enabled     = true;

    // create temp array to store feature data
    $myTableFieldData = [];
    array_push($myTableFieldData, array("col1" => $row['feature1']));
    array_push($myTableFieldData, array("col1" => $row['feature2']));
    array_push($myTableFieldData, array("col1" => $row['feature3']));
    // ...

    $entry->getContent()->setAttributes(array(
        'myTableFieldHandle' => $myTableFieldData,
    ));

    // if you want to check the data first before importing
    // uncomment this and comment out saveEntry below
    //var_dump($entry);

    $success = craft()->entries->saveEntry($entry);

    if (!$success)
    {
        $errors = $entry->getErrors();
        foreach ($errors as $error) {
            MyImportPlugin::log('Error:'.$error[0], LogLevel::Error);
        }
    }
    else
    {
        MyImportPlugin::log('Successfully saved entry "'.$entry->id.'"', LogLevel::Info);
    };

}
  • Do I just save this to a .php file and add to plugin folder, or do I just run in a PHP file on its own? Never build a plugin before. I appreciate the help Douglas. – Bryan Garrant Apr 8 '15 at 15:56
  • That question is a bit more complicated. And I guess I assumed that you were already familiar with plugins. This example represents an (incomplete) method in a Service file which would be one part of your plugin. You would call this service method from either an init function in your main plugin file; via a Controller method (defined in your plugins controller file that itself would be triggered by hitting a specific url or clicking a button, etc); or from a Variable method (defined in your plugins variable's file and called from a template). – Douglas McDonald Apr 8 '15 at 16:14
  • I would suggest you start by reading the plugin documentation and/or installing a plugin template like BusinessLogic to get you started. How much data are we talking about? It might be easier to do it manually. – Douglas McDonald Apr 8 '15 at 16:14
  • 341 entries with about 10 feature fields each. – Bryan Garrant Apr 8 '15 at 16:21
  • I could merge the 10 fields in the CSV using Excel, but how can I find the array format needed for a Matrix or Table import? – Bryan Garrant Apr 8 '15 at 16:26

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.