Processwire - API: Unterschied zwischen den Versionen
Aus Wikizone
Steff (Diskussion | Beiträge) |
Steff (Diskussion | Beiträge) |
||
| Zeile 12: | Zeile 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | == Seiten erstellen == | + | == Seiten via API erstellen == |
https://processwire.com/talk/topic/352-creating-pages-via-api/ | https://processwire.com/talk/topic/352-creating-pages-via-api/ | ||
| + | === Allgemein === | ||
| + | <syntaxhighlight lang="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; | ||
| + | </syntaxhighlight> | ||
=== Tipps === | === Tipps === | ||
* save a page before '''adding files''' to it | * 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 | * 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)''' | * 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());'' | ||
Version vom 29. Oktober 2018, 15:33 Uhr
Allgemeine Tipps
- Wenn kein Zugriff auf das Pages Objekt möglich ist kann man über wire()->pages auf die Funktionen zugreifen.
ProcessWire - Seiten löschen
https://processwire.com/api/ref/page/delete/g/
// Delete pages named "delete-me" that don't have children
$items = $pages->find("name=delete-me, numChildren=0");
foreach($items as $item) {
$item->delete();
}
Seiten via API erstellen
https://processwire.com/talk/topic/352-creating-pages-via-api/
Allgemein
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;
Tipps
- 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());