ProcessWire - Module Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 208: Zeile 208:
 
== User Interface im Admin Bereich - Helferlein ==
 
== User Interface im Admin Bereich - Helferlein ==
 
=== Buttons ===
 
=== Buttons ===
''Händisch''
+
''Händisch'' Hier noch mit einer Abfrage ob man im Modal ist um Modals in Modals zu verhindern.
 +
<pre>
 +
if(!$this->input->get('modal')) {
 
  <a href="./link-to-page/" class="ui-button ui-state-default"> My Button </a>
 
  <a href="./link-to-page/" class="ui-button ui-state-default"> My Button </a>
 +
}
 +
</pre>
  
 
''Programmatisch''
 
''Programmatisch''
Nützlich um z.b. was abzufragen.
+
Nützlich um ganze Sets zu erzeugen.
<pre>
+
 
if(!$this->input->get('modal')) {
+
<syntaxhighlight lang="php">
     $button = $this->modules->get('InputfieldButton');
+
      $out = '';
    $button->value = 'Open Page in Panel';
+
   
     $button->attr('data-href', './table');
+
      $buttons = [
    $button->addClass('pw-panel');
+
        'randomizeData' => 'Randomize Data',
    $out .= $button->render();
+
        'addDataset' => 'Add Dataset',
  }
+
        'addData' => 'Add Data',
</pre>
+
        'removeDataset' => 'Remove Dataset',
 +
        'removeData' => 'Remove Data',
 +
      ];
 +
      
 +
      $button = $this->modules->get('InputfieldButton');
 +
      $button->setSmall(true);
 +
      
 +
      foreach($buttons as $id => $label) {
 +
        $button->id = $id;
 +
        $button->value = $label;
 +
        $out .= $button->render();
 +
      }
 +
</syntaxhighlight>
  
 
=== AdminTable ===
 
=== AdminTable ===

Version vom 31. Januar 2021, 08:00 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 Hier noch mit einer Abfrage ob man im Modal ist um Modals in Modals zu verhindern.

if(!$this->input->get('modal')) {
 <a href="./link-to-page/" class="ui-button ui-state-default"> My Button </a>
}

Programmatisch Nützlich um ganze Sets zu erzeugen.

      $out = '';
    
      $buttons = [
        'randomizeData' => 'Randomize Data',
        'addDataset' => 'Add Dataset',
        'addData' => 'Add Data',
        'removeDataset' => 'Remove Dataset',
        'removeData' => 'Remove Data',
      ];
    
      $button = $this->modules->get('InputfieldButton');
      $button->setSmall(true);
    
      foreach($buttons as $id => $label) {
        $button->id = $id;
        $button->value = $label;
        $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

Skripte hinzufügen

public function ___executeChart() {
  $this->config->scripts->add(
    'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js'
  );
  $this->config->scripts->add(
    'http://www.chartjs.org/samples/latest/utils.js'
  );
  $this->config->scripts->add(
    $this->config->urls->ProcessSimple . 'chart.js'
  );

  $out = '';

  $buttons = [
    'randomizeData' => 'Randomize Data',
    'addDataset' => 'Add Dataset',
    'addData' => 'Add Data',
    'removeDataset' => 'Remove Dataset',
    'removeData' => 'Remove Data',
  ];

  $button = $this->modules->get('InputfieldButton');
  $button->setSmall(true);

  foreach($buttons as $id => $label) {
    $button->id = $id;
    $button->value = $label;
    $out .= $button->render();
  }

  return $out;
}