Jump to content
bmacnaughton

mechanism to initialize Inputfield configuration at construction or init time?

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

How about implementing a custom function for that? 

$field = wire('modules')->get("InputfieldPasswordGeneric")->setConfigs($array);

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Gadgetto
      Hi,
      for my GroupMailer module I've created a custom Fieldtype + Inputfield module which provides multi-column field values. The first field column is a visible text field and there are some other columns which are not presented to user (they are rendered as hidden form fields).

      This is the database schema:
      $schema['data'] = 'text NOT NULL'; // we're using 'data' to represent our 'subject' field $schema['sendstatus'] = 'tinyint NOT NULL DEFAULT 0'; // message send status $schema['recipients'] = "int(10) unsigned NOT NULL DEFAULT 0"; // recipients counter $schema['sent'] = "int(10) unsigned NOT NULL DEFAULT 0"; // sent counter $schema['started'] = "int(10) unsigned NOT NULL DEFAULT 0"; // message sending start $schema['finished'] = "int(10) unsigned NOT NULL DEFAULT 0"; // message sending finished This are the ___wakeupValue and ___sleepValue methods:
      Now I try to extend this Fieldtype/Inputfield to provide multi language features.
      Only the first value ("data" which represents the "subject" field) should be/needs to be multi language!
      I had a look at the built in Fieldtypes (e.g FieldtypeText & FieldtypeTextLanguage) which provides multi language support but I couldn't find a similar case (multi-value field with language support). All built in Fieldtypes are single-value fields.
      I know this is a very "general" question but maybe somebody could push me in the right direction?
    • By Harmen
      I want to add a few pages to an AsmSelect Page field inside a repeater using the following code:
      $trialsPage = wire("pages")->get(28422); // Get the page $trialsPage->of(false); $newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater foreach ($selectedProducts as $selectedProduct){ $productPage = $pages->get("template=product, reference=$selectedProduct"); $newTrial->trial_selected_products->add($productPage); } $newTrial->save(); $trialsPage->save(); However, when I check the page where the field is located it doesn't have the new values as expected. The selected pages exist, the field is in the right location, made sure that the output formatting is turned off: $page->of(false); But it still doesn't work with a variable. No matter what I try, it doesn't work.
      It only works when I replace $selectedProduct with a hardcoded string. Am I doing something wrong here?
    • By Sebi2020
      Hey, I'm new and I created a simple module for tagging pages because I didn't found a module for it (sadly this is not a core feature). This module is licensed under the GPL3 and cames with absolutly no warranty at all. You should test the module before using it in production environments. Currently it's an alpha release. if you like the module or have ideas for improvements feel free to post a comment. Currently this fieldtype is only compatible with the Inputfield I've created to because I haven't found  an Inputfield yet, that returns arrays from a single html input.
      Greetings Sebi2020
      FieldtypeTags.zip.asc
      InputfieldTagify.zip
      InputfieldTagify.zip.asc
      FieldtypeTags.zip
    • By theoretic
      Hi there! And thanks for Processwire!
      Maybe i'm not very attentive but couldn't find any tutorial on creating custom fieldtype with custom inputfield. Would like to make one for selecting color(s) from predefined list of colors. The closest existing match is FieldtypeSelectOptions with InputfieldSelect but i need something different. There's a very good post from @Soma   which is, however, insufficient to begin building my own fieldtype or at least to attach the existing inputfield to an existing field.
      Will appreciate any help. Thanks in advance!
    • By DxJR
      So is there anyway to get Inputfield Dependencies to work with front-end editing? the field is there, but it isn't showing the results I need based on the field selection it's dependent on. 
×
×
  • Create New...