Jump to content

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


hellomoto
 Share

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?

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...