Jump to content

Getting started with hooks


deltavik
 Share

Recommended Posts

So I thought of hooking up with hooks. I installed that "hello world" module, and now, when I save a page, I see "Hello world..." message on top -- which makes sense. To play with it further, I copied the hook code from the module to my header.php file.

The code is: 

$pages->addHookAfter('save', $this, 'example1');

function example1($event) {
    $page = $event->arguments[0];
    $this->message("Hello World 2 ! You saved {$page->path}.");
}

It is a verbatim copy of the code from the module, and it is placed in a file that gets called first via prependTemplateFile in config.php. 

However, now when I save a page, I don't see the message from my hook function. What could I be missing?

thanks,

Link to comment
Share on other sites

What say the doc :

Quote

The $config->prependTemplateFile is not used by the ProcessWire admin, so if you are defining hooks specific to the ProcessWire admin, you should define them at the very top of the /site/templates/admin.php. If your hooks are applicable to both your site (front-end) and the ProcessWire admin, then you should manually include your sites initialization fie, i.e. include("./_init.php"); from your /site/templates/admin.php file.

 

also $this is not callable in this context as you're not in an object.

In order to get the hook working, you should write the hook in _init.php  :

function example1(HookEvent $event) {
    $page = $event->arguments[0];
    wire('session')->message("Hello World 2 ! You saved {$page->path}");
}
$pages->addHookAfter('Pages::save', null, 'example1');

And in admin.php, add :

include("./_init.php");

 

Now go to in backend, and save a page, the message 'Hello World 2 ! You saved /your/page/' should appear.

 

  • Like 2
Link to comment
Share on other sites

  • 4 years later...

Thanks, this was helpful for me. Just one notice:

To get out the message, "wire('session')" was not working for me, I used $this instead:

function example1(HookEvent $event) {
    $page = $event->arguments[0];
    $this->message("Hello World 2 ! You saved {$page->path}");
}
$pages->addHookAfter('Pages::save', null, 'example1');

 

  • 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 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.
       
    • By Kiwi Chris
      I need to restrict editing access based on both the user role, and the value of a sub-field in a page reference field.
      eg $page->competition->closingDate 
      There's some discussion about restricting editing access based on role, and that works, but when I try to access the page via the method @Robin S suggests, the page returned is an admin page rather than the page being edited, so I don't have access to the edited page's fields.
       
    • By picarica
      so i am trying to put CustomHooksForVariations.module, a custom module, i am placing it into site/modules direcotry yet my modules page in admin panel gives me errors
      so this is the screen show when i refresh modules, i dont know why the shole hook is written on top of the page :||

      and this next image is when i try to install it, i saw that it is not defiuned modules.php but it shouldnt need to be ?, any ways i dont want to edit site's core just to make one moulde work there has to be a way

    • By Andi
      Continuing my journey into PW hooks, I'm trying to find a way to retrieve all images from a page that explicitly *do not* have a certain tag (or tags) attached to them.
      Found this post from 2015
      But I'm wondering if there's a more elegant way to go about this.
      Let's say I have a multi-image field called "images_header" and instead of
      $page->images_header->findTag('mytag'); I would like to do this:
      $page->images_header->excludeTag('mytag'); So I'd be able to do
      // find images that don't have the tag "mytag" $images = $page->images_header->excludeTag('mytag'); // check if there's any images if (count($images)>0) { // do something.. } Would this be possible by hooking into Pagefiles somehow?
      There's this bit in /wire/core/Pagefiles.php Line 626 that I'd basically just need to reverse (or at least in my mind 😄 )
      public function findTag($tag) { $items = $this->makeNew(); foreach($this as $pagefile) { if($pagefile->hasTag($tag)) $items->add($pagefile); } return $items; } Any ideas on how this could be done in a graceful manner?
      Thanks in advance!
    • By Andi
      Getting a little deeper into the ProcessWire state-of-mind here. I seriously think I wouldn't have come back to webdev if it wasn't for this wonderful little gem of a CMS.
      I have an "Options" field added to all users on a site. If the user has anything other then "default" selected, I would like to show a permanent message in the admin like the one in the screenshot, only so that the user can't close it. As a friendly reminder that he changed that option from default to something crazy 🙂
      I've read up on how to send messages to users, but where would I hook into to make this show up all the time in the backend?
      https://processwire.com/api/ref/wire/message/
      Thanks in advance!

×
×
  • Create New...