Jump to content

Failing to hook from one custom module into another


Ovi
 Share

Recommended Posts

The full page gets displayed for this kind of hook cause it's not that you hook into page view or render.. so it was working all the time. I wouldnt know why not.

Anyway now you know for the next time ;)

Link to comment
Share on other sites

  • 3 weeks later...

Hi everyone, I'm back on this project after it got put on the backburner for a while and so i'll need to get this working. Thanks for your awesome support so far!

But one problem that I can spot in taking a closer look at the code is that the definition of your processRatingInput() method in CommentFormWithRatings.php has no arguments sent to it, but your hook function is assuming arguments[0] is a $page, when in fact it would be null. This doesn't indicate why the hook isn't getting called but it seems like you would get a PHP error in your hook function. I would fix that just in case it is causing the strange behavior or hiding some other error. 

What Ryan is saying here is very true, since i get the error:

Undefined offset: 0 in /public_html/site/modules/PageAverageRating.module on line 24 

With line 24 being of course, just like Ryan says: 

$page = $event->arguments[0];

The reason "$event" is there is that i just copy-pasted stuff from HelloWorld.module and tried to make it work.

Obviously the $event argument is passed by all the methods that are being hooked in the examples given in the HelloWord.module file, but not by the method i'm hooking into.

So now that the hook works, i just need a way to access the current page and all its attributes. 

Can anybody suggest a simple (as possible) way i can achieve that?

Thanks so much as always.

Link to comment
Share on other sites

So what i have now is this: 

<?php

class PageAverageRating extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Copy average rating into hidden field',
            'version' => 100,
            'summary' => 'Copies the average rating to another field',
            'singular' => true,
            'autoload' => true,
        );
    }

    public function init() {
        
       $this->addHookAfter("CommentFormWithRatings::processRatingInput", $this, 'copyAverageRating');

    }

    public function copyAverageRating() {
        $page = wire('page');
        $page->average_rating = $page->product_comments->averageRating;    
    }
}

Still no dice. Don't get any errors either.

Link to comment
Share on other sites

What you mean with no dice. It's not about luck or dices :D

What you expect to do it? Is average_rating a field? Where is averageRating coming from? And don't you need to save the page or field after setting it?

  • Like 1
Link to comment
Share on other sites

It's working! :biggrin:

I was missing $page->setOutputFormatting(false);  and $page->save();

Working module:

<?php

class PageAverageRating extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Copy average rating into hidden field',
            'version' => 100,
            'summary' => 'Copies the average rating to another field',
            'singular' => true,
            'autoload' => true,
        );
    }

    public function init() {
        
       $this->addHookAfter("CommentFormWithRatings::processRatingInput", $this, 'copyAverageRating');

    }

    public function copyAverageRating() {
        
        $page = wire('page'); // get access to the page
      
        $page->setOutputFormatting(false); // without this we can't save the page later on
        
        // we're interested in product pages only:
        if($page->template->name != 'product') return;
 
        // use the existing averageRating method to get the average rating and store it in the field
        // we created called average_rating:
        $page->average_rating = $page->product_comments->averageRating;
        $page->save();
        
    }
}

Many thanks Antti, Soma and of course Ryan, you guys rock. This is finally starting to make sense for me.

I think i will write a guide to help people like me get started with module development.

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