$fields is an API variable that contains all the custom fields in your site
It provides the API functions available in the admin control panel under Setup > Fields. Use it to retrieve, save or delete custom fields.
$fields is an API variable that you aren't likely to use very often unless you are developing ProcessWire plugin modules for working with fields, or trying to implement some kind of automation with fields. This is because the functions in $fields can also be accessed in the admin control panel under Setup > Fields. (That part of the admin control panel actually uses this $fields API variable to perform it's tasks.)
$fields properties and functions
| $fields->get("name") | Get the field specified by the given field name |
| $fields->get(123) | Get the field specified by the given field ID (integer) |
| $fields->get("selector") | Get the first matching field specified by the given selector |
| $fields->find("selector") | Find all fields matching the given selector |
| $fields->save($field) | Save the given Field object |
| $fields->delete($field) | Delete the given Field object |
Iterating $fields
If you iterate $fields, it will cycle through all the fields in your site in alphabetical order:
foreach($fields as $field) echo $field->name . ", ";
Field objects
$fields works with individual Field objects, which are described in this section. When you call $fields->get(...) or iterate through $fields, it is returning a Field object. Likewise, when you call $fields->save() or $fields->delete() you are passing it a Field object.
There is no Field object already provided to the API, so any Field objects that you work with will be the result of retrieving them from another API variable like $fields->get() or $page->fields. For example:
$field = $fields->get("title");
$field = $page->fields->get("title"); Field properties and functions
| $field->name | Name of this field (name string) |
| $field->id | Numeric ID of this field |
| $field->label | Text label for this field, appears as the <label> element in the editor |
| $field->type | The type of field, refers to a Fieldtype plugin module |
| $field->flags | Bitwise flags of the field: if($field->flags & Field::flagAutojoin) // field has the "autojoin" flag set if($field->flags & Field::flagGlobal) // field has the "global" flag set |
| $field->get($key) | Get a field property (the properties above, or a custom property) Same as $field->$key |
| $field->set($key, $value) | Set a field property. Can be any one of the above or something else for a custom setting. Same as $field->$key = $value |
| $field->save() | Save this field Same as $fields->save($field) |
| $field->numFieldgroups() | Returns the number of fieldgroups that use this field |
| $field->getFieldgroups() | Returns an array of fieldgroups that use this field |
| $field->getTable() | Returns the name of the database table created by this field |
| $field->getInputfield($page) | Returns an Inputfield object associated with this field for use on $page |
| (string) $field | Casting a Field as a string returns the field's name |
Comments
No comments yet. Be the first to post!
Post a Comment
Your e-mail is kept confidential and not included with your comment. Website is optional.

