arjen Posted February 15, 2016 Share Posted February 15, 2016 Hi, I would like to add an wrapClass property to an Inputfield in the admin. The class should be based on the tag used in the Inputfield settings. I have given all my fields a tag. When I load a page in the admin only the Page fields are loading the wrapClass. I'm using the following code (PW version 3.0.7): <?php wire()->addHookBefore('Inputfield::render', function($event) { $inputfield = $event->object; if (!$inputfield->tags) return; $inputfield->addClass("tag-{$inputfield->tags}", 'wrapClass'); }); in my /site/init.php. I also tried /site/templates/admin.php but with no difference. Any thoughts? Link to comment Share on other sites More sharing options...
Martijn Geerts Posted February 15, 2016 Share Posted February 15, 2016 https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/Inputfield.php#L184 Are you sure Inputfield::render is called at all? Maybe InputfieldSomethingElse::render is called. 2 Link to comment Share on other sites More sharing options...
arjen Posted February 15, 2016 Author Share Posted February 15, 2016 Hey Martijn, Yup, pretty sure. It only seems to work on Page fields. I can change pretty much anything but only within the Page fields. I must be missing something. I also tried addHookAfter and made sure the event is returned, but no success. If I try for example: <?php wire()->addHookBefore('InputfieldText::render', function($event) { // Notice the change from Inputfield to InputfieldText. $inputfield = $event->object; $inputfield->label = 'tralala'; }); The labels of all the Text fields will be replaced with tralala. But I would like a more general solution, otherwise I would have to specify all fields. Link to comment Share on other sites More sharing options...
horst Posted February 15, 2016 Share Posted February 15, 2016 I'm not sure, but I think it is not possible to hook into an abstract declared method: https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/Inputfield.php#L577, or is it? 1 Link to comment Share on other sites More sharing options...
arjen Posted February 15, 2016 Author Share Posted February 15, 2016 Hi Horst, I loosely based my coding on this topic. I assumed that if InputfieldPage render method was hookable the InputfieldPage render method would be too. Still tangling my head around this OO stuff, not really sure what this abstract method means. Martijn will probably explain it to me again Link to comment Share on other sites More sharing options...
horst Posted February 15, 2016 Share Posted February 15, 2016 Hi Arjen, in this case, the InputfieldText extends the Inputfield class and has to provide a class named ___render(). Every class that extends the Inputfield class have to do this, otherwise an error will be thrown by PHP. But the parent class itself has not an executable method. To declare it abstract means, it is declared as mandatory for all (child) classes that extends this (parent) class to have this method. 1 Link to comment Share on other sites More sharing options...
arjen Posted February 16, 2016 Author Share Posted February 16, 2016 Hey Horst, Thanks for your post. We figured it out. My assumption that the Inputfield stores the tags wasn't correct. You have to get the field itself and you can access the tags from there. You can find the code that actually works: wire()->addHookBefore('Inputfield::render', function($event) { $inputfield = $event->object; if ($inputfield->hasFieldtype && !empty(wire('fields')->get($inputfield->name)->tags) { $inputfield->set('wrapClass', "tag-" . wire('fields')->get($inputfield->name)->tags); } }); 3 Link to comment Share on other sites More sharing options...
arjen Posted February 20, 2016 Author Share Posted February 20, 2016 After doing some testing it turns out that when a user doesn't have access to edit a field the classed were not added since there is no Inputfield to render. Better solution: wire()->addHookBefore('InputfieldWrapper::render', function($event) { $wrapper = $event->object; foreach ($wrapper->getChildren() as $f) { // Only fieldtypes can have tags && make sure the fieldtype has tags if ($f->hasFieldtype && !empty(wire('fields')->get($f->name)->tags)) { $f->set('wrapClass', "tag-" . wire('fields')->get($f->name)->tags); } } }); 5 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