ProcessWire - Module Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 205: Zeile 205:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== User Interface im Admin Bereich - Helferlein ==
 +
=== Buttons ===
 +
''Händisch''
 +
<a href="./link-to-page/" class="ui-button ui-state-default"> My Button </a>
 +
 +
''Programmatisch''
 +
Nützlich um z.b. was abzufragen.
 +
<pre>
 +
if(!$this->input->get('modal')) {
 +
    $button = $this->modules->get('InputfieldButton');
 +
    $button->value = 'Open Page in Panel';
 +
    $button->attr('data-href', './table');
 +
    $button->addClass('pw-panel');
 +
    $out .= $button->render();
 +
  }
 +
</pre>
  
 
=== AdminTable ===
 
=== AdminTable ===
Zeile 214: Zeile 231:
 
   return $table->render();
 
   return $table->render();
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=== Seiten in Panel öffnen ===
 +
Man kann Unterseiten in einem Panel aufmachen. Einfach die '''Klasse pw-panel zu''' Links hinzufügen.
 +
<a href="./due-contracts/" class="ui-button ui-state-default pw-panel"> Fällige Verträge </a>
 +
Mehr Infos im Quelltext (suche nach panel)
 +
https://github.com/processwire/processwire/blob/master/wire/modules/Jquery/JqueryUI/panel.js

Version vom 31. Januar 2021, 07:43 Uhr

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/
https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0 (in multiinstance modules)
<?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 Module

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); 
		}
	}
 $filesPath = $page->filesManager->path;

User Interface im Admin Bereich - Helferlein

Buttons

Händisch

<a href="./link-to-page/" class="ui-button ui-state-default"> My Button </a>

Programmatisch Nützlich um z.b. was abzufragen.

if(!$this->input->get('modal')) {
    $button = $this->modules->get('InputfieldButton');
    $button->value = 'Open Page in Panel';
    $button->attr('data-href', './table');
    $button->addClass('pw-panel');
    $out .= $button->render();
  }

AdminTable

  $table = $this->modules->get('MarkupAdminDataTable');
  $table->headerRow(['A', 'B', 'C']);
  $table->row([1, 2, 3]);
  $table->row([4, 5, 6]);
  return $table->render();

Seiten in Panel öffnen

Man kann Unterseiten in einem Panel aufmachen. Einfach die Klasse pw-panel zu Links hinzufügen.

<a href="./due-contracts/" class="ui-button ui-state-default pw-panel"> Fällige Verträge </a>

Mehr Infos im Quelltext (suche nach panel)

https://github.com/processwire/processwire/blob/master/wire/modules/Jquery/JqueryUI/panel.js