Jump to content

Image custom fields - set values with the API


totoff
 Share

Recommended Posts

Dear all,

I'm upgrading an older side with the new custom fields for images feature as of 3.0.142. My image field is set to "Automatic" and holds a bunch of images together with their respective description on each page. New custom fields include "caption" among others and to make my live easier I I'm trying to populate "caption" with the value from the (default) description field. But unfortunately I can't seem to find out how to save the newly set values. This is my code:

<?php foreach (page()->images as $image) {
	$image->set('caption', $image->description);
	bd($image->caption);
    echo files()->render("markup/views/view-card-image-fancybox.php", array('image'=>$image));
} ?>
<?php 
$page->save();
bd($page->save());
?>

This sets the value as intended (see screenshot) but doesn't save it permanently to the database. What am I doing wrong?

Thanks!

 

Bildschirmfoto 2021-02-22 um 15.33.38.png

Link to comment
Share on other sites

This is driving me nuts! I managed to set the value for a single image description field and saving it using page->save(); but no luck with values I set for the custom fields inside the foreach loop. They are not saved to the database. Im sure this has a super obvious reason but I'm lost finding it.

Any help is much appreciated.

Link to comment
Share on other sites

Hi elabx,

thanks, really appreciate your help. Unfortunately that doesn't work either. I've tried all kinds of variations including setting the image title as this is a mandatory field. This is where I currently am:

foreach (page()->images as $image) {
    page()->of(false);
    $image->title = $image->description;
	bd($image->title, 'title');
    $page->save('images');
}

But it doesn't work. The value for $image->title is assigned (as I can see with Tracy) but doesn't get stored to the database. ?

Link to comment
Share on other sites

I did this a few weeks ago and I got it to work.

I seem to remember I had a certain amount of trouble saving the record, but I can't remember what exactly. However, the following code worked (I've simplified getting the text to add to the image fields).

Note that I've done something a bit weird and saved both the field and then the page. This might have been an error (I was running a one-off process), or I might have found it was necessary for some reason I don't understand. It may just be that saving the field is what does the trick.

Also, for reasons of my own, I used $record as a page variable, but I don't see why this would matter. Otherwise, I can't see any difference between this and your code.

foreach($selectedRecords as $record) {
    if(count($record->article_images)) {
        $record->of(false);
        foreach($record->article_images as $image) {

            $image->photo_caption = "Some caption text";
            $image->photo_credit = "Some credit text";

			// First save
            $record->save('article_images');
        }

		// Second save!
		$record->save();
    }
}

Hope this helps, and I'd be interested if you find this works and can figure out what actually fixes the problem.

  • Like 1
Link to comment
Share on other sites

@BillH Man, you saved my live. I had a nightmare last night, seeing myself migrating dozens of entries manually but your code came to the rescue. It works! Here is my code:

page()->of(false);
foreach (page()->images as $image) {
    $image->artist_name = $page->title;
    if($image->description):
    $image->title = $image->description;
    $image->image_caption = $image->description;
    endif;
    // first save inside loop saves the field value
    $page->save('images');
}
// saves the page
$page->save();

Yesterday I had the output formatting statement inside the loop and I assume this was the mistake. Thanks for helping me!

  • Like 3
Link to comment
Share on other sites

Pleased it worked ?

Whether you set output formatting inside or outside the loop shouldn't affect the issue – though inside would waste a few processor cycles!

I've done some tests, and the issue is a result of page->save() not working for custom image fields.

  • Saving the image field (inside or outside the loop) is necessary to save the content of the image custom fields.
  • Saving the page (inside or outside the loop) does nothing for the image custom fields.

It seems to me that saving the page should work as it does with other fields, and I have reported this as an issue https://github.com/processwire/processwire-issues/issues/1334

However, there may be a good reason for it behaving as it does, and it has been pointed out on the issue page that the Table Profield behaves in the same way.

  • Like 2
Link to comment
Share on other sites

36 minutes ago, BillH said:

I've done some tests, and the issue is a result of page->save() not working for custom image fields.

That's good news for me, felt like a complete noob … Thanks for letting me know.

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

×
×
  • Create New...