ProcessWire - Hooks: Unterschied zwischen den Versionen
| Zeile 89: | Zeile 89: | ||
=== Custom Hook Methode === | === Custom Hook Methode === | ||
| + | Beispiel aus der ready.php | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
/** @var ProcessWire $wire */ | /** @var ProcessWire $wire */ | ||
Version vom 13. Februar 2020, 20:09 Uhr
Wo setzt man hooks am Besten ein
Es gibt einige Standard Dateien die für solche Zwecke geeignet sind:
/site/init.php
PW Boot -> Module mit autoload -> init.php
This file is included during ProcessWire's boot initialization, immediately after autoload modules have been loaded and had their init() methods called. Anything you do in here will behave the same as an init() method on a module. When this file is called, the current $page has not yet been determined. This is an excellent place to attach hooks that don't need to know anything about the current page.
/site/ready.php
API geladen und ready -> $page bereit aber noch nicht gerendert
This file is included immediately after the API is fully ready. It behaves the same as a ready() method in an autoload module. The current $page has been determined, but not yet rendered. This is an excellent place to attach hooks that may need to know something about the current page. It's also an excellent place to include additional classes or libraries that will be used on all pages in your site.
/site/finished.php
Seite gerendert
This file is included when ProcessWire has finished rendering and delivering a page, and is in the process of shutting down. It is called immediately before the API is disengaged, so you can still access any API variable and update $session values as needed. Admittedly, this is probably not the place you would put hooks, but it is an ideal place to perform your own shutdown, should your application call for it.
Beispiele für Hooks
Basic Starter Hook
$wire->addHookAfter('Pages::added', function(HookEvent $event) {
$page = $event->arguments(0);
bd($page); // tracy debugger output
});
// more specific
$wire->addHookAfter('Pages::added(template=basic-page)', function(HookEvent $event) {
$page = $event->arguments(0);
bd($page);
});
Feld manipulieren
Beim Speichern einer Seite ein Feld anpassen
$pages->addHookAfter('saveReady', function(HookEvent $event) {
$page = $event->arguments(0);
// If the page has the relevant template...
if($page->template == 'oe') {
// make title uppercase
$page->name = strtoupper($page->name);
}
});
Seiten im Backend sortieren
/*sort clients of key account manager by title in backend*/
$pages->addHookAfter('saveReady', function(HookEvent $event) {
$page = $event->arguments(0);
// If the page has the relevant template...
if($page->template == 'key_account_manager') {
// Sort the Page Reference field by title
$page->pr_kam_clients->sort('title') ;
}
});
Externe Klasse verfügbar machen
/site/ready.php
<?php
$pages->addHookAfter('saved', function($event) {
$page = $event->object;
if($page->template == 'product') {
// update other related pages when 'product' page is saved
}
});
if($page->template != 'admin') {
// include an external helper class...
require('./classes/ProductCart.php');
// ...and establish it as a new $cart API variable, and
// populate it with products saved in the user's session:
$wire->wire('cart', new ProductCart($session->productsInCart));
}
There is also a /site/finished.php that obtains the products from the $cart and saves them back to the session, ready for the next request:
/site/finished.php
<?php
if($page->template != 'admin') {
$session->productsInCart = $cart->getArray();
}
Custom Hook Methode
Beispiel aus der ready.php
/** @var ProcessWire $wire */
/**
* Example of a custom hook method
*
* This hook adds a “numPosts” method to pages using template “category”.
* The return value is the quantity of posts in category.
*
* Usage:
* ~~~~~
* $numPosts = $page->numPosts(); // returns integer
* numPosts = $page->numPosts(true); // returns string like "5 posts"
* ~~~~~
*
*/
$wire->addHook('Page(template=category)::numPosts', function($event) {
/** @var Page $page */
$page = $event->object;
// only category pages have numPosts
if($page->template != 'category') return;
// find number of posts
$numPosts = $event->pages->count("template=blog-post, categories=$page");
if($event->arguments(0) === true) {
// if true argument was specified, format it as a "5 posts" type string
$numPosts = sprintf(_n('%d post', '%d posts', $numPosts), $numPosts);
}
$event->return = $numPosts;
});