bmacnaughton Posted March 4, 2015 Share Posted March 4, 2015 I would like to provide a way to for the caller to configure an Inputfield when getting it, i.e., when either __construct or init is called, as opposed to having to set each field independently after the field has been instantiated. How would I do that? What I have now: // get a password field $field = wire('modules')->get("InputfieldPasswordGeneric"); $field->attr('id+name', 'password'); $field->attr('size', 40); $field->required = true; $field->label = 'Password:'; What I'd like to do (conceptually): $defaults = array( 'required' => true, 'label' => 'Password:', 'allow_whitespace' => false, 'numDigits' => 0, ); $field = wire('modules')->get("InputfieldPasswordGeneric", $defaults); $field->attr('id+name', 'password'); $field->attr('size', 40); I could just create a property, like defaults, and decode that. But the code won't see that until rendering time so will have to do additional processing each render. It seems like it belongs at initialization or construction time. Link to comment Share on other sites More sharing options...
Soma Posted March 5, 2015 Share Posted March 5, 2015 I'm not sure I understand what the problem with first method is. Are you speaking about your Inputfield's default (in module) ? Look at InputfieldText how it set defaults public function __construct() { parent::__construct(); $this->setAttribute('type', 'text'); $this->setAttribute('size', 0); $this->setAttribute('maxlength', self::defaultMaxlength); $this->setAttribute('placeholder', ''); $this->setAttribute('pattern', ''); $this->set('initValue', ''); // optional initial value $this->set('stripTags', false); // strip tags from input? } Link to comment Share on other sites More sharing options...
bmacnaughton Posted March 6, 2015 Author Share Posted March 6, 2015 I didn't ask this very well. I'm trying to find out if there is a way to specify the values at the time the field is being created, not within the field code itself. $field = wire('modules')->get("InputfieldPasswordGeneric"); I'm looking for the rough equivalent of: $field = new InputfieldPasswordGeneric($config); where $config is an associative array that can override the defaults for selected properties so the field is configurable. Does that make sense? Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 6, 2015 Share Posted March 6, 2015 How about implementing a custom function for that? $field = wire('modules')->get("InputfieldPasswordGeneric")->setConfigs($array); Link to comment Share on other sites More sharing options...
bmacnaughton Posted March 6, 2015 Author Share Posted March 6, 2015 I can do that. I wanted to check to make sure there was no standard way to do so before I went that route. Thank you. Link to comment Share on other sites More sharing options...
bmacnaughton Posted March 11, 2015 Author Share Posted March 11, 2015 How about implementing a custom function for that? $field = wire('modules')->get("InputfieldPasswordGeneric")->setConfigs($array); Should my setConfig function return $this so it can be chained? Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 11, 2015 Share Posted March 11, 2015 In this case it would be good, so it can be directly saved to the variable, but it's not a necessity and it's your module, you decide what you do there. ProcessWire does also not return "$this" most of the time you chain stuff. // PageArray -> Page -> Pageimages -> Pageimage -> String $pages->get("id=1466")->images->eq(0)->url; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now