Jump to content
ro-bo

Change Repeater Field by API

Recommended Posts

Hi there!
I am unfortunately unable to change (empty) the entry of a repeater field via a hook in ready.php.

wire()->addHookAfter("Pages::saveReady", function (HookEvent $event) {

   $page = $event->arguments("page");

   if ( $page->hasField('my_repeater') ) {

      $repeater = $page->my_repeater;
    
      foreach ( $repeater as $item ) {
        
         $dateStart = $item->date_start;
         $dateEnd = $item->date_end;

         if ( $dateStart && $dateEnd && $dateStart >=  $dateEnd ) {
			
			// $page->of(true); // that doesn't help either

            $dateEnd = ''; // !!!! that dosen't work !!!!

            throw new WireException('start date must be earlier than end date');

         }//endif

      }//endforeach

   }//endif

});

What am I doing wrong?
I've tried so much, read the documentation, searched the forum and google, but I can't find a solution.

Please help me 🙂
Robert

Share this post


Link to post
Share on other sites

What does not work? Do you get any error? 

If you want to save the "date_end" field: 
Did you try to save the repeater item (also with output formating set to false)? 

Share this post


Link to post
Share on other sites

Hello androbey!
Thanks for your answer.

Unfortunately nothing happens. The only error message is my own WireException. The goal is that the field (represented by the variable $dateEnd) with the wrong input should be empty after saving the page in PW.

I've already tried that:

...
$repeater = $page->my_repeater;
...
if ( $dateStart && $dateEnd && $dateStart >=  $dateEnd ) {

  	$page->of(false); // and of course 'true' too :-)

  	$repeater->save();

	throw new WireException('start date must be earlier than end date');

}//endif
...
      

Robert

Share this post


Link to post
Share on other sites

Hi Robert,

on first look I didn't look to close to your code. Please try the following:

 

wire()->addHookAfter("Pages::saveReady", function (HookEvent $event) {
	$page = $event->arguments("page");

	if ( $page->hasField('my_repeater') ) {

		$repeater = $page->my_repeater;

		foreach ( $repeater as $item ) {
			$dateStart = $item->date_start;
			$dateEnd = $item->date_end;
			if ( $dateStart && $dateEnd && $dateStart >=  $dateEnd ) {
				$item->date_end = ''; 
				$item->save();
				
        		throw new WireException('start date must be earlier than end date');
			}//endif
		}//endforeach
	}//endif
});


Hope it helps.

Share this post


Link to post
Share on other sites

Thanks for your input.

But unfortunately that doesn't work either ... I've already tried it.
Somehow I think that couldn't work because the field to be saved is the repeater and not only a field in the repeater itselfe.

My next assumption is that it would have to work very differently than my current approach, since repeaters are saved as templates in the database. In my example above as temlate 'repeater_my_repeater'.

Unfortunately, I don't know how to store the fields value within the repeater via API / hook.
Therefore I still ask for your help or at least a hint 🙂

 

Share this post


Link to post
Share on other sites

@ro-bo,

I tried it with ProcessWire version 3.0.148, hook code placed in ready.php and it works without any problems.

Maybe a good idea is to use Tracy debugger, to get a hint where the problem lies.

 

Share this post


Link to post
Share on other sites

Hello and thanks a lot for your answers.

I already knew some of the posts, but I could not solve my special problem with the solutions there.

I have tried a lot now and noticed that field values with Pages::saveReady seem to change only if you actually make an entry in the PW backend and save the page.
Existing field values which already exist before the hook is included (and remain "untouched" in the backend) are not changed when the page is saved.
ProcessPageEdit::buildForm also works if the field input already exists and has not been changed by PW backend before saving.
That's why I thought this didn't work.

Something similar I've written here :

Please let me know if this is total nonsense what i am writing here.
I am not really familiar with hooks.

Robert

Share this post


Link to post
Share on other sites

By the way. Does anyone have a similar display problem in firefox and chrome regarding bolt icon for hookable methods?
See the screenshot below.

icon-bolt.png

Share this post


Link to post
Share on other sites

From my linked post: https://processwire.com/talk/topic/17346-hook-on-repeater-field-save/?do=findComment&comment=152504

On 10/4/2017 at 11:33 AM, Tom. said:

EDIT: 

Silly me, it was because I wasn't making changes to the repeater when testing, there for it wasn't firing saveReady. 

ProcessWire has a feature to track changes. If there are no changes unneeded hooks do not get fired. This is not only better for performance (I guess) but also great for conditional hooks: https://processwire.com/blog/posts/new-ajax-driven-inputs-conditional-hooks-template-family-settings-and-more/#new-conditional-hooks

  • Like 1

Share this post


Link to post
Share on other sites

@bernhard

ProcessWire has a feature to track changes. If there are no changes unneeded hooks do not get fired ... of course that makes sense!

Silly me, it was because I wasn't making changes to the repeater when testing, there for it wasn't firing saveReady... Of course i missed the very last sentence down there!

Thanks a lot for the help!

Robert

 

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...