Assets

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

Assets service.

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

View source

Public Properties

$currentUserTemporaryUploadFolder

Signature

public craft\models\VolumeFolder getCurrentUserTemporaryUploadFolder ( )

$totalAssets

Type
integer
Access
Read-only

View source

Signature

public integer getTotalAssets ( $criteria = null )

$userTemporaryUploadFolder

Signature

public craft\models\VolumeFolder getUserTemporaryUploadFolder ( craft\elements\User $userModel = null )

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.
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.
createFolder()Save an Asset folder.
deleteFoldersByIds()Deletes a folder by its ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() 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.
getAssetUrl()Returns the URL for an asset, possibly with a given transform applied.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCurrentUserTemporaryUploadFolder()Return the current user's temporary upload folder.
getFolderById()Returns a folder by its ID.
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 CP thumbnail path for a given asset.
getThumbUrl()Returns the CP 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()Get the user's temporary upload folder.
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.
moveAsset()Move or rename an Asset.
off()Detaches an existing event handler from this component.
on()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()Triggers an event.

createFolder()

Save an Asset folder.

View source

Arguments

Throws

Signature

public void createFolder ( craft\models\VolumeFolder $folder, \craft\services\bool $indexExisting = false )

deleteFoldersByIds()

Deletes a folder by its ID.

View source

Arguments

  • $folderIds (array, integer)
  • $deleteDir (boolean) – Should the volume directory be deleted along the record, if applicable. Defaults to true.

Throws

Signature

public void deleteFoldersByIds ( $folderIds, \craft\services\bool $deleteDir = true )

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

Arguments

  • $fullPath (string) – The path to ensure the folder exists at.
  • $volume (craft\base\Volume)
  • $justRecord (boolean) – If set to false, will also make sure the physical folder exists on Volume.

Returns

integer

Throws

Signature

public integer ensureFolderByFullPathAndVolume ( \craft\services\string $fullPath, craft\base\Volume $volume, \craft\services\bool $justRecord = true )

findFolder()

Finds the first folder that matches a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

craft\models\VolumeFolder, null

Signature

public craft\models\VolumeFolder, null findFolder ( $criteria = null )

findFolders()

Finds folders that match a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

craft\models\VolumeFolder[]

Signature

public craft\models\VolumeFolder[] findFolders ( $criteria = null )

getAllDescendantFolders()

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

View source

Arguments

Returns

array

Signature

public array getAllDescendantFolders ( craft\models\VolumeFolder $parentFolder, \craft\services\string $orderBy = 'path' )

getAssetById()

Returns a file by its ID.

View source

Arguments

Returns

craft\elements\Asset, null

Signature

public craft\elements\Asset, null getAssetById ( \craft\services\int $assetId, \craft\services\int $siteId = null )

getAssetUrl()

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

View source

Arguments

Returns

string, null

Signature

public string, null getAssetUrl ( craft\elements\Asset $asset, $transform = null, \craft\services\bool $generateNow = null )

getCurrentUserTemporaryUploadFolder()

Return the current user's temporary upload folder.

View source

Returns

craft\models\VolumeFolder

Signature

public craft\models\VolumeFolder getCurrentUserTemporaryUploadFolder ( )

getFolderById()

Returns a folder by its ID.

View source

Arguments

Returns

craft\models\VolumeFolder, null

Signature

public craft\models\VolumeFolder, null getFolderById ( \craft\services\int $folderId )

getFolderTreeByFolderId()

Get the folder tree for Assets by a folder id.

View source

Arguments

Returns

array

Signature

public array getFolderTreeByFolderId ( \craft\services\int $folderId )

getFolderTreeByVolumeIds()

Get the folder tree for Assets by volume ids

View source

Arguments

  • $allowedVolumeIds (array)
  • $additionalCriteria (array) – Additional criteria for filtering the tree

Returns

array

Signature

public array getFolderTreeByVolumeIds ( $allowedVolumeIds, array $additionalCriteria = [] )

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

