Jump to content

How to Iterate Through All Fields of a Repeater?


bytesource
 Share

Recommended Posts

Hi all,

I have been trying to get the fields of a given page, and that page can also contains repeaters - which have their own set of fields.

Retrieving all fields was relatively easy, but I am not sure how to iterate through the fields of a repeater. As the below code is meant to be expanded to more than just one page, I also don't know the names of the repeaters fields.

This is what I have come up with so far:

<?php

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

$languagePage = wire('pages')->get("/");
$pageFields   = $languagePage->fields;
$russian      = wire('languages')->get("ru");

// 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") {

        $languagePage->setLanguageValue($russian, $field->name, "some dummy text");

    } elseif ($type == "FieldtypeRepeater") {
        
        // How to I itereate through all fields of a repeater?
        foreach($field->fields as $subfield) {
            $type = $subfield->type;

            if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") {

                $languagePage->setLanguageValue($russian, $subfield->name, "some dummy text");

        }

    }

}

$languagePage->save();

The problem is the code inside the elseif statement.

I am aware that a repeater can contain other repeaters, which calls for a recursive approach, but before I tackle this problem I wanted to be sure everything else is working.

Any suggestions are highly welcome!

Cheers,

Stefan

Link to comment
Share on other sites

Hi Stefan,

The repeater field is PageArray holding pages. I'm not sure if a repeater can contain another repeater, if it's possible this wouldn't be a good idea in my opinion ;)

Extract your logic into a function that can be called recursive, this is just an example written in the browser, not tested:


function setLanguageValue(Page $p, $language) {
  
  $pageFields = $p->fields;
  
  // iterate through all fields of the given page
  foreach ($pageFields as $field) {
    $type = $field->type;
    // only select a field if it is translatable
    if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") {

        $p->setLanguageValue($language, $field->name, "some dummy text");

    } elseif ($type == "FieldtypeRepeater") {
        
        // Go recursive here
        $repeaterFieldName = $field->name;
        foreach ($p->$repeaterFieldName as $subpage) {
          setLanguageValue($subpage, $language);
        }

     }

  }
 
  $p->save();

}

// Execute it
$startPage = wire('pages')->get("/");
$russian = wire('languages')->get("ru");
setLanguageValue($startPage, $russian);

  • Like 2
Link to comment
Share on other sites

@Wanze

I just wanted to let you know that the code you posted is working as expected. Besides, it seems  FieldtypePageTable also is a PageArray containing pages, so I added this one, too:

<?php

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

echo "Translation starts now...";

function setLanguageValue (Page $p, $language) {

    $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") {

            // only translate if the field value for the default language is not empty
            $defaultValue = $p->getLanguageValue(wire('languages')->get('default'), $field);
            if (!empty($defaultValue)) {

                $p->setLanguageValue($language, $field->name, "some dummy text");
            }

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

    $p->save();
}

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

setLanguageValue($p, $russian);

Thanks again for your help!

Cheers,

Stefan

  • 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...