Jump to content

Add wrapClass to Inputfield in admin


arjen
 Share

Recommended Posts

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

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

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

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.

  • Like 1
Link to comment
Share on other sites

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);
    }
});
  • Like 3
Link to comment
Share on other sites

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);
        }
    }
    
});
  • Like 5
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...