Jump to content

Recommended Posts

Posted

I'd like to add extra markup to a specific inputfield text in the admin using a module, I tried an hook like this but pw says the method doesn't exist
 

$this->addHookBefore('Inputfield::render', $this, "addMarkup");

 
protected function addMarkup($event){
    $Inputfield = $event->object;
    if($Inputfield->attr('name') == "myfieldname"){
         
        $Inputfield->setMarkup([
            'item_content' => "my markup"
        ]);
           
    }
}


Posted

If you hook the render() method, the $event->return contains the markup. Therefore you need to modify $event->return to modify the markup:

Tpb5ZCW.png

  • Like 3
  • 5 years later...
Posted (edited)

Just adding this here as note for me and others. There is also an alternative way to append custom markup to an inputfield:

$inputfield->appendMarkup('<div class="test">Hello</div>');

 

Edited by jploch
Sorry the original post didn’t make much sense. I corrected my post
  • Like 1
Posted
36 minutes ago, jploch said:

Just adding this here as note for me and others. There is also a much simpler way to add custom markup to an inputfield:

$field->appendMarkup('<div class="test">Hello</div>');

Which approach to take depends on where you want the added markup to appear, relative to things like description and notes.

Demo:

$wire->addHookBefore('InputfieldText::render', function(HookEvent $event) {
	/** @var InputfieldText $inputfield */
	$inputfield = $event->object;
	if($inputfield->name !== 'text_1') return;
	$inputfield->prependMarkup('<div>prependMarkup</div>');
	$inputfield->appendMarkup('<div>appendMarkup</div>');
});

$wire->addHookAfter('InputfieldText::render', function(HookEvent $event) {
	/** @var InputfieldText $inputfield */
	$inputfield = $event->object;
	if($inputfield->name !== 'text_1') return;
	$event->return = '<div>before render</div>' . $event->return . '<div>after render</div>';
});

image.png.ad183d98c135132beddbce4ca55cf740.png

Related request: https://github.com/processwire/processwire-requests/issues/536

  • Like 4

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
  • Recently Browsing   0 members

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