Fields

Type
Class
Namespace
craft\services
Inherits
craft\services\Fields » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

Fields service.

An instance of the Fields service is globally accessible in Craft via Craft::$app->fields.

View source

Public Properties

PropertyDescription
$allFieldTypesstring[] – The available field type classes
$allFieldscraft\base\FieldInterface[] – The fields
$allGroupscraft\models\FieldGroup[] – The field groups
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$fieldTypesWithContentstring[] – The field type classes
$fieldsWithContentcraft\base\FieldInterface[] – The fields
$oldFieldColumnPrefixstring

$allFieldTypes

Type
string[]
Access
Read-only

The available field type classes

View source

Signature

public string[] getAllFieldTypes ( )

$allFields

Type
craft\base\FieldInterface[]
Access
Read-only

The fields

View source

Signature

public craft\base\FieldInterface[] getAllFields ( $context = null )

$allGroups

Type
craft\models\FieldGroup[]
Access
Read-only

The field groups

View source

Signature

public craft\models\FieldGroup[] getAllGroups ( )

$fieldTypesWithContent

Type
string[]
Access
Read-only

The field type classes

View source

Signature

public string[] getFieldTypesWithContent ( )

$fieldsWithContent

Type
craft\base\FieldInterface[]
Access
Read-only

The fields

View source

Signature

public craft\base\FieldInterface[] getFieldsWithContent ( )

$oldFieldColumnPrefix

Signature

public string $oldFieldColumnPrefix = 'field_'

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to null.
assembleLayout()Assembles a field layout.
assembleLayoutFromPost()Assembles a field layout from post data.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
createField()Creates a field with a given config.
deleteField()Deletes a field.
deleteFieldById()Deletes a field by its ID.
deleteGroup()Deletes a field group.
deleteGroupById()Deletes a field group by its ID.
deleteLayout()Deletes a field layout.
deleteLayoutById()Deletes a field layout(s) by its ID.
deleteLayoutsByType()Deletes field layouts associated with a given element type.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
doesFieldWithHandleExist()Returns whether a field exists with a given handle and context.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getAllFieldTypes()Returns all available field type classes.
getAllFields()Returns all fields within a field context(s).
getAllGroups()Returns all field groups.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCompatibleFieldTypes()Returns all field types whose column types are considered compatible with a given field.
getFieldByHandle()Returns a field by its handle.
getFieldById()Returns a field by its ID.
getFieldIdsByLayoutIds()Returns the field IDs grouped by layout IDs, for a given set of layout IDs.
getFieldTypesWithContent()Returns all field types that have a column in the content table.
getFieldsByElementType()Returns all of the fields used by a given element type.
getFieldsByGroupId()Returns all the fields in a given group.
getFieldsByLayoutId()Returns the fields in a field layout, identified by its ID.
getFieldsWithContent()Returns all fields that have a column in the content table.
getGroupById()Returns a field group by its ID.
getLayoutById()Returns a field layout by its ID.
getLayoutByType()Returns a field layout by its associated element type.
getLayoutTabsById()Returns a layout's tabs by its ID.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
saveField()Saves a field.
saveGroup()Saves a field group.
saveLayout()Saves a field layout.
trigger()Triggers an event.
updateFieldVersion()Sets a new field version, so the ContentBehavior and ElementQueryBehavior classes will get regenerated on the next request.

assembleLayout()

Assembles a field layout.

View source

Arguments

  • $postedFieldLayout (array) – The post data for the field layout
  • $requiredFields (array) – The field IDs that should be marked as required in the field layout

Returns

craft\models\FieldLayout – The field layout

Signature

public craft\models\FieldLayout assembleLayout ( array $postedFieldLayout, array $requiredFields = [] )

assembleLayoutFromPost()

Assembles a field layout from post data.

View source

Arguments

  • $namespace (string, null) – The namespace that the form data was posted in, if any

Returns

craft\models\FieldLayout – The field layout

Signature

public craft\models\FieldLayout assembleLayoutFromPost ( \craft\services\string $namespace = null )

createField()

Creates a field with a given config.

View source

Arguments

  • $config (mixed) – The field’s class name, or its config, with a type value and optionally a settings value

Returns

craft\base\FieldInterface – The field

Signature

public craft\base\FieldInterface createField ( $config )

deleteField()

Deletes a field.

View source

Arguments

Returns

boolean – Whether the field was deleted successfully

Throws

Signature

public boolean deleteField ( craft\base\FieldInterface $field )

deleteFieldById()

