php Processwire使用API​​创建和操作页面,模板,字段,用户等。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Processwire使用API​​创建和操作页面,模板,字段,用户等。相关的知识,希望对你有一定的参考价值。

<?php

// Create New User
$new_user = new User();
$new_user->of(false);
$new_user->name = "USER_NAME";
$new_user->email = "USER_EMAIL";
$new_user->pass = "USER_PASSWORD";
$new_user->addRole("USER_ROLE");
$new_user->save();
$new_user->of(true);
    
// Edit user
$user->of(false);
$user->set('name', 'USER_NAME');
$user->set('email', 'USER_EMAIL');
$user->save();
$user->of(true);

// Edit user 2
$user->of(false);
$user->name = "NAME";
$user->email = "EMAIL";
$user->save();
<?php

$fg = $this->fieldgroups->get('FIELDGROUP_NAME');
// if not, create it
if(empty($fg)) {
    $fg = new Fieldgroup();
    $fg->name = 'NAME';
    $fg->add($this->fields->get('title'));
    $fg->add($this->fields->get('FIELD2'));
    $fg->add($this->fields->get('FIELD3'));
    $fg->save();
}
<?php

// create field
$f = new Field(); // create new field object
$f->type = $this->modules->get("FieldtypePage"); // get a field type (this is page reference)
$f->name = 'FIELD_NAME';
$f->field_label = 'FIELD LABEL';
$f->inputfield = 'InputfieldChosenSelectMultiple';
$f->addable = 1;
$f->save();

/**
 * Select Options Field
 */
$f = new Field();
$f->type = $this->modules->get("FieldtypeOptions");
$f->inputfieldClass = 'InputfieldRadios'; // input type: radio, select etc...
$f->name = 'some_name';
$f->label = 'some_label';
$f->save(); 
// save before adding options
$set_options = new SelectableOptionManager();
  $options = '
      1=Blue
      2=Green
      3=Brown
  ';
$set_options->setOptionsString($f, $options, false);
$f->save();
<?php
// new fieldgroup
$fg = new Fieldgroup();
$fg->name = 'TEMPLATE_NAME';
$fg->add($this->fields->get('title')); // needed title field
$fg->add($this->fields->get('body')); // needed body field
$fg->save();

// new template using the fieldgroup and a template
$t = new Template();
$t->name = 'TEMPLATE_NAME';
$t->fieldgroup = $fg; // add the field group
$t->save();

// set template options
$t = wire('templates')->get("TEMPLATE_NAME");
$t->noParents = '-1';
$t->allowPageNum = '1';
$t->urlSegments = '1';
$t->tags = '-Blog';
$t->pageLabelIcon = 'fa-rocket';
$t->parentTemplates = array(wire('templates')->get('PARENT_TEMPLATE_NAME')); // allowedForParents
$t->childTemplates = array(wire('templates')->get('CHILD_TEMPLATE_NAME')); // allowedForChildren
$t->save();

// change field settings for this template
$t = wire('templates')->get('TEMPLATE_NAME');
$f = $t->fieldgroup->getField('FIELD_NAME', true);
$f->columnWidth = "50";
$this->fields->saveFieldgroupContext($f, $t->fieldgroup);//save new setting in context
        
/*
*  Check if template is in use before delete
*/
$t_del = wire('templates')->get("TEMPLATE_NAME");
$fg_del = wire('fieldgroups')->get('TEMPLATE_NAME');
if ($t_del->getNumPages() > 0) {
	throw new WireException("Can't uninstall because template been used by some pages.");
}else {
	wire('templates')->delete($t_del);
	wire('fieldgroups')->delete($fg_del);
}
<?php

// Change page status
$p->of(false);
$p->status('hidden');
$p->save();
$p->of(true);
    
// Create new page
$p = new Page();
$p->template = "basic-page";
$p->parent = 1;
$p->title = "Page Title";
$p->save();
// add image (need to save before add image)
$p->img->add("img_url");
$p->save();

// Create New Page 2
$new_page = $pages->add('TEMPLATE_NAME', "PARENT_PAGE", [
    'title' => "TITLE",
    'text' => "TEXT",
    'body' => "SOME_TEXT"
]);
    
// Edit Page
$p->of(false);
$p->set('title', 'SOME_TITLE');
$p->save();
$p->of(true);
    
// Set And Save
$p->setAndSave('text', 'SOME_NEW_TEXT');
// or
$p->setAndSave([
    'title' => 'It is Friday again',
    'subtitle' => 'Here is another new blog post',
    'body' => 'Hope you all have a great weekend!'
]);
    
// get page first
$p = $pages->get("/SOME_PAGE/");
// delete page
$pages->delete($p);
// delete page and subpages
$pages->delete($p, true);

以上是关于php Processwire使用API​​创建和操作页面,模板,字段,用户等。的主要内容,如果未能解决你的问题,请参考以下文章

php Processwire Images API

php Processwire Find Pages API

php Processwire API表单

php Processwire Modules API

php Processwire更改页面创建日期

php 使用jQuery排序的Processwire排序页面