Jump to content

Recommended Posts

Posted

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.

Posted

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?
}
Posted

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?

Posted

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?

Posted

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;

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
×
×
  • Create New...