Federico Posted November 28, 2017 Share Posted November 28, 2017 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 More sharing options...
adrian Posted November 28, 2017 Share Posted November 28, 2017 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? 3 Link to comment Share on other sites More sharing options...
Robin S Posted November 28, 2017 Share Posted November 28, 2017 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. 1 Link to comment Share on other sites More sharing options...
adrian Posted November 28, 2017 Share Posted November 28, 2017 Just now, Robin S said: ...but surprisingly it seems to work regardless, so probably not the cause of the problem. Nice catch - for some reason in PHP methods are not case sensitive - very weird IMO Link to comment Share on other sites More sharing options...
Robin S Posted November 28, 2017 Share Posted November 28, 2017 2 minutes ago, adrian said: in PHP methods are not case sensitive Interesting. @Federico, if you are wanting an integer field that auto-increments across the whole site then there is a module for that: https://modules.processwire.com/modules/integer-auto-increment/ I haven't used it myself. 1 Link to comment Share on other sites More sharing options...
Federico Posted November 28, 2017 Author Share Posted November 28, 2017 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 More sharing options...
adrian Posted November 28, 2017 Share Posted November 28, 2017 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 1 Link to comment Share on other sites More sharing options...
Federico Posted November 28, 2017 Author Share Posted November 28, 2017 your math is correct @adrian a simple text field type does the job. Thank you all 1 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