cb2004 Posted September 25, 2017 Posted September 25, 2017 So I have my image field, and I also have my image descriptions in 1 text field seperated by |, these are in the correct order. I am trying to apply the images descriptions to the images but I am having a brain fart. My start is: foreach ($pages->get(3780)->find("imagesMain.count>0") as $p) { $p->setOutputFormatting(false); $imagesCount = count($p->imagesMain); $descriptions = explode("|", $p->drupalAlt); $descriptionsFiltered = array_filter($descriptions); if($imagesCount === count($descriptionsFiltered)) { } $p->save(); $p->setOutputFormatting(true); } In some places the images are not present yet but the descriptions are, hence the if($imagesCount === count($descriptionsFiltered)) check. So, in there I want to foreach on the imagesMain and apply the descriptions in order. Cheers for any help.
BitPoet Posted September 25, 2017 Posted September 25, 2017 Something like for($i = 0; $i < $imagesCount; $i++) { $img = $p->imagesMain->eq($i); $descr = $descriptionsFiltered[$i]; $img->description = $descr; } ? 1
cb2004 Posted September 25, 2017 Author Posted September 25, 2017 Perfect. Full code: foreach ($pages->get(3780)->find("imagesMain!=, drupalAlt!=") as $p) { $p->setOutputFormatting(false); $imagesCount = count($p->imagesMain); $descriptions = array_filter(explode("|", $p->drupalAlt)); if($imagesCount === count($descriptions)) { for($i = 0; $i < $imagesCount; $i++) { $p->imagesMain->eq($i)->description = $descriptions[$i]; } } $p->save(); $p->setOutputFormatting(true); }
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