Craft 3 Class Reference

Class craft\services\Fields

Inheritance
craft\services\Fields » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/services/Fields.php

Fields service.

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

Public Properties
Property Type Description Defined By
$oldFieldColumnPrefix string craft\services\Fields
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
assembleLayout() Assembles a field layout. craft\services\Fields
assembleLayoutFromPost() Assembles a field layout from post data. craft\services\Fields
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
createField() Creates a field with a given config. craft\services\Fields
deleteField() Deletes a field. craft\services\Fields
deleteFieldById() Deletes a field by its ID. craft\services\Fields
deleteGroup() Deletes a field group. craft\services\Fields
deleteGroupById() Deletes a field group by its ID. craft\services\Fields
deleteLayout() Deletes a field layout. craft\services\Fields
deleteLayoutById() Deletes a field layout(s) by its ID. craft\services\Fields
deleteLayoutsByType() Deletes field layouts associated with a given element type. craft\services\Fields
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
doesFieldWithHandleExist() Returns whether a field exists with a given handle and context. craft\services\Fields
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getAllFieldTypes() Returns all available field type classes. craft\services\Fields
getAllFields() Returns all fields within a field context(s). craft\services\Fields
getAllGroups() Returns all field groups. craft\services\Fields
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCompatibleFieldTypes() Returns all field types whose column types are considered compatible with a given field. craft\services\Fields
getFieldByHandle() Returns a field by its handle. craft\services\Fields
getFieldById() Returns a field by its ID. craft\services\Fields
getFieldIdsByLayoutIds() Returns the field IDs grouped by layout IDs, for a given set of layout IDs. craft\services\Fields
getFieldTypesWithContent() Returns all field types that have a column in the content table. craft\services\Fields
getFieldsByElementType() Returns all of the fields used by a given element type. craft\services\Fields
getFieldsByGroupId() Returns all the fields in a given group. craft\services\Fields
getFieldsByLayoutId() Returns the fields in a field layout, identified by its ID. craft\services\Fields
getFieldsWithContent() Returns all fields that have a column in the content table. craft\services\Fields
getGroupById() Returns a field group by its ID. craft\services\Fields
getLayoutById() Returns a field layout by its ID. craft\services\Fields
getLayoutByType() Returns a field layout by its associated element type. craft\services\Fields
getLayoutTabsById() Returns a layout's tabs by its ID. craft\services\Fields
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
saveField() Saves a field. craft\services\Fields
saveGroup() Saves a field group. craft\services\Fields
saveLayout() Saves a field layout. craft\services\Fields
trigger() Triggers an event. yii\base\Component
updateFieldVersion() Increases the app's field version, so the ContentBehavior (et al) classes get regenerated. craft\services\Fields
updateFieldVersionAfterRequest() Increases the app's field version, so the ContentBehavior (et al) classes get regenerated. craft\services\Fields
Events
Event Type Description Defined By
EVENT_AFTER_DELETE_FIELD craft\events\FieldEvent The event that is triggered after a field is deleted. craft\services\Fields
EVENT_AFTER_DELETE_FIELD_GROUP craft\events\FieldGroupEvent The event that is triggered after a field group is deleted. craft\services\Fields
EVENT_AFTER_DELETE_FIELD_LAYOUT craft\events\FieldLayoutEvent The event that is triggered after a field layout is deleted. craft\services\Fields
EVENT_AFTER_SAVE_FIELD craft\events\FieldEvent The event that is triggered after a field is saved. craft\services\Fields
EVENT_AFTER_SAVE_FIELD_GROUP craft\events\FieldGroupEvent The event that is triggered after a field group is saved. craft\services\Fields
EVENT_AFTER_SAVE_FIELD_LAYOUT craft\events\FieldLayoutEvent The event that is triggered after a field layout is saved. craft\services\Fields
EVENT_BEFORE_DELETE_FIELD craft\events\FieldEvent The event that is triggered before a field is deleted. craft\services\Fields
EVENT_BEFORE_DELETE_FIELD_GROUP craft\events\FieldGroupEvent The event that is triggered before a field group is deleted. craft\services\Fields
EVENT_BEFORE_DELETE_FIELD_LAYOUT craft\events\FieldLayoutEvent The event that is triggered before a field layout is deleted. craft\services\Fields
EVENT_BEFORE_SAVE_FIELD craft\events\FieldEvent The event that is triggered before a field is saved. craft\services\Fields
EVENT_BEFORE_SAVE_FIELD_GROUP craft\events\FieldGroupEvent The event that is triggered before a field group is saved. craft\services\Fields
EVENT_BEFORE_SAVE_FIELD_LAYOUT craft\events\FieldLayoutEvent The event that is triggered before a field layout is saved. craft\services\Fields
EVENT_REGISTER_FIELD_TYPES craft\events\RegisterComponentTypesEvent The event that is triggered when registering field types. craft\services\Fields

