Jump to content

Image gone after $page->save()


simonsays
 Share

Recommended Posts

Hello everyone,

I have two methods inside a class

    public function savePageBasic() {
        $input = $this->wire('input');
        $page = $this->wire('page');
        $sanitizer = $this->wire('sanitizer');
        if ($input->post->submit && $this->canEdit()) {
            $p = new Page();
            $parent = $this->wire('pages')->get($page->path);
            $p->parent = $parent;
            $p->template = $sanitizer->text($this->input->post->section);
            $p->name = $sanitizer->pageName($input->post->title);
            $p->addStatus(Page::statusUnpublished);
            $p->title = $sanitizer->text($input->post->title);
            
            return $p;
        }
        return false;
    }    

public function submit() {
        $input = $this->wire('input');
        $page = $this->wire('page');
        $session = $this->wire('session');
        if ($input->post->submit && $this->canEdit()) {
            if (($p = $this->savePageBasic()) !== false) {
                $p->addStatus(Page::statusHidden);
                $this->setDummyData($p);
                $p->save();
            }
            $session->redirect($page->path);
        }
    }

    /**
     * Set dummy data to page fields
     */
    protected function setDummyData(&$page) {
        $dummyData = [
            'FieldtypeTextareaLanguage' => 'Start writing your <b>%s</b> here...<br/>Make sure text is input before you publish...',
            'FieldtypeImage' => 'https://dummyimage.com/1200x900/c2c2c2/2e2e2e.png&text=No+image+yet',
        ];
        foreach ($page->template->fields AS $field) {
            $type = (string)$field->type;
            if (isset($dummyData[$type])) {
                $value = sprintf($dummyData[$type], strtolower($field->label));
                $page->{$field->name} = $value;
                $page->save();
            }
        }
        return true;
    }

I have a page with textarea and single image and a method which sets some dummy data to them.

The problem is, that after running $page->save() inside submit() method, image disappears (text stays).

Commenting $page->save out of submit() method solves the issue, but this is wrong in my opinion as I might want to have some manipulations after dummy data is set.

I checked the output formatting and it stays false throughout the entire cycle.

What could be the problem?

Link to comment
Share on other sites

Hi. Just looked at your issue quickly, so you might be already doing the right thing. Note that you need to first save a new page before adding images to its image field. Save the page first, then add the image, then save again.

  • Like 1
Link to comment
Share on other sites

6 minutes ago, kongondo said:

Hi. Just looked at your issue quickly, so you might be already doing the right thing. Note that you need to first save a new page before adding images to its image field. Save the page first, then add the image, then save again.

Like I wrote, the image is saved as long as I do not run $page->save() after running setDummyData().

I do not understand, why it empties the image field.

Link to comment
Share on other sites

It's just like @kongondo said - you have to save a new page before you can add files or images to it. The directory needed to hold the files/images gets created on the first save - then you can add files/images.

So in submit(), do $p->save() before setDummyData()...

public function submit() {
    $input = $this->wire('input');
    $page = $this->wire('page');
    $session = $this->wire('session');
    if ($input->post->submit && $this->canEdit()) {
        if (($p = $this->savePageBasic()) !== false) {
            $p->addStatus(Page::statusHidden);
            $p->save();
            $this->setDummyData($p);
        }
        $session->redirect($page->path);
    }
}

 

P.S. This...

$parent = $this->wire('pages')->get($page->path);
$p->parent = $parent;

...could be better optimised as...

$p->parent = $page;

 

  • Like 2
Link to comment
Share on other sites

9 hours ago, Robin S said:

It's just like @kongondo said - you have to save a new page before you can add files or images to it. The directory needed to hold the files/images gets created on the first save - then you can add files/images.

So in submit(), do $p->save() before setDummyData()...


public function submit() {
    $input = $this->wire('input');
    $page = $this->wire('page');
    $session = $this->wire('session');
    if ($input->post->submit && $this->canEdit()) {
        if (($p = $this->savePageBasic()) !== false) {
            $p->addStatus(Page::statusHidden);
            $p->save();
            $this->setDummyData($p);
        }
        $session->redirect($page->path);
    }
}

 

P.S. This...


$parent = $this->wire('pages')->get($page->path);
$p->parent = $parent;

...could be better optimised as...


$p->parent = $page;

 

 

Like I said, I do not receive an error (know, that page must exists before files are attached), I simply get an empty image.

So, I added $page->save() to savePageBasic() (thanks for the optimization advice) to make sure page ID exists by the time image is added

    public function savePageBasic() {
        $input = $this->wire('input');
        $page = $this->wire('page');
        $sanitizer = $this->wire('sanitizer');
        if ($input->post->submit && $this->canEdit()) {
            $p = new Page();
            $parent = $page->path;
            $p->parent = $parent;
            $p->template = $sanitizer->text($this->input->post->section);
            $p->addStatus(Page::statusUnpublished);
            $p->title = $sanitizer->text($input->post->title);
            $p->save();
            return $p;
        }
        return false;
    }

The submit() method remained the same

    public function submit() {
        $input = $this->wire('input');
        $page = $this->wire('page');
        $session = $this->wire('session');
        if ($input->post->submit && $this->canEdit()) {
            if (($p = $this->savePageBasic()) !== false) {
                $p->addStatus(Page::statusHidden);
                $this->setDummyData($p);
                $p->save();
                $this->sc->flash->success('Page "' . $p->title . '" successfully saved');
            }
            $session->redirect($page->path);
        }
    }

$p->save() reset the image somehow (commenting it out solves the issue). But that is extremely wrong IMHO.

Link to comment
Share on other sites

    protected function copy() {
        $input = $this->wire('input');
        $id = $this->wire('sanitizer')->int($input->post->id);
        $original = ($id) ? $this->wire('pages')->get($id) : $this->wire('page');
        $redirect = $this->wire('page')->path;
        if ($this->canEdit() && $original) {
            $original->of(false);
            $copy = $this->wire('pages')->clone($original, null, false);
            $copy->title .= ' - Copy';
            $copy->save();
            if ($original->hasChildren()) {
                $children = $original->children('include=all');
                foreach ($children AS $child) {
                    $this->copyChild($child, $copy);
                }
            }
            $this->sc->flash->success('Page "' . $original->title . '" successfully copied as "' . $copy->title . '"');
            $templateTags = array_map('strtolower', explode(' ', $original->template->tags));
            if (!in_array('section', $templateTags)) {
                $redirect = $copy->path; 
            }
        }
        $this->wire('session')->redirect($redirect);
    }

Bump!

I was still unable to solve the problem.

I now have a copy method, which does not use setDummyData and has 100% same problem.

Every time a save is performed, image is reset.

When I comment out $copy->save(); the image is preserved. Does anyone know a workaround?

 

Edited by simonsays
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...