Jump to content

How to import multi-language content using the ProcessWire API


Recommended Posts

I have a database where most of the content has versions in two languages. Suppose I want to import that content into a ProcessWire instance where I have configured mult-language support for those languages. How could I write an import script that iterates over each article, and pairs it with it's partner so that they both appear together in the article's language tabs?

Here's what I'm thinking so far:

// Get the data from the old database
$articles = database()->query("
    SELECT *
    FROM textpattern
    WHERE section='" . IMPORT_SECTION . "'
    // Join to l10n_articles
    ORDER BY posted desc
");

// Create each article in ProcessWire
foreach($articles as $article) {

    if ($article['Status'] == 4) {

        $newArticle = new Page();
        $newArticle->template = 'article';
        $newArticle->parent = pages()->get("/" . IMPORT_SECTION);
        $newArticle->name = $article['url_title'];

        // Import each field into the appropriate ProcessWire field here.
        // I understand how to do this already.
        
        // Determine this article's language from the l10n_articles table here.
        // Determine the ID of the article's other-language partner here.
        // This should be pretty straightforward.

        // TBD: Insert this content into the appropriate language tab in ProcessWire here.
        // I have no idea how to do this.
        
        $newArticle->save();

    }

}

I imagine the API for this works according to principles I already understand, but I don't know where to look.

Thanks in advance for any guidance you can offer! Also, any notes about expected obstacles and challenges would be great.

Has anyone here solved this problem before?

Link to comment
Share on other sites

  • johnstephens changed the title to How to import multi-language content using the ProcessWire API
<?php namespace ProcessWire;

// get your languages
$default = $languages->get("default"); // retrive default (german)
$english = $languages->get("english"); // retrive english

// imported pages with content in different fields
// needed to move content in the correct fields there
$importedPages = $pages->findMany("template=product, parent=4372, fkImportId=import44242");

foreach ($importedPages as $importedPage) {
    $importedPage->of(false); // outputFormatting must be OFF

    // title field
    $importedPage->title->setLanguageValue($default, $importedPage->fkTitleDe); // set in default
    $importedPage->title->setLanguageValue($english, $importedPage->fkTitleEn); // set in english
	
  	// textarea field
    $importedPage->textarea->setLanguageValue($default, $importedPage->fkDescShortDe); // set in default
    $importedPage->textarea->setLanguageValue($english, $importedPage->fkDescShortEn); // set in english

    $importedPage->save(); // save the page with the new language values
}

Had a similar task a while back with content already stored in pages but in totally wrong fields (on purpose) - as we imported from CSV and moved content into their correct places later on which was way easier and faster with ImportPagesCSV and this little snippet here.

Hope this helps a bit.

Link to comment
Share on other sites

Another thing I see here... as you are moving content from Textpattern (I loved to use it!)... you might want to double check that you don't import the textile version but rather the HTML version of each page/article. Otherwise it might end looking funny.

Another thing that might cause a headache could be image management. The last time I used Textpattern images were placed just by referencing them by ID. At least that's what I did back then. In this case you could (if possible) import those nowadays with Import External Images which looks up full URLs to an image and imports them.

Link to comment
Share on other sites

Thank you, @wbmnfktr! This looks doable and helped me estimate the work involved.

Thanks for the Textpattern import tips, too! I became intimately familiar with Textpattern's database scheme before writing my first TXP content importer for anther ProcessWire site migration, and I remembered to import from the body_html field then. 🙂 But I did not know about the "Import External Images" plugin, and wound up writing my own image import scripts, which got a little hairy.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...