ProcessWire - Module Snippets
Aus Wikizone
Nützliche Snippets für das Schreiben von Modulen
Siehe auch
ProcessWire - Module schreiben
Grundrezepte
Adminseite erstellen (Non Process Module)
Das benötigt man NICHT IN PROCESS MODULEN. Da funktioniert das auch ohne manuelles erstellen.
Here is a short example on how you could do it (of course there are a lot of different ways to archive this):
public function ___install() {
// Creates a new Page object
$page = new Page();
// use your own selector to choose the parent page
$parent = $this->pages->get("name=setup");
$page->parent = $parent;
// choose a template
$page->template = $this->templates->get('admin');
// name and title
$page->name = "your-page";
$page->title = "Your Page";
// .. and save the new page object as a real page
$page->save();
// success message
$this->message(sprintf($this->_("Installed to %s"), $page->path));
}
public function ___uninstall() {
$parent = $this->pages->get("name=setup");
// the name has to be the same as defined above
// you could use a constant variable for this
$page = $parent->child("name=your-page");
// only delete page if existing
if($page->id) {
$this->pages->delete($page);
$this->message(sprintf($this->_("Removed %s"), $page->path));
}
}
</syntaxhighlights>
== Diverses ==
=== ProcessWire Version testen ===
<syntaxhighlight lang="php">
public function ___install()
{
if (ProcessWire::versionMajor == 2 && ProcessWire::versionMinor < 1) {
throw new WireException("This module requires ProcessWire 2.1 or newer");
}
//...
Testen ob ein bestimmtes Modul installiert ist
if (!$this->modules->isInstalled('Tasker')) {
$this->message('Tasker module is missing. Install it before using Dataset module.');
return;
}
$this->tasker = $this->modules->get('Tasker');
Seiten erstellen
https://processwire.com/talk/topic/352-creating-pages-via-api/
<?php
include(./index.php) // bootstrap PW
$p = new Page(); // create new page object
$p->template = 'page'; // set template
$p->parent = wire('pages')->get('/about/'); // set the parent
$p->name = 'mynewpage_url'; // give it a name used in the url for the page
$p->title = 'My New Page'; // set page title (not neccessary but recommended)
// added by Ryan: save page in preparation for adding files (#1)
$p->save();
// populate fields
$p->image = 'path/to/image.jpg'; // populate a single image field (#2)
$p->images->add('path/to/image1.jpg'); // add multiple to images field
$p->save();
// testing
echo 'id: '.$p->id.'<br/>';
echo 'path: '.$p->path;
You may need to add a
$page->setOutputFormatting(false)
depending on the context and if PW gives you an error about needing to turn off output formatting.
You can override a page by using id:
$p = new Page();
$p->template = "basic-page";
$p->parent = 1;
$p->title = "Page Title";
$p->id = 4254;
$p->of(false);
$p->save();
With
adjustName=>true
and not using name PW will create a unique name. Just omitting name might work too.
Get Unique Pagename
/**
* Given a page name, check that it is unique and return it or a unique numbered variation of it
*
*/
protected function getUniquePageName($pageName) {
$n = 0;
do {
$testName = $pageName . "-" . (++$n);
$test = $this->parent->child("name=$testName, include=all");
if(!$test->id) break;
} while(1);
return $testName;
}
Selektoren
Einfacher Check auf gültigen Selector
// check the page selector
if (strlen($selector)<2 || !strpos($selector, '=')) {
$this->error("ERROR: invalid page selector '{$selector}' found in the input.");
return false;
}
Seite im Admin Modulen
Wenn keine Seite angelegt ist wird sie angelegt. Nützlich z.B. für die ___install() und ___uninstall() Methoden
/**
* Return the page that this Process is installed on
*
*/
protected function getInstalledPage() {
$admin = $this->pages->get($this->config->adminRootPageID);
$parent = $admin->child("name=setup");
if(!$parent->id) $parent = $admin;
$page = $parent->child("name=" . self::adminPageName);
if(!$page->id) {
$page = new Page();
$page->parent = $parent;
$page->template = $this->templates->get('admin');
$page->name = self::adminPageName;
$page->title = "Import Pages From CSV";
$page->process = $this;
$page->sort = $parent->numChildren;
$page->save();
}
return $page;
}
/**
* Install the module and create the page where it lives
*
*/
public function ___install() {
if(ProcessWire::versionMajor == 2 && ProcessWire::versionMinor < 1) {
throw new WireException("This module requires ProcessWire 2.1 or newer");
}
$page = $this->getInstalledPage();
$this->message("Installed to {$page->path}");
if($page->parent->name == 'setup') $this->message("Click to your 'Setup' page to start using the CSV Importer");
}
/**
* Uninstall the module
*
*/
public function ___uninstall() {
$page = $this->getInstalledPage();
if($page->id) {
$this->message("Removed {$page->path}");
$this->pages->delete($page);
}
}
=== Pfad zu Seitedateien ===
$filesPath = $page->filesManager->path;