Processwire - API: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
(2 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
== Links ==
 +
https://processwire.com/api/ref/
 +
 
== Allgemeine Tipps ==
 
== Allgemeine Tipps ==
 
* Wenn kein Zugriff auf das Pages Objekt möglich ist kann man über wire()->pages auf die Funktionen zugreifen.
 
* Wenn kein Zugriff auf das Pages Objekt möglich ist kann man über wire()->pages auf die Funktionen zugreifen.
  
== ProcessWire - Seiten löschen ==
+
== ProcessWire API - Page Manipulation ==
https://processwire.com/api/ref/page/delete/g/
+
[[ProcessWire API - Page Manipulation]]
<syntaxhighlight lang="php">
 
// Delete pages named "delete-me" that don't have children
 
$items = $pages->find("name=delete-me, numChildren=0");
 
foreach($items as $item) {
 
  $item->delete();
 
}
 
</syntaxhighlight>
 
 
 
== Seiten via API modifizieren ==
 
<syntaxhighlight lang="php">
 
// Modify a page and save it
 
$p = $pages->get('/festivals/decatur/beer/');
 
$p->of(false); // turn off output formatting, if it's on
 
$p->title = "Decatur Beer Festival";
 
$p->summary = "Come and enjoy fine beer and good company at the Decatur Beer Festival.";
 
$pages->save($p);
 
</syntaxhighlight>
 
 
 
== Seiten via API erstellen ==
 
https://processwire.com/api/ref/pages/save/
 
https://processwire.com/talk/topic/352-creating-pages-via-api/
 
 
 
=== Allgemein ===
 
<syntaxhighlight lang="php">
 
$p = new Page();
 
$p->template = "basic-page";
 
$p->parent = 1;
 
$p->title = "Page Title";
 
//$p->id = 4254; Only if you want to set this manually
 
$p->of(false);
 
$p->save();
 
</syntaxhighlight>
 
 
 
=== In Modulen ===
 
Siehe auch: https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0
 
<syntaxhighlight lang="php">
 
include(./index.php) // bootstrap PW (not needed when creating pages and your already in bootstraped mode (i.e. on a page template)
 
 
 
$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();
+
== ProcessWire API Variable ==
 +
ProcessWire hat einige nützliche Variablen die man ständig benötigt. Außerdem kann man außer über eine Variable auf viele Objekte auch als Funktion zugreifen.
 +
Wichtig sind z.B. folgende Objekte
 +
Page
 +
Pages
 +
Config
 +
Wire
 +
User
 +
=== Zugriff auf Variablen ===
 +
Es gibt unterschiedliche Wege mit unterschiedlichen Vor- und Nachteilen auf API Variablen zuzugreifen.
 +
https://processwire.com/docs/start/variables/
  
// testing
+
$page the most common access as a variable.
 +
page() accessing as a function can be very convenient, when available.
 +
wire('page') This works anywhere, and with any version of ProcessWire.
 +
$this->page This is how you might access it from within a Wire-derived class.
 +
$this->wire('page') A slightly more efficient way you can access from within a Wire-derived class.
 +
$pages->wire('page') API variables can also access all other API variables, by way of their wire() method. In this example, we are accessing the $page API variable from the $pages API variable. So if you have any one API variable, then you actually have them all.
  
echo 'id: '.$p->id.'<br/>';
+
Accessing API variables as a function rather than a variable can be more convenient when the variable version may be out of scope, or if your IDE highlights API variables as undefined. So whenever we refer to an API variable, make note that access it however you'd like.
echo 'path: '.$p->path;
 
</syntaxhighlight>
 
  
=== Tipps ===
+
This is just an introduction to some of ProcessWire's API variables. Once you are familiar with these, you'll want to use ProcessWire's full API reference instead.
* save a page before '''adding files''' to it
 
* To '''add a single image''', you'd need to do ''$p->image = 'path/to/image.jpg'''; rather than ''$p->image('path/to/image.jpg')''. This is because there is no Page::image() function
 
* may need to add a '''$page->setOutputFormatting(false)'''
 
* if your module creates new objects, like ''$page = new Page();'', you'll want to wire those new objects to the current ProcessWire instance, which makes it a "wired" object. You do it like this: ''$page = $this->wire(new Page());''
 

Aktuelle Version vom 15. August 2021, 07:42 Uhr

Links[Bearbeiten]

https://processwire.com/api/ref/

Allgemeine Tipps[Bearbeiten]

  • Wenn kein Zugriff auf das Pages Objekt möglich ist kann man über wire()->pages auf die Funktionen zugreifen.

ProcessWire API - Page Manipulation[Bearbeiten]

ProcessWire API - Page Manipulation

ProcessWire API Variable[Bearbeiten]

ProcessWire hat einige nützliche Variablen die man ständig benötigt. Außerdem kann man außer über eine Variable auf viele Objekte auch als Funktion zugreifen. Wichtig sind z.B. folgende Objekte

Page
Pages
Config
Wire
User

Zugriff auf Variablen[Bearbeiten]

Es gibt unterschiedliche Wege mit unterschiedlichen Vor- und Nachteilen auf API Variablen zuzugreifen.

https://processwire.com/docs/start/variables/
$page the most common access as a variable.
page() accessing as a function can be very convenient, when available.
wire('page') This works anywhere, and with any version of ProcessWire.
$this->page This is how you might access it from within a Wire-derived class.
$this->wire('page') A slightly more efficient way you can access from within a Wire-derived class.
$pages->wire('page') API variables can also access all other API variables, by way of their wire() method. In this example, we are accessing the $page API variable from the $pages API variable. So if you have any one API variable, then you actually have them all.

Accessing API variables as a function rather than a variable can be more convenient when the variable version may be out of scope, or if your IDE highlights API variables as undefined. So whenever we refer to an API variable, make note that access it however you'd like.

This is just an introduction to some of ProcessWire's API variables. Once you are familiar with these, you'll want to use ProcessWire's full API reference instead.