Arguments

Returns

string

Signature

public string getIconPath ( craft\elements\Asset $asset )

getNameReplacementInFolder()

Find a replacement for a filename

View source

Arguments

  • $originalFilename (string) – The original filename for which to find a replacement.
  • $folderId (integer) – THe folder in which to find the replacement

Returns

string – If a suitable filename replacement cannot be found.

Throws

Signature

public string getNameReplacementInFolder ( \craft\services\string $originalFilename, \craft\services\int $folderId )

getRootFolderByVolumeId()

Returns the root folder for a given volume ID.

View source

Arguments

  • $volumeId (integer) – The volume ID

Returns

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

Signature

public craft\models\VolumeFolder, null getRootFolderByVolumeId ( \craft\services\int $volumeId )

getThumbPath()

Returns the CP thumbnail path for a given asset.

See also getThumbUrl()View source

Arguments

  • $asset (craft\elements\Asset) – Asset to return a thumb for
  • $width (integer) – Width of the returned thumb
  • $height (integer, null) – Height of the returned thumb (defaults to $width if null)
  • $generate (boolean) – Whether to generate a thumb in none exists yet
  • $fallbackToIcon (boolean) – Whether to return the path to a generic icon if a thumbnail can't be generated

Returns

string, false – Thumbnail path, or false if it doesn't exist and $generate is false

Throws

Signature

public string, false getThumbPath ( craft\elements\Asset $asset, \craft\services\int $width, \craft\services\int $height = null, \craft\services\bool $generate = true, \craft\services\bool $fallbackToIcon = true )

getThumbUrl()

Returns the CP thumbnail URL for a given asset.

See also craft\elements\Asset::getThumbUrl()View source

Arguments

  • $asset (craft\elements\Asset) – Asset to return a thumb for
  • $width (integer) – Width of the returned thumb
  • $height (integer, null) – Height of the returned thumb (defaults to $width if null)
  • $generate (boolean) – Whether to generate a thumb in none exists yet
  • $fallbackToIcon (boolean) – Whether to return the URL to a generic icon if a thumbnail can't be generated

Returns

string

Throws

Signature

public string getThumbUrl ( craft\elements\Asset $asset, \craft\services\int $width, \craft\services\int $height = null, \craft\services\bool $generate = false, \craft\services\bool $fallbackToIcon = true )

getTotalAssets()

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

View source

Arguments

  • $criteria (mixed)

Returns

integer

Signature

public integer getTotalAssets ( $criteria = null )

getTotalFolders()

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

View source

Arguments

  • $criteria (mixed)

Returns

integer

Signature

public integer getTotalFolders ( $criteria )

getUserTemporaryUploadFolder()

Get the user's temporary upload folder.

View source

Arguments

Returns

craft\models\VolumeFolder

Signature

public craft\models\VolumeFolder getUserTemporaryUploadFolder ( craft\elements\User $userModel = null )

moveAsset()

Move or rename an Asset.

View source

Arguments

Returns

boolean – Whether the asset was renamed successfully

Throws

Signature

public boolean moveAsset ( craft\elements\Asset $asset, craft\models\VolumeFolder $folder, \craft\services\string $filename = '' )

renameFolderById()

Rename a folder by it's id.

View source

Arguments

Returns

string – The new folder name after cleaning it.

Throws

Signature

public string renameFolderById ( \craft\services\int $folderId, \craft\services\string $newName )

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

Arguments

Throws

Signature

public void replaceAssetFile ( craft\elements\Asset $asset, \craft\services\string $pathOnServer, \craft\services\string $filename )

storeFolderRecord()

Store a folder by model

View source

Arguments

Signature

public void storeFolderRecord ( craft\models\VolumeFolder $folder )

Events

EVENT_AFTER_REPLACE_ASSET

Type
\craft\services\AssetEvent

The event that is triggered after an asset is replaced.

EVENT_BEFORE_REPLACE_ASSET

Type
\craft\services\AssetEvent

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.