Property Details

$oldFieldColumnPrefix public property #

public string $oldFieldColumnPrefix 'field_'

Method Details

assembleLayout() public method #

Assembles a field layout.

public craft\models\FieldLayout assembleLayout ( array $postedFieldLayout, array $requiredFields = [] )
$postedFieldLayout array The post data for the field layout
$requiredFields array The field IDs that should be marked as required in the field layout
return craft\models\FieldLayout The field layout

assembleLayoutFromPost() public method #

Assembles a field layout from post data.

public craft\models\FieldLayout assembleLayoutFromPost ( \craft\services\string $namespace null )
$namespace string, null The namespace that the form data was posted in, if any
return craft\models\FieldLayout The field layout

createField() public method #

Creates a field with a given config.

public craft\base\FieldInterface createField ( $config )
$config mixed The field’s class name, or its config, with a type value and optionally a settings value
return craft\base\FieldInterface The field

deleteField() public method #

Deletes a field.

public boolean deleteField ( craft\base\FieldInterface $field )
$field craft\base\FieldInterface The field
return boolean Whether the field was deleted successfully
throws Throwable if reasons

deleteFieldById() public method #

Deletes a field by its ID.

public boolean deleteFieldById ( \craft\services\int $fieldId )
$fieldId integer The field’s ID
return boolean Whether the field was deleted successfully

deleteGroup() public method #

Deletes a field group.

public boolean deleteGroup ( craft\models\FieldGroup $group )
$group craft\models\FieldGroup The field group
return boolean Whether the field group was deleted successfully

deleteGroupById() public method #

Deletes a field group by its ID.

public boolean deleteGroupById ( \craft\services\int $groupId )
$groupId integer The field group’s ID
return boolean Whether the field group was deleted successfully

deleteLayout() public method #

Deletes a field layout.

public boolean deleteLayout ( craft\models\FieldLayout $layout )
$layout craft\models\FieldLayout The field layout
return boolean Whether the field layout was deleted successfully

deleteLayoutById() public method #

Deletes a field layout(s) by its ID.

public boolean deleteLayoutById ( $layoutId )
$layoutId integer, integer[] The field layout’s ID
return boolean Whether the field layout was deleted successfully

deleteLayoutsByType() public method #

Deletes field layouts associated with a given element type.

public boolean deleteLayoutsByType ( \craft\services\string $type )
$type string The element type
return boolean Whether the field layouts were deleted successfully

doesFieldWithHandleExist() public method #

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

public boolean doesFieldWithHandleExist ( \craft\services\string $handle, \craft\services\string $context null )
$handle string The field handle
$context string, null The field context (defauts to ContentService::$fieldContext)
return boolean Whether a field with that handle exists

getAllFieldTypes() public method #

Returns all available field type classes.

public string[] getAllFieldTypes ( )
return string[] The available field type classes

getAllFields() public method #

Returns all fields within a field context(s).

public craft\base\FieldInterface[] getAllFields ( $context null )
$context string, string[], null The field context(s) to fetch fields from. Defaults to {@link ContentService::$fieldContext}.
return craft\base\FieldInterface[] The fields

getAllGroups() public method #

Returns all field groups.

public craft\models\FieldGroup[] getAllGroups ( )
return craft\models\FieldGroup[] The field groups

getCompatibleFieldTypes() public method #

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

public string[] getCompatibleFieldTypes ( craft\base\FieldInterface $field, \craft\services\bool $includeCurrent true )
$field craft\base\FieldInterface The current field to base compatible fields on
$includeCurrent boolean Whether $field's class should be included
return string[] The compatible field type classes

getFieldByHandle() public method #

Returns a field by its handle.

public craft\base\FieldInterface, null getFieldByHandle ( \craft\services\string $handle )
$handle string The field’s handle
return craft\base\FieldInterface, null The field, or null if it doesn’t exist

getFieldById() public method #

Returns a field by its ID.

public craft\base\FieldInterface, null getFieldById ( \craft\services\int $fieldId )
$fieldId integer The field’s ID
return craft\base\FieldInterface, null The field, or null if it doesn’t exist

getFieldIdsByLayoutIds() public method #

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

public array getFieldIdsByLayoutIds ( array $layoutIds )
$layoutIds integer[] The field layout IDs

getFieldTypesWithContent() public method #

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

public string[] getFieldTypesWithContent ( )
return string[] The field type classes

