Jump to content
totoff

Image custom fields - set values with the API

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

Share this post


Link to post
Share on other sites

Try setting the page's output formatting to false before saving. 

page()->of(false);

 

Share this post


Link to post
Share on other sites
59 minutes ago, elabx said:

Try setting the page's output formatting to false before saving. 


page()->of(false);

 

Thank you, unfortunately that doesn't work either. Same problem.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Maybe try setting caption directly without the set method:

page()->image->caption = $description

I'm thinking set method is trying to act on the Pageimage object properties, and not be affecting the image fields. 

Share this post


Link to post
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. 😞

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By killedfriendz
      I am very sorry for asking this but i totally do not understand how to set values of checbox using API. 
      I have checbox field on my page with name "order_status". 
      So i've tried few ways to make it checked but it still doesn't work:
       
      $userPage->order_status->value = 1; $userPage->order_status->add(1); $userPage->order_status->add(true); Could you please tell me how to do it?
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
    • By totoff
      Dear all,
      tomorrow I'll have to introduce some editors on how to upload and edit images in image fields in Processwire. The site in question is quite image rich and I've spent some time to serve the appropriate images via scrcset already cropped for different screen sizes.
      However, the backend offers a lot of possibilities to manipulate images and most of them I'm not familiar with because as a developer I've never spent much thinking on what all the features might be good for … Yet I'm concerned that my editors work on images in the backend may interfere with my optimization strategies in the templates.
      Is there a general rule of thumb what content editors should do or not do with images in order not to break srcset strategies etc.? Maybe there is already a source of information you could point me to?
      Your recommendations are much appreciated!
    • By dalibor
      I'm using hook for retrieving data from image metadata (IPTC) after image upload and filling them to some fields. When I choose server-side image resize, everything works well. But when I use client-side resize (which si much faster and client comfortable  - it uses PWImageResizer.js) image metadata are lost during resizing - I have downloaded the file after resize and checked for EXIF, IPTC etc. and they are not present.  Using PW 3.0.165. Tried to look into PWImageResizer.js but uhh it's too javascreepy to me:) 
    • By SwimToWin
      As an admin I want to use the API to ask if a page is published - using the $page->isPublished() method - so that I know it's published - as opposed to unpublished or trashed pages. That includes hidden pages.
      This method will correspond to Settings -> Status when editing pages:

      (Published is also mentioned explicitly where the edit page says "Published on [?]".)
      I would expect the API - and specifically the $page->hasStatus() method - to ask if a page has status published.
      But as I can see, it's only possible to ask for exceptions such as isUnpublished() and isHidden().
      <?php // This fails with "Fatal Error: Uncaught Error: Undefined class constant 'statusPublished'" if($page->hasStatus(Page::statusPublished)) { echo 1; } ?> PS: My current use case is that I want to count number of published vs. unpublished pages. I can only do that by getting all pages (include=all), then subtract any unpublished pages.
×
×
  • Create New...