Jump to content

addHookAfter and image drag/drop on repeater causing issues


a-ok
 Share

Recommended Posts

Folks,

I may have discovered a) a bug or b) that I can't write code for shit.

I have this hook setup:

$pages->addHookAfter('Pages::saveReady', function($event) {
	$page = $event->arguments(0);
	$repeater = $page->collections_detail_images;
	foreach ($repeater as $row) {
		$row->of(false);
	 	$row->collections_detail_image_page_title = $page->title;
	 	$row->collections_detail_image_page_type = $page->collections_detail_category->name;
	 	$row->collections_detail_image_page_pattern = $page->collections_detail_pattern->name;
	 	$row->collections_detail_image_page_designer = $page->collections_detail_designer->name;
		$row->setAndSave();
	}
});

For each repeater row on a page I am editing, upon save, it fills in the desired fields with content required. This all works well, however, the issue/bug is this...

Each repeater row also has an image field (limited to 1), which I have been dragging and dropping an image to, for each row in the repeater, then saving. I tend to add 5-10+ rows (each with an image) at once then save. This is where the issue occurs. Even though it looks like the image is there and saved... it's not appearing on the front-end. If I do one row at a time and save, it works fine, it's only if I do 2 or more in one 'save session'. This only happens with drag and drop too.

If I remove my hook, shown above, I don't seem to get any issue.

Any thoughts?

Link to comment
Share on other sites

This isn't a direct answer to your issue, but do you actually need this hook?

It looks like you want each repeater item to store some values from the page the repeater field is on (the "for-page"), but this seems like unnecessary duplication. Can't you just get the values from the for-page once and output it wherever needed? If you want the value from inside a loop that is outputting repeater items you can use getForPage(), but chances are you already have the for-page in another variable (e.g. $page).

So in your template:

<?php
$type = $page->collections_detail_category->name; // similarly for other values
foreach($page->collections_detail_images as $collections_detail_image) {
	// do what you want with $type, and any other repeater item fields
}

 

Link to comment
Share on other sites

Thanks for the reply, Robin.

The reason I need to use this code is I am using a pageField for the repeaters but you cannot output the title of the page the repeater is on within the Custom format title label. This isn't front end but backend.

Link to comment
Share on other sites

Ah, I see.

If these values are being stored only for the purposes of displaying in the Page inputfield you could combine them into a single field in the repeater.

Or potentially avoid the need for this hook by using the Inputfield Selectize module:

Quote

Your select options can use any field or subfield on the page, but also sub-subfields, or any data you provide, since you are not limited by tag replacement: you control the precise data supplied to the options using a PHP array that returns data to the module, which is in turn supplied in JSON to the select as a data-data attribute.

 

Link to comment
Share on other sites

Thanks, Robin. Difficulty is that they are used separately. The pageField, where I select a repeater, doesn't return which 'getForPage()' it is on... that is why I need to include a hidden input field, then using that hook it would insert the title of the page into the repeater and thus I can use it for the custom format title label on the pageField.

You see my dilemma.

Link to comment
Share on other sites

See the screenshots below for a demo of how the Inputfield Selectize module can be used with getForPage()...

Page with repeater field
page-with-repeater.png

 

Page field using Inputfield Selectize
inputfield-selectize.png

 

Page field options (note: I'm recycling an existing 'headline' field as the attribute in the repeater)
page-field-options.png

 

Edit: I'm a big fan of the new Inputfield Selectize, but if you want to use one of the other Page inputfields then have a read of this post - you'll see how the code could be adapted to use getForPage().

  • Like 3
Link to comment
Share on other sites

cool that you found the selectize inputfield.. hope it works well and let me know if you run into any issues or requests.

In combination with the custom PHP option it opens up to almost anything.. hope to be releasing an ajax version sometime in future..

  • Like 2
Link to comment
Share on other sites

13 minutes ago, Macrura said:

cool that you found the selectize inputfield.. hope it works well and let me know if you run into any issues or requests.

In combination with the custom PHP option it opens up to almost anything.. hope to be releasing an ajax version sometime in future..

Thanks so much, Macrura. I didn't know you did it. Really great work... just what PW needed tbh.

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

×
×
  • Create New...