Jump to content

Add image field image to first row of repeater


a-ok
 Share

Recommended Posts

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

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

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

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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...