Jump to content

How to ID a Specific Image Using The API?


bytesource
 Share

Recommended Posts

I need to export all translatable fields (FieldtypeTextLanguage, FieldtypeTextareaLanguage), translate them, and then import the translated text. 
 
For normal page fields this is relatively easy. The same is true for repeaters, as a repeater is just a PageArray holding pages. So, in order to import the translated text into these fields, I just need the page id and field name for identification. 
 
However, as far as I know, FieldtypeImage is not represented as an array of pages (holding images). I still could iterate through all images to retrieve the image descriptions, but then how could I find the correct image later when I want to import the translated image description?
 
Does a single image carry some kind of ID that I could use to retrieve it via the API?
 
Just for completeness, here is a quick and dirty proof of concept of what I am trying to accomplish:
 
<?php

// bootstrap Processwire
include("./index.php");

function getURL(Page $p) {

$id = $p->id;
echo $id . ", " . $p->name;
echo "</br>";
}

function getLanguageValues (Page $p) {

$pageFields   = $p->fields;

// iterate through all fields of a page
foreach ($pageFields as $field) {
$type = $field->type;

// only select a field if it is translatable
if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") {

convertToCSV($p, $field); 
} 
elseif ($type == "FieldtypeRepeater" || $type == "FieldtypePageTable") {
        
        $fieldName = $field->name;
        // Repeater and and PageTable fields are PageArrays holding pages.
        foreach($p->$fieldName as $subpage) {
         getLanguageValues($subpage);
        } 
    }
    elseif ($type == "FieldtypeImage") {

     $fieldName = $field->name;
     foreach ($p->$fieldName as $img) {
     // ?
     }
    }
  }
}

function convertToCSV ($p, $field) {

$defaultLanguage = wire('languages')->get('default');
$defaultText     = $p->getLanguageValue($defaultLanguage, $field);

// only translate if the field value for the default language is not empty
if (!empty($defaultText)) {

$id = $p->id . "-" . $field->name;

echo $id . ", " . $defaultText;
echo "</br>";
}
}

function getValues ($p) {
getURL($p);
getLanguageValues($p);
}

// ===============================================================

$p       = wire('pages')->get("/");

$lang = wire('languages')->get("de");

getValues($p);

// Sample output [id (page id - field name), text]
// [...]
// 1-portfolio_slogan, Our Strengths
// 1012-subject, Advanced Drilling Equipment  // field belonging to a repeater
// [...]

// Simple test to check how re-importing translated values might work
// http://processwire.com/api/multi-language-support/multi-language-fields/#multi-language-field-values

// set 'subject' textfield value from repeater to target language
$targetPage = wire('pages')->get(1012);
$deutsch = "Bohrgeräte";

$targetPage->subject->setLanguageValue($lang, $deutsch);
$targetPage->save();

?>
 
Cheers, 
 
Stefan
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...