Jump to content

Lost in Pageimages or How to add a file to an existing Imageinputfield


felixthecat
 Share

Recommended Posts

Hello,

What is the correct way to add an manually created file to an existing imagefield. The new created file is in the same Folder where the page stores the files.

Here what i tried. Runs without errors but doesnt add the file to the imagefield:
 

// image_ok.jpg is the file which is stored in the imagefield
// image_new.jpg is the manually created file and should be added to the imagefield of image_ok.jpg

$pagefiles = $imageOK->pagefiles;
$pagefiles->add('image_new.jpg' );
$page = $imageOK->page;
$page->of(false);
$page->save($pagefiles->name);

What is wrong?

Thanks.

Link to comment
Share on other sites

If you don't already know the page and field name that $imageOK belongs to and have to work it out from the Pageimage then I suggest:

$p = $imageOK->page;
$field_name = $imageOK->field->name;
$p->of(false);
$p->$field_name->add('/full/disk/path/to/image_new.jpg');
$p->save($field_name);

 

  • Like 3
Link to comment
Share on other sites

42 minutes ago, felixthecat said:

Thank you... that is working ?
But what is the relation between pagimages and the field?

Here is the description of Pageimages class

ProcessWire Pageimages

Pageimages are a collection of Pageimage objects.

Typically a Pageimages object will be associated with a specific field attached to a Page. 
There may be multiple instances of Pageimages attached to a given Page (depending on what fields are in it's fieldgroup).

https://processwire.com/apigen/class-Pageimages.html

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