simonsays Posted September 11, 2018 Share Posted September 11, 2018 Hello everyone, I am stuck and would really appreciate some assistance. I am trying to write a recursive method which populates newly created pages with default dummy data. /** * Set dummy data to page fields * * @param type $page * @return $this */ protected function setDummyData(&$page) { $language = $this->user->language; $this->user->language = $this->wire('languages')->get('default'); $dummyDataType = $this->getDefaultDummyData(); $page->of(false); foreach ($page->template->fields AS $field) { $type = (string) $field->type; if (isset($dummyDataType[$type])) { $value = sprintf($dummyDataType[$type], strtolower($field->label)); $page->{$field->name} = $value; } elseif ($field->type instanceof FieldtypeOptions) { $page->{$field->name} = $field->defaultValue; } elseif ($field->type instanceof FieldtypeRepeater) { $page->save(); foreach ($page->{$field->name} as &$repeater) { $this->setDummyData($repeater); } } } $page->save(); $this->user->language = $language; return $this; } /** * Fetch default dummy data by type * * return array */ protected function getDefaultDummyData() { $dummyDataType = [ 'FieldtypeTextareaLanguage' => 'Start writing your <b>%s</b> here...<br/>Make sure text is input before you publish...', 'FieldtypeImage' => $this->wire('config')->paths->root . 'site/migrations/dummy/default.png', ]; return $dummyDataType; } Today I tried extending it to work with repeater fields and simply nothing happens. It detects the $field->type as Repeater, but when I try to loop through all repeaters within this field, it shows its count as 0. Anyone has an idea on what might be wrong? Should repeater item be initialized in a different way? Link to comment Share on other sites More sharing options...
elabx Posted September 11, 2018 Share Posted September 11, 2018 Is there any other place in the code where you actually create the repeater items themselves? Maybe that's missing. Link to comment Share on other sites More sharing options...
adrian Posted September 11, 2018 Share Posted September 11, 2018 Not really an answer to your question, but perhaps this module does what you need already: It doesn't currently support repeater fields, but might be worth using this as a base and extending it to support repeaters. There are lots of other posts that describe how to populate repeaters. https://www.google.com/search?q=site%3Aprocesswire.com%2Ftalk%2F repeaters api Link to comment Share on other sites More sharing options...
simonsays Posted September 12, 2018 Author Share Posted September 12, 2018 15 hours ago, elabx said: Is there any other place in the code where you actually create the repeater items themselves? Maybe that's missing. Nope. Are repeater items created in a different way? Is there a good example? Link to comment Share on other sites More sharing options...
elabx Posted September 12, 2018 Share Posted September 12, 2018 I would try using this function: https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module#L432 Repeaters items are actual pages that have to be created, hence when you are iterating the field in your code counts 0 repeaters, because no repeaters exist! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now