ProcessWire - Module Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 5: Zeile 5:
  
 
== Diverses ==
 
== 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");
 +
        }
 +
//...
 +
</syntaxhighlight>
 +
 
=== Testen ob ein bestimmtes Modul installiert ist ===
 
=== Testen ob ein bestimmtes Modul installiert ist ===
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
Zeile 32: Zeile 43:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
== Selektoren ==
 
== Selektoren ==
 
=== Einfacher Check auf gültigen Selector ===
 
=== Einfacher Check auf gültigen Selector ===

Version vom 14. Januar 2020, 11:08 Uhr

Nützliche Snippets für das Schreiben von Modulen

Siehe auch

ProcessWire - Module schreiben

Diverses

ProcessWire Version testen

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');

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;