PWaddict

Is it possible to change the Notes of a field with a Hook?

Recommended Posts

yes

Spoiler

// site/ready.php
$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
  $field = $event->object;
  if($field->name != 'yourfield') return;
  $field->entityEncodeText = false;
  $field->notes = 'this is <strong>UNTESTED</strong>';
});

 

 

  • Like 4

Share this post


Link to post
Share on other sites
11 hours ago, bernhard said:

yes

  Hide contents


// site/ready.php
$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
  $field = $event->object;
  if($field->name != 'yourfield') return;
  $field->entityEncodeText = false;
  $field->notes = 'this is <strong>UNTESTED</strong>';
});

 

 

It works great. Thank you.

I hope you can help me with 1 more thing: The field belongs to a PageTable. That PageTable is used under 2 different templates: template1 & template2. I want to change the field notes ONLY for template2.

Share this post


Link to post
Share on other sites

I figured how to do it:

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
	
	$field = $event->object;
	if($field->name != 'myfield') return;
	$page = $this->process->getPage();
	$parent = $page->parent();
	if ($parent->template->name == "mytemplate") {
	$field->entityEncodeText = false;
	$field->notes = 'My new notes!';
	}
	
});

Please correct me if I did something wrong.

  • Like 2

Share this post


Link to post
Share on other sites

ok didn't know there is a difference.

if you do not do already i recommend using adrians awesome tracy debugger module. then you see that inside a repeader the field has a different name:

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {	
	$field = $event->object;
	bd($field->name);
});

repeater.thumb.png.c093088080acc466539aa6e545b4ec10.png

hope that helps and you find your solution on your own :) i'm busy

  • Like 2

Share this post


Link to post
Share on other sites
19 hours ago, PWaddict said:

How can I do the same thing on a field that belongs to a repeater???

Easiest way that works for inputfields both inside and outside of repeaters is to match the name of the field associated with the inputfield:

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
    $inputfield = $event->object;
    if($inputfield->hasField == 'YOUR_FIELD_NAME') {
        //...

 

  • Like 4

Share this post


Link to post
Share on other sites

Here is the solution:

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
	
	$field = $event->object;
	$page = $this->process->getPage();
	
	if($field->hasField == 'myfield' && $page->template->name == "mytemplate") {
		$field->entityEncodeText = false;
		$field->notes = 'My new notes!';
	}
	
});

Thanks for your tips.

Share this post


Link to post
Share on other sites

Now I cannot access Setup > Templates. I'm getting this error: ProcessWire: ProcessTemplate: Method ProcessTemplate::getPage does not exist or is not callable in this context.

Share this post


Link to post
Share on other sites
3 minutes ago, PWaddict said:

Now I cannot access Setup > Templates.

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
    $field = $event->object;
    if($this->process != 'ProcessPageEdit') return;
    $page = $this->process->getPage();
    //...

BTW, it would be better to hook a more specific inputfield render rather than just Inputfield::render. So something like InputfieldPageTable::render or whatever inputfield type you are targeting.

  • Like 2

Share this post


Link to post
Share on other sites
23 minutes ago, Robin S said:

BTW, it would be better to hook a more specific inputfield render rather than just Inputfield::render. So something like InputfieldPageTable::render or whatever inputfield type you are targeting.

I replaced Inputfield::render with InputfieldRepeater::render but it doesn't work.

Share this post


Link to post
Share on other sites
10 hours ago, PWaddict said:

I replaced Inputfield::render with InputfieldRepeater::render but it doesn't work.

you need to use the classname of the inputfield that you are modifying. your field is inside a repeater, but it is not a repeater itself. if it is a textfield you have to use InputfieldText, if it was a checkbox it would have to be InputfieldCheckbox.

Also $field->entityEncodeText = false; is only necessary if you want to use markup in your note ;)

  • Like 3

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.