Jump to content

Hooking ProcessPageView


wumbo
 Share

Recommended Posts

Hi there!

I'm using ProcessWire 3 and I'd like to build a module that tabs into the ProcessPageView::execute hook.
Currently I've built a small module that defines the hooking in its init() method as usual.

public static function getModuleInfo()
{
    return [
	    'title' => 'MicrositeRouting',
        'version' => 001,
        'summary' => 'Routing logic for microsites',
        'requires' => 'ProcessWire>=3.0.14',
        'autoload' => 'template!=admin',
        'singular' => true
    ];
}

public function init()
{
    $this->wire('log')->message(__METHOD__);
    $this->addHookBefore('ProcessPageView::execute', $this, 'lookUpPage');        
}

public function lookUpPage(HookEvent $event)
{
    $this->wire('log')->message(__METHOD__);
}

I provided some log messages to check if the module works. But it seems that in spite of the module being initialized properly (the message is found in my logs), the hook itself isn't processed at all (the log doesn't show the corresponding message).

I surely missed something important but currently can't find it.

Thank you for any suggestions. 

Link to comment
Share on other sites

I think the issue lies with the autoload condition:

9 hours ago, wumbo said:

'autoload' => 'template!=admin',

If this is changed to...

'autoload' => true,

...then it works for me. If you change this, do a refresh in Modules to clear the cache.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 3 years later...

For the sake of completeness. Hooks in ProcessPageView are also possible if the hook is placed in the /site/init.php file.

Example:

// pick up $_GET['it'] (requested uri) before unset
wire()->addHookBefore('ProcessPageView::execute', function($e) {
    var_dump($_GET['it']);
});

 

  • Like 1
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.
  • Similar Content

    • By FireWire
      Hey all,
      I've been building a new feature on our PW site and the URL hooks are exactly what the doctor ordered. I am developing a RESTful web API to allow external platforms to integrate with our site. The API endpoints are namespaced at /api/v1. I am having trouble making use of the named route parameter using regex.
      I have a hook for /api/v1/offline-events that gets all Offline Event pages. The next route to be defined is /api/v1/offline-events/{event ID}. The event ID is using a custom string (not the Page ID) that is a 15 character alphanumeric string like this: E4LxQRXZadgjstw. However when I use a PCRE compliant regular expression (tested on regexr.com) I get an error.
      Here is my hook code for reference and the error produced:
      <?php namespace ProcessWire; $wire->addHook('/api/v1/offline-events/{eventId:#(?=.{15}$)([A-Za-z0-9])\w+#}/?', function($e) { // ommitted for brevity }); // Error thrown: Warning: preg_match(): Unknown modifier '(' in /wire/core/WireHooks.php on line 1170 I've tried using all delimiters in the documentation (!@#%) but no change in the outcome.
      The second question is how do I create a wildcard capture for all 404s that occur after the /api endpoint? Currently it is loading the website 404 page.
      Many thanks!
    • By nuel
      Hi
      I'm working with PW for some time now, but – please don't laugh – never used hooks.
      I have 200 – 300 subpages with the template 'artist', the title of each page represents the full name, sometimes John Doe, sometimes John Emmet Brown Doe. I want to auto-populate the field 'artist_last_name' with the word that is most likely the last name, the last word of the page title. If there are more names, it can be changed manually. I need this for easier and quicker alphabetical sorting.
      What I put together is this, in ready.php. Doesn't work. Again, I'm new to hooks..
      <?php     $wire->addHookBefore('Pages::saved', function(HookEvent $event) {     $p = $event->arguments('page');     if($p->template != 'artist') return;     if($p->artist_last_name) return;     $ln = end(explode(' ', $p->title));     $p->set('artist_last_name',$ln);     }); ?> In the best case this would be done once globally without having to open and save every page. But new pages should populate the field on page save.
      Thanks for your help, I think it's easy, I just need a push..
      Nuél
    • By spercy16
      I was previously using individual fields to capture and output multiple cards onto my web page but after recently learning about repeaters would prefer to use this field type, as it will be a lot more user friendly for end users to create new cards. I tried to modify my code to grab the fields from the new Repeater field(s) but am getting this error message on the page:
      Here is the code I'm currently trying to use for the cards:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items foreach($page->tall_card as $card) { $count = 0; $count++; $options = array( 'quality' => 70 ); $image = $card->image; $img = $card->image->size(550, 400, $options); $cardHeading = $card->title; $cardBody = $card->plain_paragraph; $raised = $card->raised_amount; $goal = $card->goal_amount; $link = $card->link; if ($raised == 0 ) : $percent = 0; else: $percent = $raised_amount / $goal_amount * 100; endif; if ($percent <= 5) : $percent = 0; endif; ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $cardHeading; ?></h2> <?php echo $cardBody; ?> </div> <div class="progressBarSection"> <progress class="progressBar" id="progressbar<?php echo $count; ?>" value="<?php echo $percent; ?>" max="100"></progress> <span class="raisedAmount"> $<?php echo $raised; ?> usd </span> <span class="goalAmount"> $<?php echo $goal; ?> usd </span> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $link; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php } ?> Thanks in advance for any help!
    • By CachePuzzles
      Hi there,
      I'm probably missing something simple, but I'm just setting up my first site so I guess that's bound to happen.
      I've installed the LoginRegistration module and at first I was thinking that the confirmation emails weren't being sent. However I now see that new user confirmation emails are taking 20+ minutes to arrive. Is there a way to speed this up?
      Also the email is coming from processwire@mydomain - I couldn't found a place to change this.
      Any advice or help would be great!
      Brian
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
×
×
  • Create New...