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 4

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 bobbit6k
      Hi guys, 
       
      is there a way to declare numbers other than integer and float?
       
      My issue is that intgere is not enough for me because i'm dealing with multiple of bilions (let's say something like 111,532,372,567)
       
      Thanks,
       
      Rob
    • By abdus
      There's native `Fieldset in Tab` for creating editor tabs, but sometimes it could make more sense to put a field that's not directly related to `Content` into `Settings` or `Children` tab (such as for body class or some toggles that I see being used often). You can use the hook below to move fields between the tabs.
       

       
      // site/ready.php wire()->addHookAfter('ProcessPageEdit::buildForm', function (HookEvent $e) { // make sure we're editing a page and not a user if ($e->process != 'ProcessPageEdit') return; // RESTRICT BY TEMPLATE // $page = $e->object->getPage(); // if ($page->template != 'home') return; // RESTRICT BY ROLE // $user = $e->user; // if (!$user->hasRole('editor')) return; $form = $e->return; $contentTab = $form->children->get('id=ProcessPageEditContent'); $settingsTab = $form->children->get('id=ProcessPageEditSettings'); // $childrenTab = $form->children->get('id=ProcessPageEditChildren'); // if page template is set noSettings = true, $settings will not exist if (!$settingsTab) return; // MOVE TITLE FIELD TO SETTINGS TAB $title = $contentTab->get('title'); if (!$title) return; $contentTab->remove('title'); $settingsTab->prepend($title); });  
       
    • By abdus
      Based on this forum post, I wanted to find a more elegant solution for changing a field's settings per template. Hooking the page editor after it generates the form works and may still be needed for more complex modifications. But using field & template context, it's easier to modify the field settings and it greatly reduces the need for creating an almost identical field just to adjust a few things.
      In my blog post, I've written on how to extend contextual options to allow any fieldtype / inputfield settings to be changed depending on the template. Hope you find it useful, and if you have any questions or comments, feel free to post them here.
      https://abdus.co/blog/doing-more-with-fewer-fields-in-processwire/
    • By MilenKo
      Hello all. I know it might sound silly, but wanted to check how are you guys proceeding if in the theme development you have a need of some fields that are repeating in several templates. For example, I am having an image field that can be used as the site logo in "Settings" template or as to show the page image in the markup of the inner page template or else. I am aware, that if I leave the resizing in the markup, than the field can be used on multiple templates, but the field has some specific label/description, notes and/or placeholder that would be not-related. 
      Sticking to the logo example, I have a label saying: Add your logo image here. So if I use the same field for something else, the same text would show while editing the new next page calling for image.
      How are you guys taking care of this? Are you creating specific fields for every aspect of your theme or there are some tricks I am missing in the big picture?
      P.S. Besides the descriptive text of the field some might require to have a single image and some an array (e.g. you don't need to upload multiple logos, unless you want to randomize or call one based on some criterias, but for a gallery - sure I would have multiple)
    • By jploch
      Hi! 
      this should be easy, but I can't get it to work.
      I have a repeater with events.
      Every event has an option-field with multiple checkboxes.
      To filter the events on the frontend I have a script, that uses the class names to filter the results (an event can have multiple categories).

      Now I just want to add every checked option (title) to use as my class name.
      This is what I have so far, wich only gets the first title.
      foreach($page->events as $event) { $tags = $event->options->title; echo "<div class='size1of2 {$tags}'>"; echo "<img src='{$event->image->url}'>"; echo "{$event->text_editor}"; echo "</div>"; }
      I know there is an example with a foreach, but how would I use it in this context?
      foreach($page->countries as $country) { echo "<li>$country->title</li>"; }