Jump to content
James F Carr

How to Pass Data to Modal Edit Windows

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

@Robin S. Excellent solution! I think it is worth mentioning in the other thread that the solution does work (and cross-reference it with this GIF)...to help those who come across that thread. Ta.

  • Like 3

Share this post


Link to post
Share on other sites

this is amazing, wish i saw this about 2 hrs ago (before i wasted a bunch of time trying it another way...)

  • Like 1

Share this post


Link to post
Share on other sites

I'm not sure what was the point exactly. It works, but I need the id the moment I created the page table page not after saving it.

  • Like 1

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.

  • Similar Content

    • By MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
    • By BitPoet
      Here's a small new module that adds a "Manage tags" button to the template list, just like the field list already has.
      Easily add, remove and change tags for your templates.
      https://github.com/BitPoet/TemplateTagsEditList

    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By Vigilante
      In general, is there an easy way to know which method should be used to access the API?
      For example, when _ini.php is used in the theme, it would seem you have to use wire()->addHookBefore(...). But on other sites where they used ready.php, I've seen it go straight to just doing $this->addHookAfter() even though there is no class or namespace set up in the ready.php file.
      So how do I know when I can do $this, or $wire, or wire() to access things? And any other variables I'm not aware of.
      Thanks!
    • By VeiJari
      Hello forum,
      This is really a weird one, because front end editing works in a earlier website we did to a customer. 
      When I check the source code for current website it does initiate front end edit: 
      <span id=pw-edit-1 class='pw-edit pw-edit-InputfieldPageTitle' data-name=title data-page=1021 data-lang='1017' style='position:relative'><span class=pw-edit-orig>Tekijät</span><span class=pw-edit-copy id=pw-editor-title-1021 style='display:none;-webkit-user-select:text;user- select:text;' contenteditable>Tekijät</span></span>  But when I double click nothing happens (yes I'm 100% sure I'm superuser and logged in)
      I also tried to apply the front end with other methods than:
      $page->edit('title'); But didn't work either.
      We are using jquery 2.2.4, so it should not be a problem.
      Is this a bug related to current master or something else?
      Someone else having this problem as well?
×
×
  • Create New...