Peter Troeger

passing variable to hook not working with _GET Variables?

Recommended Posts

Hi 🙂

I encountered a new problem I don't understand and have been trying to solve for the last few hours.

I'm trying to pass a _GET variable to a hook, but maybe the hook is executed before the get variables can be used?

This is my code:

	$jobID = $input->get->job;

	//$jobID = '1033'; <- when I do it like this, everything works perfectly.

	$activeJob = $pages->get($jobID); 
	$temp_value = $activeJob->id.' - '.$activeJob->title;
	echo $temp_value; //outputs expected string '1033 - Job Title'

	$wire->addHookAfter('InputfieldHidden::render', function($event) use(&$activeJob) {
		if($event->object->name == 'job_titel_und_nummer') {
			$temp_value = $activeJob->id.' - '.$activeJob->title;
			echo $temp_value;  //outputs '0 - ', so nothing
			$event->object->set('value', $temp_value);
		}
	});

As always, any help greatly appreciated! Thank you!

Peter

Share this post


Link to post
Share on other sites

Any change if you remove the ampersand from your use() statement?

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, Peter Troeger said:

$jobID = $input->get->job;

$jobID = $wire->input->get->job;

 

More infos :

 

@BitPoet I think he is outside template 

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, flydev said:

I think he is outside template

Though $input seems to be defined, or the echo before attaching the hook would show a NullPage too.

  • Like 1

Share this post


Link to post
Share on other sites
16 hours ago, BitPoet said:

Any change if you remove the ampersand from your use() statement?

No change. Tried that as well.

 

16 hours ago, flydev said:

$jobID = $wire->input->get->job;

 

More infos :

 

@BitPoet I think he is outside template 

That's right, I'm in ready.php

When I add $wire->  I get the following error: Notice: Trying to get property of non-object.

Any other ideas? 🙂

 

Share this post


Link to post
Share on other sites

Do you have installed TracyDebugger ? What's your ProcessWire version ? 

 

$jobID = 0;
$jobID = wire()->input->get->job;
bd("wire() :   $jobID");

$jobID = 0;
$jobID = $wire->input->get->job;
bd("$wire :   $jobID");

$jobID = 0;
$jobID = $input->get->job;
bd("$input :   $jobID");

 

151313140_Capturedecran2018-12-14a09_31_20.png.c8ec256958e86012fcc2a8ec82bd63d6.png

 

Share this post


Link to post
Share on other sites

@flydev no tracyDebugger installed.

My processwire Version is  3.0.121

 

When I add your code modified like this:

$jobID = 0;
$jobID = wire()->input->get->job;
echo $jobID;

$jobID = 0;
$jobID = $wire->input->get->job;
echo $jobID;

$jobID = 0;
$jobID = $input->get->job;
echo $jobID;

every echo outputs the ID correctly (must have had a spelling error before).
But still not working in my $wire->addHookAfter('InputfieldHidden::render', function($event) use($jobID) {

oh, and it also works fine with addHookProperty.

Share this post


Link to post
Share on other sites

You should install TracyDebugger asap and then replace all your `echo` with `bd($your_var)` call, it will be easily for us to help you and for you to debug your code.

Your hook is working fine on my side. Comment this line `// if($event->object->name == 'job_titel_und_nummer') {` and try again.

 

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.