Jump to content
a-ok

addHookAfter and image drag/drop on repeater causing issues

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?

Share this post


Link to post
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
}

 

Share this post


Link to post
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.

Share this post


Link to post
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.

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

This is great, Robin.  I appreciate you taking the time to explain this to me... crazy I didn't know about it.

See attached. A real big help!

Screen Shot 2016-06-24 at 19.29.52.png

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By celfred
      Hello,
      I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :
        bd('outside');   $wire->addHookAfter('Page::render', function($event) {     bd('inside');   }); And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !
      EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
    • By tarkvsg
      Hi!
      tell me, pls, how to execute this equality (from ready.php)
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {     if($event->object->hasField == 'FIELDNAME')     {         .....     } } - $hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)
      the left side equation is the object entity, and the right side - the string 
      How it is?
    • By horst
      Hey,
      I want to hook into the 404 handling of PW at the most earliest stage. Pleas can anybody point me to the hook or file that suits this best?
    • By Sanyaissues
      Hi, I have a page with a repeater called teams with two fields: team (a pageReference with a list of teams) and people (a pageReference with a list of people who likes this team).
      I'm trying to populate the people field in the repeater according to the value of the team field in the same repeater. So far. I'm able to get the pageArray that I want to assign to the people field, but i don't know how to "save" the value for each instance of the repeater.
      I hope somebody can give me a light. Thanks in advance.
       
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'people') { $repeaterField = $event->arguments('page')->teams; foreach ($repeaterField as $t) { // Is this the way to loop the instances of the repeater? $team = $t->team->id; $t->people = $event->pages->find("template=user, team={$team}"); var_dump($t->people); // This returns the values that i want to assign for each repeate instance $event->return = $t->people; // I hope this assign a custom pageArray for each repeater, but it assigns the same for all the instances of the repeater } } });  
×
×
  • Create New...