Deletes a field by its ID.

View source

Arguments

  • $fieldId (integer) – The field’s ID

Returns

boolean – Whether the field was deleted successfully

Signature

public boolean deleteFieldById ( \craft\services\int $fieldId )

deleteGroup()

Deletes a field group.

View source

Arguments

Returns

boolean – Whether the field group was deleted successfully

Signature

public boolean deleteGroup ( craft\models\FieldGroup $group )

deleteGroupById()

Deletes a field group by its ID.

View source

Arguments

  • $groupId (integer) – The field group’s ID

Returns

boolean – Whether the field group was deleted successfully

Signature

public boolean deleteGroupById ( \craft\services\int $groupId )

deleteLayout()

Deletes a field layout.

View source

Arguments

Returns

boolean – Whether the field layout was deleted successfully

Signature

public boolean deleteLayout ( craft\models\FieldLayout $layout )

deleteLayoutById()

Deletes a field layout(s) by its ID.

View source

Arguments

Returns

boolean – Whether the field layout was deleted successfully

Signature

public boolean deleteLayoutById ( $layoutId )

deleteLayoutsByType()

Deletes field layouts associated with a given element type.

View source

Arguments

  • $type (string) – The element type

Returns

boolean – Whether the field layouts were deleted successfully

Signature

public boolean deleteLayoutsByType ( \craft\services\string $type )

doesFieldWithHandleExist()

Returns whether a field exists with a given handle and context.

View source

Arguments

  • $handle (string) – The field handle
  • $context (string, null) – The field context (defauts to ContentService::$fieldContext)

Returns

boolean – Whether a field with that handle exists

Signature

public boolean doesFieldWithHandleExist ( \craft\services\string $handle, \craft\services\string $context = null )

getAllFieldTypes()

Returns all available field type classes.

View source

Returns

string[] – The available field type classes

Signature

public string[] getAllFieldTypes ( )

getAllFields()

Returns all fields within a field context(s).

View source

Arguments

  • $context (string, string[], null) – The field context(s) to fetch fields from. Defaults to {@link ContentService::$fieldContext}.

Returns

craft\base\FieldInterface[] – The fields

Signature

public craft\base\FieldInterface[] getAllFields ( $context = null )

getAllGroups()

Returns all field groups.

View source

Returns

craft\models\FieldGroup[] – The field groups

Signature

public craft\models\FieldGroup[] getAllGroups ( )

getCompatibleFieldTypes()

Returns all field types whose column types are considered compatible with a given field.

View source

Arguments

Returns

string[] – The compatible field type classes

Signature

public string[] getCompatibleFieldTypes ( craft\base\FieldInterface $field, \craft\services\bool $includeCurrent = true )

getFieldByHandle()

Returns a field by its handle.

View source

Arguments

  • $handle (string) – The field’s handle

Returns

craft\base\FieldInterface, null – The field, or null if it doesn’t exist

Signature

public craft\base\FieldInterface, null getFieldByHandle ( \craft\services\string $handle )

Example

getFieldById()

Returns a field by its ID.

View source

Arguments

  • $fieldId (integer) – The field’s ID

Returns

craft\base\FieldInterface, null – The field, or null if it doesn’t exist

Signature

public craft\base\FieldInterface, null getFieldById ( \craft\services\int $fieldId )

getFieldIdsByLayoutIds()

Returns the field IDs grouped by layout IDs, for a given set of layout IDs.

View source

Arguments

  • $layoutIds (integer[]) – The field layout IDs

Returns

array

Signature

public array getFieldIdsByLayoutIds ( array $layoutIds )

getFieldTypesWithContent()

Returns all field types that have a column in the content table.

View source

Returns

string[] – The field type classes

Signature

public string[] getFieldTypesWithContent ( )

getFieldsByElementType()

Returns all of the fields used by a given element type.

View source

Arguments

Returns

craft\base\FieldInterface[] – The fields

Signature

public craft\base\FieldInterface[] getFieldsByElementType ( \craft\services\string $elementType )

getFieldsByGroupId()

Returns all the fields in a given group.

View source

Arguments

  • $groupId (integer) – The field group’s ID

Returns

craft\base\FieldInterface[] – The fields

Signature

public craft\base\FieldInterface[] getFieldsByGroupId ( \craft\services\int $groupId )

getFieldsByLayoutId()

Returns the fields in a field layout, identified by its ID.

View source

Arguments

  • $layoutId (integer) – The field layout’s ID

Returns

craft\base\FieldInterface[] – The fields

Signature

