Adding and Assigning Fields without using gui?
#1
Posted 11 March 2012 - 09:29 PM
Is there an easy way to add fields and then assign it to templates besides going through the admin gui? I find it slows down development anytime I think of a new field and have to manually add it then assign it to all the templates I need it in... (would be a nice feature if we could add the field to multiple templates at the time of creating the new field)
Looking at the cheatsheet, I see I can see how to set the name, title, and label of a field using $field->name ... etc.
checking the advanced tab in the cheatsheet I can see the the the $fields-save() etc...
but I'm not really sure how to create a field object or what its structure is like~
does anyone create these via scripts? how do you guys do it?
#2
Posted 12 March 2012 - 02:50 AM
I'd say go check out the source of configurable modules. The code for building the configuration options is there - you'd only save the fields into db:
// this is a container for fields, basically like a fieldset
$fields = new InputfieldWrapper();
// since this is a static function, we can't use $this->modules, so get them from the global wire() function
$modules = wire('modules');
// Populate $data with the default config, because if they've never configured this module before,
// the $data provided to this function will be empty. Or, if you add new config items in a new version,
// $data won't have it until they configure it. Best bet is to merge defaults with custom, where
// custom overwrites the defaults (array_merge).
$data = array_merge(self::getDefaultData(), $data);
// showModal field
$field = $modules->get("InputfieldCheckbox");
$field->name = "showModal";
$field->label = "Use modal window";
$field->description = "Whether open pages in modal (overlay) window or goes to traditional administration.";
$field->value = 1; // providing a "checked" value for the checkbox is necessary
$field->attr('checked', empty($data['showModal']) ? '' : 'checked');
$fields->add($field);
return $fields;
This is from latest AdminBar by @apeisa. You can see the '$field' part, where the field is built. You probably only run $field->save() at the end instead of current $fields->add() and you have just created field via code
#3
Posted 12 March 2012 - 04:53 AM
$template = $templates->get("some_template");
$template->fields->add("newfield");
$template->fields->save();
this code is taken from here http://processwire.c...bles/templates/
So, just make an array with all the templates you want the field to be on, and go for it
$ts = array("home", "basic-page", "search");
foreach($ts as $t){
$template = $templates->get($t);
$template->fields->add("newfield");
$template->fields->save();
}EDIT: the field will be on the last position of each template
#4
Posted 12 March 2012 - 07:33 AM
// new fieldgroup
$fg = new Fieldgroup();
$fg->name = 'new-template';
$fg->add($this->fields->get('title')); // needed title field
$fg->save();
// new template using the fieldgroup
$t = new Template();
$t->name = 'new-template';
$t->fieldgroup = $fg; // add the fieldgroup
$t->noChildren = 1;
$t->save();
// add one more field, attributes depending on fieldtype
$f = new Field(); // create new field object
$f->type = $this->modules->get("FieldtypeFloat"); // get a field type
$f->name = 'price';
$f->precision = 2;
$f->label = 'Price of the product';
$f->save(); // save the field
$fg->add($f); // add field to fieldgroup
$fg->save(); // save fieldgroup
All pretty much standard OO one can figure out looking at core and PW modules. But not someone unexperienced would figure out by themself. I think at some point we need to cover these in a documentation.
@somartist | modules created | support me, flattr my work flattr.com
#5
Posted 12 March 2012 - 09:31 AM
#6
Posted 13 March 2012 - 02:26 AM
You can assign fields to templates like this:
$template = $templates->get("some_template"); $template->fields->add("newfield"); $template->fields->save();this code is taken from here http://processwire.c...bles/templates/ So, just make an array with all the templates you want the field to be on, and go for it![]()
$ts = array("home", "basic-page", "search"); foreach($ts as $t){ $template = $templates->get($t); $template->fields->add("newfield"); $template->fields->save(); }EDIT: the field will be on the last position of each template
Thanks! I think I'll start off by making fields in the gui, then just adding the field to templates should be a breeze using this script... I'll try creating fields and templates after i get a little more comfortable... maybe even try doing it through the php command line interface. That'll be the boss round though.
yes, I amHey neil (are you neil?)
#7
Posted 13 January 2013 - 11:26 PM
Great thread and answers! Thanks everyone! ![]()
Tina // TinaciousDesign.com // @tinaciousdesign
#8
Posted 05 June 2013 - 01:23 PM
I am trying to create a basic setup script that I can use to pre-populate my new ProcessWire installs with the fields and templates I use on the majority of sites I create. To do this I'm bootstrapping PW in a php file I've created in my site's root:
<?php
// Bootstrap ProcessWire
include('./index.php');
// Assign API variables to make things a little easier
$fields = wire("fields");
$templates = wire("templates");
$modules = wire("modules");
// Edit existing fields
$f = $fields->body;
$f->rows = 30;
$f->save();
$f = $fields->sidebar;
$f->rows = 30;
$f->save();
// Redirect template
if(!$fields->redirect_page) {
$f = new Field();
$f->type = $modules->get("FieldtypePage");
$f->name = 'redirect_page';
$f->label = 'Link to a Page';
$f->inputfield = 'InputfieldPageListSelect';
$f->save();
}
if(!$fields->redirect_url) {
$f = new Field();
$f->type = $modules->get("FieldtypeURL");
$f->name = 'redirect_url';
$f->label = 'Link to an off-site URL';
$f->noRelative = 1;
$f->addRoot = 0;
$f->placeholder = 'http://www.website.com/';
$f->save();
}
if(!$templates->redirect) {
$t = new Template();
$t->name = 'redirect';
$t->fields->add("redirect_page");
$t->fields->add("redirect_url");
$t->save();
}
echo 'Default setup is complete!';
Everything seems to work except for the last bit on the bottom where I try and assign the fields to the new "redirect" template. I get the error message "Error: Call to a member function add() on a non-object"
I know it's probably something simple, but I can't figure it out! Any ideas?
Edit: As usual, I figure out my problem as soon as I post my question on the forums ![]()
Here is the working code:
<?php
// Bootstrap ProcessWire
include('./index.php');
// Assign API variables to make things a little easier
$fields = wire("fields");
$templates = wire("templates");
$modules = wire("modules");
// Edit existing fields
$f = $fields->body;
$f->rows = 30;
$f->save();
$f = $fields->sidebar;
$f->rows = 30;
$f->save();
// Create redirect template & fields
$newTemplateName = 'redirect';
if(!$fields->redirect_page) {
$f = new Field();
$f->type = $modules->get("FieldtypePage");
$f->name = 'redirect_page';
$f->label = 'Link to a Page';
$f->inputfield = 'InputfieldPageListSelect';
$f->save();
}
if(!$fields->redirect_url) {
$f = new Field();
$f->type = $modules->get("FieldtypeURL");
$f->name = 'redirect_url';
$f->label = 'Link to an off-site URL';
$f->noRelative = 1;
$f->addRoot = 0;
$f->placeholder = 'http://www.website.com/';
$f->save();
}
if(!$templates->$newTemplateName) {
$fg = new Fieldgroup();
$fg->name = $newTemplateName;
$fg->add("title");
$fg->add("redirect_page");
$fg->add("redirect_url");
$fg->save();
$t = new Template();
$t->name = $newTemplateName;
$t->fieldgroup = $fg;
$t->save();
}
echo 'Default setup is complete!';
I am still unclear on what a Fieldgroup is and why I need to create one in order to make any changes to the template's field assignments. It feels like a bit of a clumsy extra step to have to go through because it doesn't seem to have any representation on the PW admin interface. I'm sure there's a good reason for it, though, knowing Ryan.
#9
Posted 05 June 2013 - 01:56 PM
Oops or couple posts above nice guy soma has some example.
@somartist | modules created | support me, flattr my work flattr.com
#10
Posted 05 June 2013 - 02:53 PM
Thanks Soma, I figured it out, but now I'm just a little confused as to why.
But I see that now that once the fieldgroup has already been created for the template and assigned to it, I can add another field simply by doing:
$t = $templates->get('templateName');
$t->fields->add("fieldName");
$t->fields->save();
Which is much quicker.
Is there any way to reorder the fields in a template through the API?
#11
Posted 07 June 2013 - 05:54 AM
Is there any way to reorder the fields in a template through the API?
See the cheatsheet documentation at http://cheatsheet.processwire.com/ and click the "advanced" button, then click "WireArray/PageArray". All the methods for WireArray are at your disposal for this. Specifically the section on "Setting/Modifying Items" is probably the most helpful. A WireArray only carries one of each item, so if you add/insert/etc. an item that's already present, it'll move it in the array rather than add another copy.
#12
Posted 07 June 2013 - 05:59 AM
I am still unclear on what a Fieldgroup is and why I need to create one in order to make any changes to the template's field assignments. It feels like a bit of a clumsy extra step to have to go through because it doesn't seem to have any representation on the PW admin interface. I'm sure there's a good reason for it, though, knowing Ryan.
It's hidden in the admin interface unless you've got advanced mode on. I've tried to keep the admin as simple as possible so that people can think of templates and fieldgroups as one in the same thing. But the reality is that they are actually separate things. The benefit of having them separate is that you can have multiple templates sharing the same group of fields. But currently we're not highlighting that behavior on the admin side just because I think there is more benefit to the clarity of templates just being a single thing. i had always thought we'd expand on the behavior on the admin side in giving people more options, but the need seems rare enough that it's stayed in advanced mode for now. Probably what I will end up doing is making the API itself abstract the behavior too, so that it adds a like-named Fieldgroup automatically when you access $template->fields and one isn't already there.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













