Jump to content

Save page in PageTable field before close event on Add New modal.


elabx
 Share

Recommended Posts

This might be a repeated topic from one i created a couple days ago but wanted to rephrase it to be more clear. 

Is there a way to save pages created through the add new button on PageTable field before it is saved/closed? I see in the field's javascript that the page is only added when closing the modal, I do some after save hook work on the page using the modal window that depends on the page being added to the field. 

Link to comment
Share on other sites

Like you say, the PageTable page only belongs to the PageTable field when you save it. So you can get the container page as you save the page but not before then. Maybe this post helps:

If your PageTable pages are set to be children of the container page it would be easier, but often for PageTable fields that's not the case.

It's much easier to get the container page for Repeater fields, so I'd say a Repeater Matrix field is a better choice over a PageTable field for these kinds of things. If you are stuck with the PageTable field and there's no other solution then you could try something like hooking ProcessPageAdd and injecting some JS that checks for an iframe parent and gets the edited page ID from the parent URL, then saves it to a cookie or PHP session via AJAX, then you look for that page ID in the cookie/session in another hook before ProcessPageEdit. But that's hardly a straightforward solution.

Link to comment
Share on other sites

15 hours ago, Robin S said:

It's much easier to get the container page for Repeater fields, so I'd say a Repeater Matrix field is a better choice over a PageTable field for these kinds of things. If you are stuck with the PageTable field and there's no other solution then you could try something like hooking ProcessPageAdd and injecting some JS that checks for an iframe parent and gets the edited page ID from the parent URL, then saves it to a cookie or PHP session via AJAX, then you look for that page ID in the cookie/session in another hook before ProcessPageEdit. But that's hardly a straightforward solution.

Thanks for your answer Robin! Your solution is one of the paths i might take. And thanks for those links! Which basically lead to a solution by Soma saving info in the session variable. 

Another Idea I haver is to add a url param to the Add New button (also mentioned in a github issue by @Macrura), to have information about the page within the modal, and maybe that way, I would just need a hook on Page::added and getting the "pagetable field owner" through a input->get->owner_field. What's your opinion on this? I'm not entirely STUCK on pagetables, but I like better the way they render (RM is too bulky) and also seems more straightforward to bring as pages to display on another lister. (tho I know i could do this with repeater pages too, just doesn't seemed to me like the best way to go about it).

EDIT: I have something like this working now: (credits to @Macrura who also posted a similar idea in a github issue, so gave it a shot):

EDIT2: Simplified hook for pagetable add new button markup edit. Thanks @Robin S!

$wire->addHookAfter('InputfieldPageTable::render', function ($event) {        
        if($event->object->name == "asignaciones"){
            $markup = $event->return;
			$markup = str_replace("&context=PageTable", "&context=PageTable&field_parent={$event->object->hasPage}", $markup);
			$event->return = $markup;
        }
});

wire()->addHookAfter('Pages::added', function($event) {
    $page = $event->arguments(0);
    if($page->template == "asignacion"){
        if($this->input->get->field_parent){
            $owner = $this->pages->get($this->input->get->field_parent);
            if($owner->id){
                $owner->of(false);
                $owner->asignaciones->add($page);
                $owner->save('asignaciones');
            }
        }
    }

});

 

  • Like 3
Link to comment
Share on other sites

32 minutes ago, elabx said:

I have something like this working now

Looks good. You could maybe simplify the first hook by using a str_replace():

$wire->addHookAfter('InputfieldPageTable::render', function (HookEvent $event) {
	$markup = $event->return;
	$markup = str_replace("&context=PageTable", "&context=PageTable&field_parent={$event->object->hasPage}", $markup);
	$event->return = $markup;
});

 

  • Like 1
Link to comment
Share on other sites

2 hours ago, Robin S said:

Looks good. You could maybe simplify the first hook by using a str_replace():


$wire->addHookAfter('InputfieldPageTable::render', function (HookEvent $event) {
	$markup = $event->return;
	$markup = str_replace("&context=PageTable", "&context=PageTable&field_parent={$event->object->hasPage}", $markup);
	$event->return = $markup;
});

 

Thanks, definitely overdid it there ?

Link to comment
Share on other sites

  • 5 months later...

Hi everyone! Back to this topic hehe.

I'm having the issue that after a PageTable modal closes, it refreshes the table and I loose all the edits I did on render with the previous hook. I just can't seem to find where I have to hook to alter the table render on that ajax call. I see there is an InputfieldPageTableAjax class, but I don't make sense if it's there where i have have to hook and from what I can tell, the renderAjax() method from that class calls the render method from the Inputfield!

Any clues on this?

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...