Craft 3 Class Reference

Class craft\services\Matrix

Inheritance
craft\services\Matrix » 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/Matrix.php

The Matrix service provides APIs for managing Matrix fields.

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

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
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
deleteBlockType() Deletes a block type. craft\services\Matrix
deleteMatrixField() Deletes a Matrix field. craft\services\Matrix
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getBlockById() Returns a block by its ID. craft\services\Matrix
getBlockTypeById() Returns a block type by its ID. craft\services\Matrix
getBlockTypesByFieldId() Returns the block types for a given Matrix field. craft\services\Matrix
getContentTableName() Returns the content table name for a given Matrix field. craft\services\Matrix
getParentMatrixField() Returns the parent Matrix field, if any. craft\services\Matrix
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
saveBlockType() Saves a block type. craft\services\Matrix
saveField() Saves a Matrix field. craft\services\Matrix
saveSettings() Saves a Matrix field's settings. craft\services\Matrix
trigger() Triggers an event. yii\base\Component
validateBlockType() Validates a block type. craft\services\Matrix
validateFieldSettings() Validates a Matrix field's settings. craft\services\Matrix

Method Details

deleteBlockType() public method #

Deletes a block type.

public boolean deleteBlockType ( craft\models\MatrixBlockType $blockType )
$blockType craft\models\MatrixBlockType The block type.
return boolean Whether the block type was deleted successfully.
throws Throwable if reasons

deleteMatrixField() public method #

Deletes a Matrix field.

public boolean deleteMatrixField ( craft\fields\Matrix $matrixField )
$matrixField craft\fields\Matrix The Matrix field.
return boolean Whether the field was deleted successfully.
throws Throwable

getBlockById() public method #

Returns a block by its ID.

public craft\elements\MatrixBlock, null getBlockById ( \craft\services\int $blockId, \craft\services\int $siteId null )
$blockId integer The Matrix block’s ID.
$siteId integer, null The site ID to return. Defaults to the current site.
return craft\elements\MatrixBlock, null The Matrix block, or null if it didn’t exist.

getBlockTypeById() public method #

Returns a block type by its ID.

public craft\models\MatrixBlockType, null getBlockTypeById ( \craft\services\int $blockTypeId )
$blockTypeId integer The block type ID.
return craft\models\MatrixBlockType, null The block type, or null if it didn’t exist.

getBlockTypesByFieldId() public method #

Returns the block types for a given Matrix field.

public craft\models\MatrixBlockType[] getBlockTypesByFieldId ( \craft\services\int $fieldId )
$fieldId integer The Matrix field ID.
return craft\models\MatrixBlockType[] An array of block types.

getContentTableName() public method #

Returns the content table name for a given Matrix field.

public string, false getContentTableName ( craft\fields\Matrix $matrixField, \craft\services\bool $useOldHandle false )
$matrixField craft\fields\Matrix The Matrix field.
$useOldHandle boolean Whether the method should use the field’s old handle when determining the table name (e.g. to get the existing table name, rather than the new one).
return string, false The table name, or false if $useOldHandle was set to true and there was no old handle.

getParentMatrixField() public method #

Returns the parent Matrix field, if any.

public craft\fields\Matrix, null getParentMatrixField ( craft\fields\Matrix $matrixField )
$matrixField craft\fields\Matrix The Matrix field.
return craft\fields\Matrix, null The Matrix field’s parent Matrix field, or null if there is none.

saveBlockType() public method #

Saves a block type.

public boolean saveBlockType ( craft\models\MatrixBlockType $blockType, \craft\services\bool $validate true )
$blockType craft\models\MatrixBlockType The block type to be saved.
$validate boolean Whether the block type should be validated before being saved. Defaults to true.
throws yii\base\Exception if an error occurs when saving the block type
throws Throwable if reasons

saveField() public method #

Saves a Matrix field.

public void saveField ( craft\fields\Matrix $field, craft\base\ElementInterface $owner )
$field craft\fields\Matrix The Matrix field
$owner craft\base\ElementInterface The element the field is associated with
throws Throwable if reasons

saveSettings() public method #

Saves a Matrix field's settings.

public boolean saveSettings ( craft\fields\Matrix $matrixField, \craft\services\bool $validate true )
$matrixField craft\fields\Matrix The Matrix field
$validate boolean Whether the settings should be validated before being saved.
return boolean Whether the settings saved successfully.
throws Throwable if reasons

validateBlockType() public method #

Validates a block type.

If the block type doesn’t validate, any validation errors will be stored on the block type.

public boolean validateBlockType ( craft\models\MatrixBlockType $blockType, \craft\services\bool $validateUniques true )
$blockType craft\models\MatrixBlockType The block type.
$validateUniques boolean Whether the Name and Handle attributes should be validated to ensure they’re unique. Defaults to true.
return boolean Whether the block type validated.

validateFieldSettings() public method #

Validates a Matrix field's settings.

If the settings don’t validate, any validation errors will be stored on the settings model.

public boolean validateFieldSettings ( craft\fields\Matrix $matrixField )
$matrixField craft\fields\Matrix The Matrix field
return boolean Whether the settings validated.