Jump to content

How to Pass Data to Modal Edit Windows


James F Carr
 Share

Recommended Posts

In the admin, I am trying to access data from a page that contains an inputfieldand autofill that data into the modal edit window for a new entry.

For instance, I have a template which contains a PageTable field. When I add a new entry to the PageTable, I want to capture the ID from the originating admin page and add it to a field in the edit modal as a way of linking the new entry back to the originating page. The new PageTable entries are NOT children of the originating page, and cannot be for structural reasons. I have attached an image which I hope will help to clarify the basic result I want to achieve.

Is there a practical means of accomplishing this? I've been able to achieve something similar with the ConnectPageFields module, but it does not work with PageTable fields. Any input would be much appreciated.

Screen_Shot_2017-11-29_at_8_40_37_AM.png

Link to comment
Share on other sites

it has been discussed a lot on some topics, and it was determined that there is currently no easy way to pass data to the modal/page.

One way to achieve it is to hook into the save of the owning page and at that point cycle through the items in the page table, setting that field to the owning page. This works fine, however is risky in case the users do not save the owning page after adding new items to the page table. You could optionally also run a cron job to scan though the pages and look for items missing that field and set them that way.

Another way I have achieved this by using a custom 'page table' which i create using @kongondo's Runtime Markup, then i can put anything i want in the add new button (url params); Then i capture the input into a session variable, use that to set the field, then clear the session variable, hooking into save of the new item.

To do it in a Page Table without doing the first way i suggested here and in the linked topic, i think you'd likely need to add a url parameter to the popup which means hooking into the button markup. This is the structure of the add new button:

<a class="InputfieldPageTableEdit" data-url="/manage/page/edit/?id=9999&modal=1&context=PageTable" href="#">Title of Page</a>

if you could add a url param, like &ownerPage=1234 you could then capture the input and use it to set the field. You'd need to have a hook on the save of the new page;

I may be working on this so if i come up with a definitive solution will post back; i do think this is something that should have a decent recipe solution since it seems to come up often in my IA requirements.

  • Like 2
Link to comment
Share on other sites

@Macrura, thanks for the quick and thorough response. I'd been searching the forums for quite a while, yet had apparently failed to find the proper search phrasing.

I can always provide an option to select the originating page with a dropdown or autocomplete, it's just not efficient from a UX perspective and provides additional opportunity for input errors. It might just be something I have to work around during the development process until I can find a more elegant solution.

Link to comment
Share on other sites

i do have a client with a pretty big site where i do the cycle through the pages and set the owning page on those page table items;

but now i have to add a cron job to set them because the client keeps forgetting to save the owning page

and then even though those page table items are connected to the owning page by their existence in the page table field, they are still 'orphans' in the sense that the front end code we use needs to be able to identify any of those page table items' owning page without having to do a lookup;

part of the reason is that this site was built before Page Table was invented;

otherwise i think this method is the most reliable and easiest, here is some example code:

if($page->template == 'class') {
	$sessionsUnset = $page->session_table->find("class=");
	foreach($sessionsUnset as $su) {
		$su->class = $page;
		$su->setOutputFormatting(false);
		$su->save();
		$this->message("This class ({$page->title}) added to session {$su->id}");    
	}
}

 

  • Like 1
Link to comment
Share on other sites

Despite Soma helpfully telling us we are all wrong, the code I suggested in that thread works for me:

So you set up a field in each PageTable template to hold some reference to the container page - in the example below I use an integer field for the page ID, but you could use a Page Reference field instead. You would set the field to hidden so users do not see or change it. But for demonstration purposes I have left the field visible and also show the value in the PageTable columns.

Note that the value is only set when the PageTable field does the AJAX reload (when the modal closes), so the value is not available when adding a new page to the PageTable until the modal is closed.

In /site/init.php

$wire->addHookBefore('InputfieldPageTableAjax::checkAjax', function($event) {
    $field_name = $this->input->get('InputfieldPageTableField');
    // Just for this PageTable field
    if($field_name !== 'test_pagetable') return;
    $page_id = (int) $this->input->get('id');
    $item_id = (int) $this->input->get('InputfieldPageTableAdd');
    if($page_id) $page = $this->pages->get($page_id); // $page is the container page
    if($item_id) $item = $this->pages->get($item_id); // $item is the PageTable item
    // Set integer field to $page->id
    $item->setAndSave('container_page_id', $page->id);
});

pt.gif.762983e642960cd3595f9d7501b0a058.gif

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