Jump to content
Sign in to follow this  
hellomoto

Conditional FieldtypeOptions: fill or filter with/by template chosen in previous field in editor

Recommended Posts

This is in my module's install() function:

      $f = new Field();
      $f->type = wire('modules')->get('FieldtypeSelectExtOption');
      $f->set("name", "iu_template")->set("label", "Template");
      $f->option_table = "templates";
      $f->option_value = "id"; $f->option_label = "name";
      //$f->filter('id not in (2,3,4,5)');
      $f->set("required",1);
      $f->set("tags", "-impupd")->set("columnWidth",50)->save();
      $f = new Field(); // set to select from template's allowed parents
      $f->type = wire('modules')->get('FieldtypePage');
      $f->set("name", "iu_parent")->set("label", "Parent");
      $f->set("derefAsPage",1)->set("required",1);
      $f->set("findPagesSelector","template!=admin,hasParent!=2");
      $f->set("tags", "-impupd")->set("columnWidth",50)->save();
      $f = new Field();
      $f->type = wire('modules')->get('FieldtypeOptions');
      $f->inputfieldClass = 'InputfieldCheckboxes';
      $f->set("name", "iu_actions")->set("label", "Action(s)");
      $f->options = ["import" => "Import", "update" => "Update"];
      $f->set("tags", "-impupd")->set("columnWidth",50)->save();
      $f = new Field();
      $f->type = wire('modules')->get('FieldtypeOptions');
      $f->set("name", "iu_uid")->set("label", "Match Field");
      $f->description = "Values must be unique.";
      $options = array();
      foreach(wire('fields') as $opt) {
        $options["{$opt->name}"] = "{$opt->title}";
      }
      $f->options = $options;
      $f->set("required",1);
      $f->set("tags", "-impupd")->set("columnWidth",50)->save();

Oddly every time I install the module the second field `iu_parent` says it still needs configuring, without fail; I have to go to the field editor and just save it as it is one time.

Anyway the real field in question here is the last, i.e., `iu_uid`. That method of adding options is entirely ineffective, sadly; it adds none at all. What I need is for it to dynamically populate with the fields in the previously selected template's fieldgroup on saveReady. This is possible with page fields, to dynamically populate options based on another field's value.

Alternatively I can set up `iu_uid` like so:

      $f = new Field();
      $f->type = wire('modules')->get('FieldtypeSelectExtOption');
      $f->set("name", "iu_uid")->set("label", "Match Field");
      $f->description = "Values must be unique.";
      $f->option_table = "fields";
      $f->option_value = "id"; $f->option_label = "name";
      $f->set("required",1);
      $f->set("tags", "-impupd")->set("columnWidth",50)->save();

Then I would just need for a hook to filter the options to correspond with the `iu_template` value.

Any pro tips/guidance to spare on this subject?

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hello forum, we're trying to use Processwire as our REST-API. We are having problems with our API login to Processwire from frontend. It gives us 403 error.
      We have installed ProcessWire to subdirectory (/api/*) and our frontend is static JS files at root ( / ). Apache access logs gives 404 to our POST-request, but browser devtools shows 403 for our POST /api/login request. 
      Processwire backend panel works. We also have a GET endpoint for the API that returns 200 with correct payload.  So we're wondering why does our GET works but POST doesn't?
      Does this have something to do with Processwire .htaccess, or is this because of our webhost? What should we check first? Any help would be appreciated.
    • By benbyf
      Never really got my head around field groups and it shows, In my module uninstall I think I figured out how to delete a page, then a template and it's fieldgroup so that nothing gets upset. But for fields that belong to templates that need to stoick around I'm still abit stuck to how you would remove the field and when I try I still get:
      Unable to delete field '[field name]' because it is in use by these fieldgroups: 'user'
      is there a tutorial or best method for removing added fields, pages and templates?
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
    • By killedfriendz
      I am very sorry for asking this but i totally do not understand how to set values of checbox using API. 
      I have checbox field on my page with name "order_status". 
      So i've tried few ways to make it checked but it still doesn't work:
       
      $userPage->order_status->value = 1; $userPage->order_status->add(1); $userPage->order_status->add(true); Could you please tell me how to do it?
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
×
×
  • Create New...