public craft\base\FieldInterface[] getFieldsByLayoutId ( \craft\services\int $layoutId )

getFieldsWithContent()

Returns all fields that have a column in the content table.

View source

Returns

craft\base\FieldInterface[] – The fields

Signature

public craft\base\FieldInterface[] getFieldsWithContent ( )

getGroupById()

Returns a field group by its ID.

View source

Arguments

  • $groupId (integer) – The field group’s ID

Returns

craft\models\FieldGroup, null – The field group, or null if it doesn’t exist

Signature

public craft\models\FieldGroup, null getGroupById ( \craft\services\int $groupId )

getLayoutById()

Returns a field layout by its ID.

View source

Arguments

  • $layoutId (integer) – The field layout’s ID

Returns

craft\models\FieldLayout, null – The field layout, or null if it doesn’t exist

Signature

public craft\models\FieldLayout, null getLayoutById ( \craft\services\int $layoutId )

getLayoutByType()

Returns a field layout by its associated element type.

View source

Arguments

  • $type (string) – The associated element type

Returns

craft\models\FieldLayout – The field layout

Signature

public craft\models\FieldLayout getLayoutByType ( \craft\services\string $type )

getLayoutTabsById()

Returns a layout's tabs by its ID.

View source

Arguments

  • $layoutId (integer) – The field layout’s ID

Returns

craft\models\FieldLayoutTab[] – The field layout’s tabs

Signature

public craft\models\FieldLayoutTab[] getLayoutTabsById ( \craft\services\int $layoutId )

saveField()

Saves a field.

View source

Arguments

Returns

boolean – Whether the field was saved successfully

Throws

Signature

public boolean saveField ( craft\base\FieldInterface $field, \craft\services\bool $runValidation = true )

saveGroup()

Saves a field group.

View source

Arguments

Returns

boolean – Whether the field group was saved successfully

Signature

public boolean saveGroup ( craft\models\FieldGroup $group, \craft\services\bool $runValidation = true )

saveLayout()

Saves a field layout.

View source

Arguments

Returns

boolean – Whether the field layout was saved successfully

Throws

Signature

public boolean saveLayout ( craft\models\FieldLayout $layout, \craft\services\bool $runValidation = true )

updateFieldVersion()

Sets a new field version, so the ContentBehavior and ElementQueryBehavior classes will get regenerated on the next request. This will only have an effect once per request. Subsequent calls will be ignored.

View source

Signature

public void updateFieldVersion ( )

Events

EVENT_AFTER_DELETE_FIELD

Type
craft\events\FieldEvent

The event that is triggered after a field is deleted.

EVENT_AFTER_DELETE_FIELD_GROUP

Type
craft\events\FieldGroupEvent

The event that is triggered after a field group is deleted.

EVENT_AFTER_DELETE_FIELD_LAYOUT

Type
craft\events\FieldLayoutEvent

The event that is triggered after a field layout is deleted.

EVENT_AFTER_SAVE_FIELD

Type
craft\events\FieldEvent

The event that is triggered after a field is saved.

EVENT_AFTER_SAVE_FIELD_GROUP

Type
craft\events\FieldGroupEvent

The event that is triggered after a field group is saved.

EVENT_AFTER_SAVE_FIELD_LAYOUT

Type
craft\events\FieldLayoutEvent

The event that is triggered after a field layout is saved.

EVENT_BEFORE_DELETE_FIELD

Type
craft\events\FieldEvent

The event that is triggered before a field is deleted.

EVENT_BEFORE_DELETE_FIELD_GROUP

Type
craft\events\FieldGroupEvent

The event that is triggered before a field group is deleted.

EVENT_BEFORE_DELETE_FIELD_LAYOUT

Type
craft\events\FieldLayoutEvent

The event that is triggered before a field layout is deleted.

EVENT_BEFORE_SAVE_FIELD

Type
craft\events\FieldEvent

The event that is triggered before a field is saved.

EVENT_BEFORE_SAVE_FIELD_GROUP

Type
craft\events\FieldGroupEvent

The event that is triggered before a field group is saved.

EVENT_BEFORE_SAVE_FIELD_LAYOUT

Type
craft\events\FieldLayoutEvent

The event that is triggered before a field layout is saved.

EVENT_REGISTER_FIELD_TYPES

Type
craft\events\RegisterComponentTypesEvent

The event that is triggered when registering field types.

Field types must implement craft\base\FieldInterface. craft\base\Field provides a base implementation.

See Field Types for documentation on creating field types.

Example