Jump to content
Markus (Blue Tomato)

SOLVED: Set value to custom image field via API

Recommended Posts

I'am working on a migration script from the ImageExtra Module to the new Custom Image Fields feature in ProcessWire.

I have some problems while setting a field value to the new custom image field. I have set up the custom field and it works correct in the admin panel but not via the API:

$page->images->my_custom_image_field = "test";
$page->save();

Results into:

Fatal error: Uncaught ProcessWire\WireException: Item 'my_custom_image_field' set to ProcessWire\Pageimages is not an allowed type in /var/www/html/wire/core/WireArray.php:458

 

 

Share this post


Link to post
Share on other sites

I found this new "setFieldValue" function in the pw dev commits: https://github.com/processwire/processwire/commit/573048abb4a6bdec77aee2cbff7d5837de857a05#diff-b4e6cafb51396ac6ada1a538da0ddc8cR639

I tried following:

if($language->id == $languages->getDefault()->id) {
	$page->images->setFieldValue("my_custom_image_field", "test default language");
} else {
	$page->images->setFieldValue("my_custom_image_field__{$language->id}", "test language {$language->name}");
}

I get no error, but all fields are still empty 😞

I will continue my investigations... Maybe @ryan has an idea?

Also strange: the function should return true or false but if I dump the return of setFieldValue, I get only an empty string back.

Share this post


Link to post
Share on other sites

Maybe not related but in the past I always stumbled across two things:

  • code in templates needed a $page->save whenever I changed something in an image field while in a module/hook it wasn't necessary or caused strange errors
  • another thing was that I always had to foreach() through the image field

I try to find an old module I built... images were a huge deal in it.

  • Like 1

Share this post


Link to post
Share on other sites

From what I understand, you have to have the specific image prior to setting any property.

$yourPage->of(false); // turn off output formatting
$yourImage = $yourPage->yourImageField->last(); // get the last image
// $yourImage = $yourPage->yourImageField->eq($index); // or, get specific image by array index
$yourImage->yourCustomField = 'yourValue'; // Set value of custom field
$yourPage->save(); // save the changes

 

  • Like 2

Share this post


Link to post
Share on other sites

Oooh - Thanks that was my problem: I have set the value on the image field and not on the image file!

This works:

if($language->id == $languages->getDefault()->id) {
	$page->images->findOne("basename=xyz.jpg")->set("my_custom_image_field", "test default language");
} else {
	$page->images->findOne("basename=xyz.jpg")->set("my_custom_image_field__{$language->id}", "test language {$language->name}");
}

 

Share this post


Link to post
Share on other sites

Please set the topic to Solved so that others aren't anticipating troubleshooting. You can edit your original post to modify the topic title. Thanks!

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 Peter Troeger
      Hello 🙂
      I have set up pagination on a mulitlanguage site.
      I've done this before, but this time I have a problem I can't solve.
      Pagination is activated on 'parent-template' and 'child-template' just to be sure 🙂
      This is my code:
      $children = $page->children('limit=1'); foreach($children as $child) { $title = $child->title; echo $title; } echo $children->renderPager(); The navigation is output correctly and the link look correct as well 'parent-page/page2/'.
      But when I click the link, the site seems to redirect back to 'parent-page/
      Any help would be greatly appreciated 🙂
      - Peter
    • By Jens Martsch - dotnetic
      Hi guys I need an estimate for the following task:
      I developed a job candidate application in ProcessWire and need an extension to a Lister Pro Page.
      My customer wants to display a summary table of the filtered data like in the attached screenshot.
      When you filtered the view, that summary should show how many candidates have which status according to the actual filtered view.
      If you change the filter, the summary table has to be updated also.
      Who can accomplish this task and what would it cost?
      I first need an estimate to tell it to my customer. If he says the price is ok I will provide you with FTP Data to a dev server version of  that tool.
      Thank you in advance.

    • By LuisM
      Hi there,
      im a bit lost right now. I added an hook after templates::save which should get the affected template and write its template ID into a JSON.
      The problem is, the event->object gives me an instance of the templates class. How can I narrow this down to the saved Template and get its ID and fieldgroup?
    • By Clément Lambelet
      Hey!
      I've been working on a Processwire installation (3.0.123) for a few days now and I must have made a big mistake this morning because the links in the admin's main menu no longer appear.
      This is not related to the admin theme, because the bug occurs with all themes (Default, Reno, Uikit).
      I tried to reinstall with the dev version (3.0.136), but the problem is still there. I also uninstalled all the modules I had added, without success. There is no error in the js console. 
      I still can access/view/edit the pages  by going through the admin/page list.
      Thanks in advance for your help!


    • By lecrackffm
      Hey all,
      i do, most probably have a problem with file encoding and / or BOM. 
      My <head>  content is getting moved into <body> and i can inspect a couple of &#65279;  ( ZERO WIDTH NO-BREAK SPACE) chars with dev tools.
      I checked all template files with phpstorm, they are all in utf-8 w/o BOM.
      The current installation is based on a profile i exported from another project where i had not such problems.
      However, in the new project i did change the default language to german and installed english as the second language. 
      Might there be any conflicts with the language support modules?
      Hints, tips or solutions are highly appreciated 🙂
×
×
  • Create New...