getFieldsByElementType() public method #

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

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

getFieldsByGroupId() public method #

Returns all the fields in a given group.

public craft\base\FieldInterface[] getFieldsByGroupId ( \craft\services\int $groupId )
$groupId integer The field group’s ID
return craft\base\FieldInterface[] The fields

getFieldsByLayoutId() public method #

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

public craft\base\FieldInterface[] getFieldsByLayoutId ( \craft\services\int $layoutId )
$layoutId integer The field layout’s ID
return craft\base\FieldInterface[] The fields

getFieldsWithContent() public method #

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

public craft\base\FieldInterface[] getFieldsWithContent ( )
return craft\base\FieldInterface[] The fields

getGroupById() public method #

Returns a field group by its ID.

public craft\models\FieldGroup, null getGroupById ( \craft\services\int $groupId )
$groupId integer The field group’s ID
return craft\models\FieldGroup, null The field group, or null if it doesn’t exist

getLayoutById() public method #

Returns a field layout by its ID.

public craft\models\FieldLayout, null getLayoutById ( \craft\services\int $layoutId )
$layoutId integer The field layout’s ID
return craft\models\FieldLayout, null The field layout, or null if it doesn’t exist

getLayoutByType() public method #

Returns a field layout by its associated element type.

public craft\models\FieldLayout getLayoutByType ( \craft\services\string $type )
$type string The associated element type
return craft\models\FieldLayout The field layout

getLayoutTabsById() public method #

Returns a layout's tabs by its ID.

public craft\models\FieldLayoutTab[] getLayoutTabsById ( \craft\services\int $layoutId )
$layoutId integer The field layout’s ID
return craft\models\FieldLayoutTab[] The field layout’s tabs

saveField() public method #

Saves a field.

public boolean saveField ( craft\base\FieldInterface $field, \craft\services\bool $runValidation true )
$field craft\base\FieldInterface The Field to be saved
$runValidation boolean Whether the field should be validated
return boolean Whether the field was saved successfully
throws Throwable if reasons

saveGroup() public method #

Saves a field group.

public boolean saveGroup ( craft\models\FieldGroup $group, \craft\services\bool $runValidation true )
$group craft\models\FieldGroup The field group to be saved
$runValidation boolean Whether the group should be validated
return boolean Whether the field group was saved successfully

saveLayout() public method #

Saves a field layout.

public boolean saveLayout ( craft\models\FieldLayout $layout, \craft\services\bool $runValidation true )
$layout craft\models\FieldLayout The field layout
$runValidation boolean Whether the layout should be validated
return boolean Whether the field layout was saved successfully
throws yii\base\Exception if $layout->id is set to an invalid layout ID

updateFieldVersion() public method #

Increases the app's field version, so the ContentBehavior (et al) classes get regenerated.

public void updateFieldVersion ( )

updateFieldVersionAfterRequest() public method #

Increases the app's field version, so the ContentBehavior (et al) classes get regenerated.

public void updateFieldVersionAfterRequest ( )

Event Details

EVENT_AFTER_DELETE_FIELD event of type craft\events\FieldEvent #

The event that is triggered after a field is deleted.

EVENT_AFTER_DELETE_FIELD_GROUP event of type craft\events\FieldGroupEvent #

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

EVENT_AFTER_DELETE_FIELD_LAYOUT event of type craft\events\FieldLayoutEvent #

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

EVENT_AFTER_SAVE_FIELD event of type craft\events\FieldEvent #

The event that is triggered after a field is saved.

EVENT_AFTER_SAVE_FIELD_GROUP event of type craft\events\FieldGroupEvent #

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

EVENT_AFTER_SAVE_FIELD_LAYOUT event of type craft\events\FieldLayoutEvent #

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

EVENT_BEFORE_DELETE_FIELD event of type craft\events\FieldEvent #

The event that is triggered before a field is deleted.

EVENT_BEFORE_DELETE_FIELD_GROUP event of type craft\events\FieldGroupEvent #

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

EVENT_BEFORE_DELETE_FIELD_LAYOUT event of type craft\events\FieldLayoutEvent #

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

EVENT_BEFORE_SAVE_FIELD event of type craft\events\FieldEvent #

The event that is triggered before a field is saved.

EVENT_BEFORE_SAVE_FIELD_GROUP event of type craft\events\FieldGroupEvent #

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

EVENT_BEFORE_SAVE_FIELD_LAYOUT event of type craft\events\FieldLayoutEvent #

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

EVENT_REGISTER_FIELD_TYPES event of type craft\events\RegisterComponentTypesEvent #

The event that is triggered when registering field types.