Jump to content

Notice from ready.php: Trying to get property of non object


esspea
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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;
    }
});

Link to comment
Share on other sites

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?

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...