Jump to content

Recommended Posts

Posted

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?

Posted

Is there any other place in the code where you actually create the repeater items themselves? Maybe that's missing. 

Posted
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?

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
  • Recently Browsing   0 members

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