Assets

Type
Class
Namespace
craft\services
Inherits
craft\services\Assets » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

Assets service.

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

View source (opens new window)

# Public Properties

# currentUserTemporaryUploadFolder

Type
craft\models\VolumeFolder
Default value
null
Access
Read-only

View source (opens new window)

# totalAssets

Type
integer (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# userTemporaryUploadFolder

Type
craft\models\VolumeFolder
Default value
null
Access
Read-only

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
createFolder() Save an Asset folder.
deleteFoldersByIds() Deletes a folder by its ID.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
ensureFolderByFullPathAndVolume() Ensure a folder entry exists in the DB for the full path and return it's id. Depending on the use, it's possible to also ensure a physical folder exists.
findFolder() Finds the first folder that matches a given criteria.
findFolders() Finds folders that match a given criteria.
getAllDescendantFolders() Returns all of the folders that are descendants of a given folder.
getAssetById() Returns a file by its ID.
getAssetPreviewHandler() Returns the asset preview handler for a given asset, or null if the asset is not previewable.
getAssetUrl() Returns the URL for an asset, possibly with a given transform applied.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getCurrentUserTemporaryUploadFolder() Return the current user's temporary upload folder.
getFolderById() Returns a folder by its ID.
getFolderByUid() Returns a folder by its UID.
getFolderTreeByFolderId() Get the folder tree for Assets by a folder id.
getFolderTreeByVolumeIds() Get the folder tree for Assets by volume ids
getIconPath() Returns a generic file extension icon path, that can be used as a fallback for assets that don't have a normal thumbnail.
getNameReplacementInFolder() Find a replacement for a filename
getRootFolderByVolumeId() Returns the root folder for a given volume ID.
getThumbPath() Returns the control panel thumbnail path for a given asset.
getThumbUrl() Returns the control panel thumbnail URL for a given asset.
getTotalAssets() Gets the total number of assets that match a given criteria.
getTotalFolders() Gets the total number of folders that match a given criteria.
getUserTemporaryUploadFolder() Returns the given user's temporary upload folder.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() (opens new window) Initializes the object.
moveAsset() Move or rename an Asset.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
renameFolderById() Rename a folder by it's id.
replaceAssetFile() Replace an Asset's file.
storeFolderRecord() Store a folder by model
trigger() (opens new window) Triggers an event.

# createFolder()

Save an Asset folder.

View source (opens new window)

Arguments

Throws

# deleteFoldersByIds()

Deletes a folder by its ID.

View source (opens new window)

Arguments

Throws

# ensureFolderByFullPathAndVolume()

Ensure a folder entry exists in the DB for the full path and return it's id. Depending on the use, it's possible to also ensure a physical folder exists.

View source (opens new window)

Arguments

Returns

integer (opens new window)

Throws

# findFolder()

Finds the first folder that matches a given criteria.

View source (opens new window)

Arguments

  • $criteria (mixed)

Returns

craft\models\VolumeFolder, null (opens new window)

# findFolders()

Finds folders that match a given criteria.

View source (opens new window)

Arguments

  • $criteria (mixed)

Returns

craft\models\VolumeFolder[]

# getAllDescendantFolders()

Returns all of the folders that are descendants of a given folder.

View source (opens new window)

Arguments

Returns

array (opens new window)

# getAssetById()

Returns a file by its ID.

View source (opens new window)

Arguments

Returns

craft\elements\Asset, null (opens new window)

# getAssetPreviewHandler()

Since
3.4.0

Returns the asset preview handler for a given asset, or null if the asset is not previewable.

View source (opens new window)

Arguments

Returns

craft\base\AssetPreviewHandlerInterface, null (opens new window)

# getAssetUrl()

Returns the URL for an asset, possibly with a given transform applied.

View source (opens new window)

Arguments

Returns

string (opens new window), null (opens new window)

# getCurrentUserTemporaryUploadFolder()

DEPRECATED

Deprecated in 3.2.0. Use getUserTemporaryUploadFolder() instead.

Return the current user's temporary upload folder.

View source (opens new window)

Returns

craft\models\VolumeFolder

# getFolderById()

Returns a folder by its ID.

View source (opens new window)

Arguments

Returns

craft\models\VolumeFolder, null (opens new window)

# getFolderByUid()

Returns a folder by its UID.

View source (opens new window)

Arguments

Returns

craft\models\VolumeFolder, null (opens new window)

# getFolderTreeByFolderId()

Get the folder tree for Assets by a folder id.

View source (opens new window)

Arguments

Returns

array (opens new window)

# getFolderTreeByVolumeIds()

Get the folder tree for Assets by volume ids

View source (opens new window)

Arguments

Returns

array (opens new window)

# getIconPath()

Returns a generic file extension icon path, that can be used as a fallback for assets that don't have a normal thumbnail.

View source (opens new window)

Arguments

Returns

string (opens new window)

# getNameReplacementInFolder()

Find a replacement for a filename

View source (opens new window)

Arguments

Returns

string (opens new window) – If a suitable filename replacement cannot be found.

Throws

# getRootFolderByVolumeId()

Returns the root folder for a given volume ID.

View source (opens new window)

Arguments

Returns

craft\models\VolumeFolder, null (opens new window) – The root folder in that volume, or null if the volume doesn’t exist

# getThumbPath()

Returns the control panel thumbnail path for a given asset.

See also getThumbUrl() View source (opens new window)

Arguments

Returns

string (opens new window), false (opens new window) – Thumbnail path, or false if it doesn't exist and $generate is false

Throws

# getThumbUrl()

Returns the control panel thumbnail URL for a given asset.

View source (opens new window)

Arguments

Returns

string (opens new window)

Throws

# getTotalAssets()

Gets the total number of assets that match a given criteria.

View source (opens new window)

Arguments

  • $criteria (mixed)

Returns

integer (opens new window)

# getTotalFolders()

Gets the total number of folders that match a given criteria.

View source (opens new window)

Arguments

  • $criteria (mixed)

Returns

integer (opens new window)

# getUserTemporaryUploadFolder()

Returns the given user's temporary upload folder.

If no user is provided, the currently-logged in user will be used (if there is one), or a folder named after the current session ID.

View source (opens new window)

Arguments

Returns

craft\models\VolumeFolder

Throws

# moveAsset()

Move or rename an Asset.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the asset was renamed successfully

Throws

# renameFolderById()

Rename a folder by it's id.

View source (opens new window)

Arguments

Returns

string (opens new window) – The new folder name after cleaning it.

Throws

# replaceAssetFile()

Replace an Asset's file.

Replace an Asset's file by it's id, a local file and the filename to use.

View source (opens new window)

Arguments

Throws

# storeFolderRecord()

Store a folder by model

View source (opens new window)

Arguments

# Events

# EVENT_AFTER_REPLACE_ASSET

Type
craft\events\ReplaceAssetEvent

The event that is triggered after an asset is replaced.

# EVENT_BEFORE_REPLACE_ASSET

Type
craft\events\ReplaceAssetEvent

The event that is triggered before an asset is replaced.

# EVENT_GET_ASSET_THUMB_URL

Type
craft\events\GetAssetThumbUrlEvent

The event that is triggered when a thumbnail is being generated for an Asset.

# EVENT_GET_ASSET_URL

Type
craft\events\GetAssetUrlEvent

The event that is triggered when a transform is being generated for an Asset.

# EVENT_GET_THUMB_PATH

Type
craft\events\AssetThumbEvent

The event that is triggered when a thumbnail path is requested.

# EVENT_REGISTER_PREVIEW_HANDLER

Type
craft\events\AssetPreviewEvent
Since
3.4.0

The event that is triggered when determining the preview handler for an asset.