esspea Posted February 16, 2023 Posted February 16, 2023 Hi all, I have a strange notice appearing on my front page: Notice: Trying to get property 'variable' of non-object in /site/ready.php on line 62 The hook in ready.php is calling a $page->parent->object and obviously does not find it when on the front page, but the hook should only load when a specific form is loaded and it has a if this form else return statement. So why does this appear and how can I prevent it? Best regards.
bernhard Posted February 16, 2023 Posted February 16, 2023 It would likely help if you share the code/hook in question, if that's possible ?
esspea Posted February 16, 2023 Author Posted February 16, 2023 Yes it's possible, here is the whole hook, line 62 is indicated with (62): $wire->addHookBefore('FormBuilderProcessor::renderReady', function($event) { $form = $event->arguments(0); if($form->name != 'kosning') return; // form name $field = $form->getChildByName('atkvaedi'); // get form field if(!$field) return; $parent = wire('page')->parent; //connect to parent page (62) $gestgjafi = $parent->parent->gestgjafi; $pages = $parent->get('thatttakandi_a_hatid'); //Get field values from parent page $sidur = explode('|', $pages); if(is_array($sidur)) { $newOptions = []; foreach ($sidur as $basID) { $page = wire('pages')->get($basID); if($page) { $children = $page->children(); foreach ($children as $child) { $optionValue = $child->title . ' (' . $page->title . ')'; $newOptions[$child->id] = $optionValue; } } } // Exclude the option with a value of $gestgjafi foreach ($newOptions as $key => $value) { if (strpos($value, $gestgjafi->title) !== false) { unset($newOptions[$key]); } } // Sort the remaining options by title in ascending order asort($newOptions); $field->options = $newOptions; } });
gornycreative Posted February 16, 2023 Posted February 16, 2023 Are you meaning to call the parent of a parent? $parent = wire('page')->parent; //connect to parent page (62) $gestgjafi = $parent->parent->gestgjafi; //This is like chaining wire('page')->parent->parent->variable.
esspea Posted February 16, 2023 Author Posted February 16, 2023 Yes, in this case I'm calling the grandparent. Is there a better way? To be clear: The hook works, there's no problem there. The question is: why is this variable being called from a page that does not contain the form the hook applies to?
esspea Posted February 17, 2023 Author Posted February 17, 2023 Well this turned out to be a simple one. The notice message disappears when: $config->debug = true; is set to false.
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