Jump to content

How to populate an integer fieldtype via autoload module


Federico
 Share

Recommended Posts

Hello PW community,

I know this is a discussed topic in some other posts, but none of them seem to work when I try to implement found suggestions. Sorry for duplicate it.

Goal: I would like to populate an integer fieldtype (called proj_code_sint) before a page is rendered in the admin, all from an autoload module. here's the sample code, which for unknown reason it doesn't do anything...

<?php

class moduleTitle extends WireData implements Module {

    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return string
     *
     */
    public static function getModuleInfo() {

        return array(
            'title' => 'moduleTitle',
            'version' => 001,
            'summary' => 'Autopopulate integer fieldtype with incremental number',
            'author' => 'mf',
            //'singular' => true,
            'autoload' => "template=admin",
            );
    }

    public function init() {
        $this->addHooKBefore("Inputfield::render", $this, "renderField");
    } 

    public function renderField(HookEvent $event) {
        $field = $event->object;
        // here custom logic
        if($field->name == "proj_code_sint") {
    	    $field->set('value', '123'); // example
    	}
	}

} 

Can you please suggest what I am missing here? maybe to save() the page at the end?

thank you very much 

 

Link to comment
Share on other sites

That should work fine. I just tested via the Tracy console (injecting into ready.php). There is certainly no need to save the page because you are populating at render().

I don't think it should have anything to do with the name of your function being renderField, but you could try something that doesn't match a core public function.

The other thing I would suggest (which I didn't do in the example below, but will work) is to use InputfieldInteger::render just to improve the efficiency a little.

Also, try like I have in your ready.php file.

The other problem might be that your module isn't installed or isn't being loaded - did you do a Modules > Refresh? Does it show up under the "Modules Loaded" section of the Debug Mode panel in Tracy?

5a1cb52f3e051_ScreenShot2017-11-27at4_59_39PM.png.45ec8a4012a304fa6680432b7ee243a4.png

  • Like 3
Link to comment
Share on other sites

It would be a good idea to fix the typo in the method name...

$this->addHooKBefore("Inputfield::render", $this, "renderField"); // should be a lowercase k in addHookBefore

...but surprisingly it seems to work regardless, so probably not the cause of the problem.

  • Like 1
Link to comment
Share on other sites

Thank you all,

It was my mistake, as the module itself was not properly loaded. The code above actually...works! sorry for that.

Good to know that in PHP methods are not case sensitive!

@Robin S yes I know and tried the module, what I do not like of that is the fact that creates additional db table to store progressive numbering, but I can't really say anything about what is the best approach in terms of efficiency and data storage. 

Btw, do you know how to store padded numerical values within the integer fieldtype? like this "0013" - I guess it requires changes to the default fieldtype behaviour in order to achieve this

 

 

Link to comment
Share on other sites

7 hours ago, Federico said:

 

Btw, do you know how to store padded numerical values within the integer fieldtype? like this "0013"

If I understand my math correctly, if it has a leading zero, then it's not an integer anymore. You could just use a text field though :)

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...