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().
Public Properties ​
| Property | Description |
|---|---|
| allVolumeIds | integer[] |
| allVolumeTypes | string[] |
| allVolumes | craft\base\VolumeInterface[] |
| behaviors | yii\base\Behavior – List of behaviors attached to this component. |
| publicVolumeIds | integer[] |
| publicVolumes | craft\base\VolumeInterface[] |
| totalViewableVolumes | integer |
| totalVolumes | integer |
| viewableVolumeIds | array |
| viewableVolumes | craft\base\VolumeInterface[] |
allVolumeIds ​
- Type
- integer[]
- Default value
null- Access
- Read-only
allVolumeTypes ​
- Type
- string[]
- Default value
null- Access
- Read-only
allVolumes ​
- Type
- craft\base\VolumeInterface[]
- Default value
null- Access
- Read-only
publicVolumeIds ​
- Type
- integer[]
- Default value
null- Access
- Read-only
publicVolumes ​
- Type
- craft\base\VolumeInterface[]
- Default value
null- Access
- Read-only
totalViewableVolumes ​
- Type
- integer
- Default value
null- Access
- Read-only
totalVolumes ​
- Type
- integer
- Default value
null- Access
- Read-only
viewableVolumeIds ​
- Type
- array
- Default value
null- Access
- Read-only
viewableVolumes ​
- Type
- craft\base\VolumeInterface[]
- Default value
null- Access
- Read-only
Public Methods ​
| Method | Description |
|---|---|
| __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. |
| createVolume() | Creates an asset volume with a given config. |
| 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. |
| getAllVolumeTypes() | Returns all registered volume types. |
| getAllVolumes() | Returns all volumes. |
| getBehavior() | Returns the named behavior object. |
| getBehaviors() | Returns all behaviors attached to this component. |
| getPublicVolumeIds() | Returns all volume IDs that have public URLs. |
| getPublicVolumes() | Returns all volumes that have public URLs. |
| getTotalViewableVolumes() | Returns the total number of volumes that are viewable by the current user. |
| getTotalVolumes() | Returns the total number of volumes. |
| 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. |
| getVolumeOverrides() | Returns any custom volume config values. |
| 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
createVolume() ​
Creates an asset volume with a given config.
Arguments ​
$config(mixed) – The asset volume’s class name, or its config, with atypevalue and optionally asettingsvalue
Returns ​
craft\base\VolumeInterface – The asset volume
createVolumeConfig() ​
- Since
- 3.5.0
Returns the config for the given volume.
Arguments ​
$volume(craft\base\VolumeInterface)
Returns ​
deleteVolume() ​
Deletes an asset volume.
Arguments ​
$volume(craft\base\VolumeInterface) – The volume to delete
Returns ​
Throws ​
deleteVolumeById() ​
Deletes an asset volume by its ID.
Arguments ​
$volumeId(integer)
Returns ​
Throws ​
ensureTopFolder() ​
Ensures a top level folder exists that matches the model.
Arguments ​
$volume(craft\base\VolumeInterface)
Returns ​
getAllVolumeIds() ​
Returns all of the volume IDs.
Returns ​
integer[]
getAllVolumeTypes() ​
Returns all registered volume types.
Returns ​
string[]
getAllVolumes() ​
Returns all volumes.
Returns ​
getPublicVolumeIds() ​
Returns all volume IDs that have public URLs.
Returns ​
integer[]
getPublicVolumes() ​
Returns all volumes that have public URLs.
Returns ​
getTotalViewableVolumes() ​
Returns the total number of volumes that are viewable by the current user.
Returns ​
getTotalVolumes() ​
Returns the total number of volumes.
Returns ​
getViewableVolumeIds() ​
Returns all volume IDs that are viewable by the current user.
Returns ​
getViewableVolumes() ​
Returns all volumes that are viewable by the current user.
Returns ​
getVolumeByHandle() ​
Returns a volume by its handle.
Arguments ​
$handle(string)
Returns ​
craft\base\VolumeInterface, null
getVolumeById() ​
Returns a volume by its ID.
Arguments ​
$volumeId(integer)
Returns ​
craft\base\VolumeInterface, null
getVolumeByUid() ​
Returns a volume by its UID.
Arguments ​
$volumeUid(string)
Returns ​
craft\base\VolumeInterface, null
getVolumeOverrides() ​
DEPRECATED
Deprecated in 3.5.8. Environment variables or dependency injection should be used instead.
Returns any custom volume config values.
Arguments ​
$handle(string) – The volume handle
Returns ​
handleChangedVolume() ​
Handle volume change
Arguments ​
$event(craft\events\ConfigEvent)
handleDeletedVolume() ​
Handle volume getting deleted
Arguments ​
$event(craft\events\ConfigEvent)
pruneDeletedField() ​
DEPRECATED
Deprecated in 3.7.51. Unused fields will be pruned automatically as field layouts are resaved.
reorderVolumes() ​
Reorders asset volumes.
Arguments ​
$volumeIds(array)
Returns ​
Throws ​
saveVolume() ​
Creates or updates a volume.
Arguments ​
$volume(craft\base\VolumeInterface) – The volume to be saved.$runValidation(boolean) – Whether the volume should be validated
Returns ​
boolean – Whether the volume was saved successfully
Throws ​
Example ​
use craft\volumes\Local;
$volume = new Local([
'name' => 'Content Images',
'handle' => 'contentImages',
'hasUrls' => true,
'url' => '$CONTENT_IMAGES_URL',
'path' => '$CONTENT_IMAGES_PATH',
]);
if (!Craft::$app->volumes->saveVolume(($volume))) {
throw new Exception('Couldn’t save volume.');
}Constants ​
| Constant | Description |
|---|---|
CONFIG_VOLUME_KEY |
Events ​
EVENT_AFTER_DELETE_VOLUME ​
The event that is triggered after a volume is deleted.
EVENT_AFTER_SAVE_VOLUME ​
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 ​
The event that is triggered before a volume is deleted.
EVENT_BEFORE_SAVE_VOLUME ​
The event that is triggered before a volume is saved.
EVENT_REGISTER_VOLUME_TYPES ​
The event that is triggered when registering volume types.
Volume types must implement craft\base\VolumeInterface. \craft\services\Volume provides a base implementation.
See Volume Types for documentation on creating volume types.
Example ​
use craft\events\RegisterComponentTypesEvent;
use craft\services\Volumes;
use yii\base\Event;
Event::on(Volumes::class,
Volumes::EVENT_REGISTER_VOLUME_TYPES,
function(RegisterComponentTypesEvent $event) {
$event->types[] = MyVolumeType::class;
}
);