Skip to content

Volumes ​

Type
Class
Namespace
craft\services
Inherits
craft\services\Volumes » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

Volumes service.

An instance of the service is available via Craft::$app->volumes().

View source

Public Properties ​

PropertyDescription
allVolumeIdsinteger[]
allVolumeTypesstring[]
allVolumescraft\models\Volume[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.
publicVolumeIdsinteger[]
publicVolumescraft\models\Volume[]
temporaryVolumecraft\models\Volume
totalViewableVolumesinteger
totalVolumesinteger
userPhotoVolumecraft\models\Volume, null
viewableVolumeIdsarray
viewableVolumescraft\models\Volume[]

allVolumeIds ​

Type
integer[]
Default value
null

View source

allVolumeTypes ​

Type
string[]
Default value
null

View source

allVolumes ​

Type
craft\models\Volume[]
Default value
null

View source

publicVolumeIds ​

Type
integer[]
Default value
null

View source

publicVolumes ​

Type
craft\models\Volume[]
Default value
null

View source

temporaryVolume ​

Type
craft\models\Volume
Default value
null
Access
Read-only
Since
4.0.0

View source

totalViewableVolumes ​

Type
integer
Default value
null

View source

totalVolumes ​

Type
integer
Default value
null

View source

userPhotoVolume ​

Type
craft\models\Volume, null
Default value
null
Access
Read-only

View source

viewableVolumeIds ​

Type
array
Default value
null

View source

viewableVolumes ​

Type
craft\models\Volume[]
Default value
null

View source

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 a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__serialize()Serializer
__set()Sets the value of a component property.
__unset()Sets a component property to be 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.
createVolumeConfig()Returns the config for the given volume.
deleteVolume()Deletes an asset volume.
deleteVolumeById()Deletes an asset volume 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.
ensureTopFolder()Ensures a top level folder exists that matches the model.
getAllVolumeIds()Returns all of the volume IDs.
getAllVolumes()Returns all volumes.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getTemporaryVolume()
getTotalViewableVolumes()Returns the total number of volumes that are viewable by the current user.
getTotalVolumes()Returns the total number of volumes.
getUserPhotoVolume()Get the user photo volume.
getViewableVolumeIds()Returns all volume IDs that are viewable by the current user.
getViewableVolumes()Returns all volumes that are viewable by the current user.
getVolumeByHandle()Returns a volume by its handle.
getVolumeById()Returns a volume by its ID.
getVolumeByUid()Returns a volume by its UID.
handleChangedVolume()Handle volume change
handleDeletedVolume()Handle volume getting deleted
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 for this component.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
pruneDeletedField()
reorderVolumes()Reorders asset volumes.
saveVolume()Creates or updates a volume.
trigger()Triggers an event.

__serialize() ​

Since
3.5.14

Serializer

View source

createVolumeConfig() ​

DEPRECATED

Deprecated in 4.0.0. Use craft\models\Volume::getConfig() instead.

Since
3.5.0

Returns the config for the given volume.

View source

Arguments ​

Returns ​

array

deleteVolume() ​

Deletes an asset volume.

View source

Arguments ​

Returns ​

boolean

Throws ​

deleteVolumeById() ​

Deletes an asset volume by its ID.

View source

Arguments ​

Returns ​

boolean

Throws ​

ensureTopFolder() ​

DEPRECATED

Deprecated in 4.5.0. craft\services\Assets::getRootFolderByVolumeId() should be used instead.

Ensures a top level folder exists that matches the model.

View source

Arguments ​

Returns ​

craft\models\VolumeFolder

getAllVolumeIds() ​

Returns all of the volume IDs.

View source

Returns ​

integer[]

getAllVolumes() ​

Returns all volumes.

View source

Returns ​

craft\models\Volume[]

getTemporaryVolume() ​

Since
4.0.0

View source

Returns ​

craft\models\Volume

Throws ​

getTotalViewableVolumes() ​

Returns the total number of volumes that are viewable by the current user.

View source

Returns ​

integer

getTotalVolumes() ​

Returns the total number of volumes.

View source

Returns ​

integer

getUserPhotoVolume() ​

Get the user photo volume.

View source

Returns ​

craft\models\Volume, null

getViewableVolumeIds() ​

Returns all volume IDs that are viewable by the current user.

View source

Returns ​

array

getViewableVolumes() ​

Returns all volumes that are viewable by the current user.

View source

Returns ​

craft\models\Volume[]

getVolumeByHandle() ​

Returns a volume by its handle.

View source

Arguments ​

Returns ​

craft\models\Volume, null

getVolumeById() ​

Returns a volume by its ID.

View source

Arguments ​

Returns ​

craft\models\Volume, null

getVolumeByUid() ​

Returns a volume by its UID.

View source

Arguments ​

Returns ​

craft\models\Volume, null

handleChangedVolume() ​

Handle volume change

View source

Arguments ​

handleDeletedVolume() ​

Handle volume getting deleted

View source

Arguments ​

pruneDeletedField() ​

DEPRECATED

Deprecated in 4.0.5. Unused fields will be pruned automatically as field layouts are resaved.

View source

reorderVolumes() ​

Reorders asset volumes.

View source

Arguments ​

Returns ​

boolean

Throws ​

saveVolume() ​

Creates or updates a volume.

View source

Arguments ​

  • $volume (craft\models\Volume) – The volume to be saved.
  • $runValidation (boolean) – Whether the volume should be validated

Returns ​

boolean – Whether the volume was saved successfully

Throws ​


Example ​

php
use craft\volumes\Local;

$volume = new Local([
    'name' => 'Content Images',
    'handle' => 'contentImages',
    'fs' => 'localFs',
]);

if (!Craft::$app->volumes->saveVolume(($volume))) {
    throw new Exception('Couldn’t save volume.');
}

Events ​

EVENT_AFTER_DELETE_VOLUME ​

Type
craft\events\VolumeEvent

The event that is triggered after a volume is deleted.


EVENT_AFTER_SAVE_VOLUME ​

Type
craft\events\VolumeEvent

The event that is triggered after a volume is saved.


EVENT_BEFORE_APPLY_VOLUME_DELETE ​

Type
craft\events\VolumeEvent
Since
3.1.0

The event that is triggered before a volume delete is applied to the database.


EVENT_BEFORE_DELETE_VOLUME ​

Type
craft\events\VolumeEvent

The event that is triggered before a volume is deleted.


EVENT_BEFORE_SAVE_VOLUME ​

Type
craft\events\VolumeEvent

The event that is triggered before a volume is saved.