hellomoto

How to AJAX refresh field on change of another in editor?

Recommended Posts

I have a manufacturers page select field and a dependent models one with pages of template `model` which are allowed children of pages with template `manufacturer`. So I have this as the findPagesCode for the models page options field:

return $page->manufacturer->children();

However this requires the page be saved in order to display options. This is not ideal. 

I have a singular autoload module with the following:

public function init() {
    $this->pages->addHookAfter('render', $this, 'filterModels');
}
public function filterModels($event) {
    $page = $event->arguments('page');
    if($page->template != 'boat_vessel') return;
    $this->message("models filter");
}

It's doing nothing. 

I was thinking I could work out something with this example but I would need the above test to be working first anyway...

$this->pages->addHookAfter('changed', function(HookEvent $event) {
  $page = $event->object;
  $change = $event->arguments(0);
  if($page->template == 'boat_vessel' && $change == 'manufacturer') {
    // execute some code 
  }
});

But what? How do I refresh the models field?

Is there a way to do this in the field settings? I would think in the custom PHP textarea that `return $page->manufacturer->children();` would work but it doesn't. 

Share this post


Link to post
Share on other sites

Actually it does, but you have to save the page to get the childpages field options.

How can I auto-save the parentpage field on selection change and immediately subsequently auto-refresh the childpage field?

Share this post


Link to post
Share on other sites

I'm trying to alter some code I used to do something similar (fields for selected template) on another project (here) and so far just this simple direct snippet is not doing anything:

  public function init() {
    $this->pages->addHookAfter('render', $this, 'filterModels');
  }

  public function filterModels($event) {
    $page = $event->arguments('page');
    if($page->template != 'boat_vessel') return;
    $this->message("models filter");
  }

The module is active... Why no results?

Share this post


Link to post
Share on other sites

There is limited (and undocumented) AJAX 'dependent selects' support built into Page Reference fields. You must use the 'Custom find' or 'Selector string' option for defining selectable pages, and reference the source Page Reference field (that exists on the same page) using syntax like this:

parent=page.page_reference_field_name

Based on testing I have found:

  • It only works for Select, Select Multiple or AsmSelect inputfields
  • It does not work inside a repeater item
Edited by Robin S
Included 'Custom find'
  • Like 6

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 mwjt42
      How do you include fields in Twig templates?
      Tried using {% $page->Title %}, but that creates an error.
      Thanks
    • By Livius
      Hello fellow Process wire Developers!
      Today i have encountered an issue using the repeater where I can't find out how to solve it therefore i am asking for help.
      To be specific, I am trying to get the "file" field working properly in the repeater but i couldn't find a way on how to assign a property of a field that is being used by the repeater.
      download_pdf is the name of the field that i want to access properties from.
      What i wished i could do was something like $download->download_pdf.size or $download->download_pdf->name, something like that but obviously these didn't work
      Hope there is a solution and thanks in advance!
      Best wishes,
      Livius

    • By pwuser1
      Hi people I think I have seen them all but maybe I missed some of the just wanted to know what do you recommend for an editor with JQuery autocompletion or support? 
    • By rolisx
      Hi Guys,
      Just finished a website locally and wanted to upload it on the webserver of my customer. I got a server 500 error. Now, the guidelines of the hoster (world4you.com) does not allow "Options" in the htaccess-file. So, when I uncomment these:
      Options -Indexes
      Options +FollowSymLinks
      the site is visible, but the content won't show and no links are available. Not sure if I need the Symlinks-part but I guess I need a workaround for the Index-part. Can anybody help here? I need the website up and running asap....
      Thanks!
      Roli
    • By quickjeff
      Hi Guys, 
      I recently have received a client who has various page layouts. I wanted to give them the ability to simply add their HTML into a body field with CKEditor to allow HTML customization.
      However, the client isn't very familiar with HTML.
      This has left me to build a template that has multiple of the similar field types.
      The thing is, I cannot use the repeater since I cannot repeat lets say an image for repeated item 1 and only body field for repeater item 2.
      Therefore, I have to literally add 15 fields for them to totally be able to edit the page without issues.
      What are your thoughts on this approach?
      Also, I have to name the template according to what it contains, so I was thinking of using numbers instead of words.
      Reason being, numbers can describe the amount of title fields, body fields and images.
      Therefore something like, 3-3-2.php or three-three-two.php 
      I have never run into a situation where the client needed so many fields. I was always able to get around it with repeaters etc. 
      Thoughts?