Jump to content

Repeater field not initialized


simonsays
 Share

Recommended Posts

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

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

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