ProcessWire - Module schreiben
Aus Wikizone
Version vom 16. Dezember 2019, 19:46 Uhr von 84.136.104.26 (Diskussion)
Beispiele
ProcessWire Hello World Modul
Beispiel Modul mit Hooks (liegt immer in der Standardinstallation)
<?php namespace ProcessWire;
/**
* ProcessWire 'Hello world' demonstration module
*
* Demonstrates the Module interface and how to add hooks.
*
* See README file for further links regarding module development.
*
* This file is licensed under the MIT license
* https://processwire.com/about/license/mit/
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* https://processwire.com
*
*/
class Helloworld extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
// The module's title, typically a little more descriptive than the class name
'title' => 'Hello World',
// version number
'version' => 3,
// summary is brief description of what this module is
'summary' => 'An example module used for demonstration purposes.',
// Optional URL to more information about the module
'href' => 'https://processwire.com',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => true,
// Optional font-awesome icon name, minus the 'fa-' part
'icon' => 'smile-o',
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// add a hook after the $pages->save, to issue a notice every time a page is saved
$this->pages->addHookAfter('save', $this, 'example1');
// add a hook after each page is rendered and modify the output
$this->addHookAfter('Page::render', $this, 'example2');
// add a 'hello' method to every page that returns "Hello World"
// use "echo $page->hello();" in your template file to display output
$this->addHook('Page::hello', $this, 'example3');
// add a 'hello_world' property to every page that returns "Hello [user]"
// use "echo $page->hello_world;" in your template file to display output
$this->addHookProperty('Page::hello_world', $this, 'example4');
}
/**
* Example1 hooks into the pages->save method and displays a notice every time a page is saved
*
* @param HookEvent $event
*
*/
public function example1($event) {
/** @var Page $page */
$page = $event->arguments[0];
$this->message("Hello World! You saved {$page->path}.");
}
/**
* Example2 hooks into every page after it's rendered and adds "Hello World" text at the bottom
*
* @param HookEvent $event
*
*/
public function example2($event) {
/** @var Page $page */
$page = $event->object;
// don't add this to the admin pages
if($page->template == 'admin') return;
// add a "Hello World" paragraph right before the closing body tag
$event->return = str_replace("</body>", "<p>Hello World!</p></body>", $event->return);
}
/**
* Example3 adds a 'hello' method (not property) to every page that simply returns "Hello World"
*
* @param HookEvent $event
*
*/
public function example3($event) {
$event->return = "Hello World";
}
/**
* Example 4 adds a 'hello_world' property (not method) to every page that returns "Hello [user]"
*
* @param HookEvent $event
*
*/
public function example4($event) {
$event->return = "Hello " . $this->user->name;
}
}
ProcessWire Admin Bereich mit eigener Funktionalität erweitern (Modul)
https://webdesign.tutsplus.com/tutorials/extending-the-processwire-admin-using-custom-modules--cms-26863
Skip to content
Why GitHub?
Enterprise
Explore
Marketplace
Pricing
Search
Sign in
Sign up
282benbyford/PW-intermediate-modules
Code Issues 0 Pull requests 0 Projects 0 Security Insights
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
PW-intermediate-modules/ProcessSimpleAdminPage/ProcessSimpleAdminPage.module
@benbyford benbyford added if staement for uninstall page
62547ec on 15 Aug 2016
77 lines (60 sloc) 3.73 KB
<?php
/**
* ProcessSimpleAdminPage
*
* @author Ben Byford
* http://www.benbyford.com
*
* @see http://www.processwire.com
*/
class ProcessSimpleAdminPage extends Process {
public static function getModuleInfo() {
return array(
'title' => 'Process Simple Admin Page',
'summary' => 'Simple Process module that adds new admin page with',
'version' => 001,
// Modules that extend Process may specify a 'page' attribute in the
// getModuleInfo(), this page will automatically be given the module
// process when added to teh pagetree.
// I have exampled but commented out the 'page' settings below
// so that I can show how one might add a page to install() and
// uninstall() in this and other modules (that might not extend
// Process)
// 'page' => array(
// 'name' => 'site-config',
// 'parent' => 'admin',
// 'title' => 'Site Config'
// )
);
}
public function execute() {
return '
<h2>Edit the text here in the module</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mattis eros vitae metus sodales eget suscipit purus rhoncus. Proin ultrices gravida dolor, non porttitor enim interdum vitae. Integer feugiat lacinia tincidunt. Nulla laoreet tristique tristique. Sed elementum justo a nisl elementum sit amet accumsan nisi tempor. Nulla quis eros et massa dignissim imperdiet a vitae purus.</p>
<p>Donec scelerisque pulvinar sem eu lobortis. Maecenas turpis ipsum, tempus dictum pharetra eu, consectetur vitae arcu. Fusce orci mauris, semper at tempus quis, volutpat molestie tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed quam tortor, tincidunt sed semper lacinia, scelerisque dapibus quam. Morbi at nisi luctus lacus auctor ultrices eu eu leo.</p>
<p>Praesent faucibus purus id felis tincidunt dignissim. Sed sit amet ligula mi, eget semper dui. Proin consectetur gravida massa, nec luctus purus hendrerit in. Etiam volutpat, elit non venenatis suscipit, libero neque consectetur diam, id rutrum magna odio ac ligula. Maecenas sollicitudin congue neque fermentum vestibulum. Morbi nec leo nisi. Donec at nisl odio, et porta ligula.</p>
<p>Sed quis arcu nisi, ac tempor augue. Praesent non elit libero, a ullamcorper lorem. Curabitur porta odio eu nunc ultricies interdum id nec risus. Donec nibh nibh, porta eget vehicula ac, aliquet eget ante. Phasellus eget lorem eu eros eleifend ultrices. Cras sit amet neque sit amet nibh fringilla cursus ut id mauris. Praesent quis nunc justo, sed suscipit lectus. Phasellus eget ultrices risus. Curabitur eu semper est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut suscipit, nisl ut imperdiet eleifend, turpis arcu placerat tortor, nec laoreet lacus neque ac tellus. Aenean ac lacus justo, quis ultricies nisi.</p>
';
}
public function install(){
// create new page to add to CMS
$page = new Page();
// add page attributes
$page->template = "admin";
$page->name = "cms-faq";
$page->title = "CMS FAQ";
$page->save();
// set this module as the page process, this allows us to display the above
$page->process = 'ProcessSimpleAdminPage';
// get admin page and set as page parent
$admin = $this->pages->get("id=2");
$page->parent = $admin;
// save page
$page->save();
}
public function uninstall(){
// delete created page
$page = $this->pages->get("name=cms-faq");
if(count($page)) $this->pages->delete($page, true);
}
}