Jump to content

How to hook into the function which applies when page has not been successfully saved?


Clarity
 Share

Recommended Posts

Hello everyone!

I'm using multi-language site, and if I save the page with title and name only on non-English language, the error appears. I need to hook into some function which applies immediately when I click "Save" so that I can fill the title on English language. Can you please tell the name of this function or give a clue?

  • Like 1
Link to comment
Share on other sites

The error is "Missing required value" on title and name. It is not in error stack, it's just a specific error which follows from requirement of title and name. I want to resolve it by filling transliterated title and name on other languages. However, I don't know the name of the function in which I should hook in order to edit the title well before actual save(). When I hook immediately before save() and try to bd() inside this hook, bd() doesn't work.

Link to comment
Share on other sites

You may try to use the page saveReady hook in ready.php to do a check and conditionally correction. But this would also be called / applied when everything is ok. 

Or, if you want to have a more optimized solution, you can check the possible involved methods :

image.png.48336c2cc0a7d3485920f35b448fb51b.png

A search in the wire folder shows me this above result, where I would first check the ProcessPageEdit module, but don't know really.

Edited by horst
  • Thanks 1
Link to comment
Share on other sites

Thank you for the clue! I found the method ___processInput in ProcessPageAdd.module which works before Pages::save, however, it takes $form (likely the form in the editor of page) as an argument instead of $page. It would be nice if it would be possible to extract $this->page from this method.

Link to comment
Share on other sites

Maybe something like this can work?

        if(!$this->wire->input->get->id) return;
        $editedPage = $this->wire->pages->get('id=' . $this->wire->input->get->id);

But, if it is to early in the process, the page doesn't have an ID, So I'm not positive if it is right to hook into processing the initial form, or if it would be better to hook into before displaying the editpage with the error message labels? (And do another redirect after correcting the missing values).

  • Like 1
Link to comment
Share on other sites

2 hours ago, Clarity said:

It would be nice if it would be possible to extract $this->page from this method.

You should be able to to that with $event->object->getPage()

Quite whether this is the right place to hook, however, is another matter...

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

15 hours ago, horst said:

Maybe something like this can work?

        if(!$this->wire->input->get->id) return;
        $editedPage = $this->wire->pages->get('id=' . $this->wire->input->get->id);

But, if it is to early in the process, the page doesn't have an ID, So I'm not positive if it is right to hook into processing the initial form, or if it would be better to hook into before displaying the editpage with the error message labels? (And do another redirect after correcting the missing values).

Actually, the page which is not yet created actually has ID = 0, but the path for adding the page is /page/add/ and it doesn't have ID of this page as GET parameter.

13 hours ago, MarkE said:

You should be able to to that with $event->object->getPage()

Quite whether this is the right place to hook, however, is another matter...

Thank you, it seems to be right! I shall explore what I can do with this page in more details and report here about the results.

  • Like 2
Link to comment
Share on other sites

I found that my initial idea of modifying the page is not good.

By looking closer into processInput method and exploring what exactly raises the error, I found that the following code does it:

    /** @var InputfieldPageName $nameField */
    $nameField = $form->getChildByName('_pw_page_name');     
    $name = $nameField->value; 

    if(!strlen($name)) {
        $nameField->error($this->_("Missing required field: name")); 
        return false; 
    }


So the error raises before actual operation with page. I've tried to modify the value of $name via the hook (write "1" to it just to check if it works):

    wire()->addHookBefore('ProcessPageAdd::processInput', function($event) {
        $form = $event->arguments('form');
        $form->getChildByName('_pw_page_name')->attr('value', '1');
        $event->arguments('form', $form);
    });

Inside the hook the value changes, however, the error still appears. Bar dump of $name inside the code of processInput shows that $name still has zero length. Can you please explain what's wrong and why I can't modify getChildByName()'s value in a hook?

Link to comment
Share on other sites

Finally, I found a solution: as @MarkE wrote, I was trying to hook not in proper place. I've hooked another ___processInput method, in InputfieldForm. The working hook is:

wire()->addHookBefore('InputfieldForm::processInput', function($event) {
	$input = $event->arguments('input');
	if(!$input->title) {
		$input->title = '1';
	}
	if(!$input->_pw_page_name) {
		$input->_pw_page_name = '1';
	}
});

Thank you all for the advices!

  • Like 3
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...