hellerdruck Posted March 25, 2021 Share Posted March 25, 2021 Hi all I'm working on a website for a real estate company using a CRM Software called "Propstack". All the Objects are created in Propstack and when ready, the customer can send an XML-File (OpenImmo Format) to the server of the website - one per language (English and German). I was able to create pages out of every estate object apart from the images. I can't make it work. Here's the important part of the XML-File (pfad would just be the image link): <openimmo> <uebertragung art="OFFLINE" umfang="TEIL" modus="CHANGE" version="1.2.7" sendersoftware="PROPSTACK" senderversion="1.0"/> <anbieter> <immobilie> <anhaenge> <anhang location="REMOTE" gruppe="TITELBILD"> <anhangtitel>Aussenansicht</anhangtitel> <format>PNG</format> <daten> <pfad>https://image-link.com/image.png</pfad> </daten> </anhang> <anhang location="REMOTE" gruppe="GRUNDRISS"> <anhangtitel>Grundriss 1.2</anhangtitel> <format>PNG</format> <daten> <pfad>https://image-link.com/image.png</pfad> </daten> </anhang> </anhaenge> </immobilie> </anbieter> </openimmo> There will always be one image with the tag gruppe="TITELBILD" that should be inserted to the field object_header_img. All other images should go to the gallery field object_images. I was able to create the pages and transmit all textfields to the right fields in Processwire then save it, but it would not save the image. Here's what I've done: $object = new Page(); $object->template = 'object'; $object->parent = wire('pages')->get('/objekte/'); $object->name = 'objekt-' . $object_id; $object->object_id = $object_id; $object->title = $xml->anbieter->...; // this works $object->save(); // this part doesn't work foreach ($xml->anbieter->immobilie->anhaenge->anhang->attributes() as $attribute) { if ($attribute == 'TITELBILD') { $object->object_header_img = $xml->anbieter->immobilie->anhaenge->anhang->daten->pfad; } else if ($attribute != 'TITELBILD') { $object->object_images->add($xml->anbieter->immobilie->anhaenge->anhang->daten->pfad); } } $object->save(); Can anyone help with this, please? Also, I would need to know, how to target the fields for a certain language. Propstack will deliver 1 xml file per language, but I would want to have one object in Processwire with both languages. Any ideas? Thank you very much Roli Link to comment Share on other sites More sharing options...
horst Posted March 25, 2021 Share Posted March 25, 2021 I think you simply can use one code line to add a file to a pagefile(s) or pageimage(s) field. When output formatting is off, what it must be when modifying a page via API, there is no difference between singular and multiple file fields. They are always multiple. The difference for a singular field only exists for output! So following your simplified code should work. Can you test this please and report back? foreach ($xml->anbieter->immobilie->anhaenge->anhang->attributes() as $attribute) { $object->object_images->add($xml->anbieter->immobilie->anhaenge->anhang->daten->pfad); } $object->save(); Link to comment Share on other sites More sharing options...
hellerdruck Posted March 25, 2021 Author Share Posted March 25, 2021 Hi @horst Unfortunately, it doesn't work. As mentioned above, I have one field, where the header image should be added (object_header_image) and the other field for the rest of the images, they go to the gallery. The only difference between these images is the xml attribute ("Titelbild" or "Grundriss" or whatever). I thought, that's how I have to target them. I just don't get it to work. Is there a setting that I haven't done on the image field itself maybe? Link to comment Share on other sites More sharing options...
horst Posted March 25, 2021 Share Posted March 25, 2021 17 minutes ago, hellerdruck said: Hi @horst Unfortunately, it doesn't work. As mentioned above, I have one field, where the header image should be added (object_header_image) and the other field for the rest of the images, they go to the gallery. The only difference between these images is the xml attribute ("Titelbild" or "Grundriss" or whatever). I thought, that's how I have to target them. I just don't get it to work. Is there a setting that I haven't done on the image field itself maybe? Oh sorry, I haven't read thoroughly. Please do a var_dump check what the "$xml->anbieter->immobilie->anhaenge->anhang->daten->pfad" contains. It looks a bit suspicious, as it is the same in all image cases. There seems to be a missing differenciation in the node "anhang". Maybe you have to loop over the anhaenge as anhang? foreach ($xml->anbieter->immobilie->anhaenge as $anhang) { // check conditions ... (attributes of $anhang) ... // when found the header image url: $object->object_header_img->add($anhang->daten->pfad); } Link to comment Share on other sites More sharing options...
3fingers Posted March 25, 2021 Share Posted March 25, 2021 You should also remember to: // Set output formatting off $object->of(false); // add images here with your conditions // Set output formatting to true back again $object->of(true); // at the end save your page $object->save(); Link to comment Share on other sites More sharing options...
hellerdruck Posted March 25, 2021 Author Share Posted March 25, 2021 Hi @3fingers, thank you! @horst, can you help me with the attributes part? I tried this now (not working): foreach ($xml->anbieter->immobilie->anhaenge->anhang as $anhang) { $attr = $anhang->attributes(); foreach ($anhang as $key => $image) { if ($attr == 'TITELBILD') { $object->object_header_img = $image->daten->pfad; } else { $object->object_images->add($image->daten->pfad); } } } Link to comment Share on other sites More sharing options...
3fingers Posted March 25, 2021 Share Posted March 25, 2021 $images = $xml->anbieter->immobilie->anhaenge->anhang->daten->pfad; foreach ($images->attributes() as $attribute) { $object->of(false); if($attribute != '') { if ($attribute == 'TITELBILD') { $object->object_header_img->add($images); } else { $object->object_images->add($images); } } $object->of(true); $object->save(); Do this work? Link to comment Share on other sites More sharing options...
BitPoet Posted March 25, 2021 Share Posted March 25, 2021 Are we talking SimpleXml here? In that case: <?php foreach ($xml->anbieter->immobilie->anhaenge->anhang as $anhang) { if ($anhang['gruppe'] == 'TITELBILD') { $object->object_header_img = $anhang->daten->pfad; } else { $object->object_images->add($anhang->daten->pfad); } } No reason to loop over the attributes. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now