a-ok Posted April 1, 2018 Share Posted April 1, 2018 Hi folks, I'm wanting to take an Image field and add the same image as the first row of a RepeaterMatrix field. If RepeaterField field rows already exist, it should shift it to the first row, and if the image has already been added then it shouldn't re-add it. I guess I could double check the image URL on this to prevent this? Anyway, I thought this would work... it's adding in a RepeaterMatrix row of the requested type but the image isn't being added. function eSubmissionImageToRepeater($event) { $page = $event->arguments(0); if ($page->template->name == 'events-detail') { // What's On detail $flexibleContentRepeater = $page->flexible_content->getNew(); $flexibleContentRepeater->save(); $flexibleContentRepeater->repeater_matrix_type = 2; $flexibleContentRepeater->save(); $flexibleContentRepeater->flexible_content_images->add($page->featured_image->httpUrl); $flexibleContentRepeater->flexible_content_images->save(); $flexibleContentRepeater->save(); $page->save(); } } $wire->addHookAfter("Pages::save", null, "eSubmissionImageToRepeater"); Any help? Link to comment Share on other sites More sharing options...
elabx Posted April 2, 2018 Share Posted April 2, 2018 Try setting the output formatting for the $flexibleContentRepeater repeater matrix item (which is a page!) $flexibleContentRepeater->of(false); I also think you shouldn't be saving the page inside the hook: Link to comment Share on other sites More sharing options...
a-ok Posted April 2, 2018 Author Share Posted April 2, 2018 7 hours ago, elabx said: Try setting the output formatting for the $flexibleContentRepeater repeater matrix item (which is a page!) $flexibleContentRepeater->of(false); I also think you shouldn't be saving the page inside the hook: Thanks! I think you're right re saving the page... I've removed that. The output formatting addition hasn't solved this though; still a blank image field. Hmm... Link to comment Share on other sites More sharing options...
a-ok Posted April 2, 2018 Author Share Posted April 2, 2018 Anyone got any other ideas? This seems to work: $flexibleContentRepeater->flexible_content_images->add("https://i.stack.imgur.com/bn9pu.png"); But this doesn't (bd'ing $page->featured_image->httpUrl returns the correct full URL...) $flexibleContentRepeater->flexible_content_images->add($page->featured_image->httpUrl); EDIT I think it's due to the ->op(false) which makes the featured_image field an array and thus this works... $featuredImage = $page->featured_image->first(); Link to comment Share on other sites More sharing options...
a-ok Posted April 3, 2018 Author Share Posted April 3, 2018 I managed to work this out by doing the following... might be useful for others. function eSubmissionImageToRepeater($event) { $page = $event->arguments(0); if ($page->template->name == 'events-detail') { // What's On detail if (count($page->flexible_content) <= 1 && $page->flexible_content->first()->type == 'text') { $featuredImage = $page->featured_image->first(); $flexibleContentRepeater = $page->flexible_content->getNew(); $flexibleContentRepeater->of(false); $flexibleContentRepeater->save(); $flexibleContentRepeater->repeater_matrix_type = 2; // Image(s) $flexibleContentRepeater->save(); $flexibleContentRepeater->flexible_content_images->add($featuredImage->httpUrl); $flexibleContentRepeater->save(); $flexibleContentRepeaterID = $flexibleContentRepeater->parent_id; $firstItem = $event->pages->find("parent_id=$flexibleContentRepeaterID")->first(); $lastItem = $event->pages->find("parent_id=$flexibleContentRepeaterID")->last(); $event->pages->insertBefore($lastItem, $firstItem); } } } if ($user->hasRole('e-submission')) { // Ony required for 'e-submissions' $wire->addHookAfter("Pages::save", null, "eSubmissionImageToRepeater"); } 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