Skip to content

Drafts ​

Type
Class
Namespace
craft\services
Inherits
craft\services\Drafts » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.2.0

Drafts service.

An instance of the service is available via Craft::$app->drafts.

View source

Public Properties ​

PropertyDescription
behaviorsyii\base\Behavior – List of behaviors attached to this component.
dbcraft\db\Connection, array, string – The database connection to use

db ​

Type
craft\db\Connection, array, string
Default value
'db'
Since
3.5.4

The database connection to use

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.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
applyDraft()Applies a draft to its canonical element, and deletes the draft.
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.
createDraft()Creates a new draft for the given element.
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.
generateDraftName()Returns the next auto-generated draft name that should be assigned, for the given source element.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getEditableDrafts()Returns drafts for a given element ID that the current user is allowed to edit
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.
insertDraftRow()Creates a new row in the drafts table.
mergeSourceChanges()Merges recent source element changes into a draft.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
publishDraft()Applies a draft to its canonical element, and deletes the draft.
purgeUnsavedDrafts()Deletes any sourceless drafts that were never formally saved.
saveElementAsDraft()Saves an element as a draft.
trigger()Triggers an event.

applyDraft() ​

Since
3.6.0

Applies a draft to its canonical element, and deletes the draft.

If an unpublished draft is passed, its draft data will simply be removed from it.

View source

Arguments ​

Returns ​

craft\base\ElementInterface – The canonical element with the draft applied to it

Throws ​

createDraft() ​

Creates a new draft for the given element.

View source

Arguments ​

  • $source (craft\base\ElementInterface) – The element to create a draft for
  • $creatorId (integer) – The user ID that the draft should be attributed to
  • $name (string, null) – The draft name
  • $notes (string, null) – The draft notes
  • $newAttributes (array) – Any attributes to apply to the draft
  • $provisional (boolean) – Whether to create a provisional draft

Returns ​

craft\base\ElementInterface – The new draft

Throws ​

generateDraftName() ​

Since
3.6.5

Returns the next auto-generated draft name that should be assigned, for the given source element.

View source

Arguments ​

  • $sourceId (integer) – The source element’s ID

Returns ​

string

getEditableDrafts() ​

Returns drafts for a given element ID that the current user is allowed to edit

View source

Arguments ​

Returns ​

craft\base\ElementInterface[]

init() ​

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

insertDraftRow() ​

Since
3.6.4

Creates a new row in the drafts table.

View source

Arguments ​

Returns ​

integer – The new draft ID

Throws ​

mergeSourceChanges() ​

DEPRECATED

Deprecated in 3.7.0. Use craft\services\Elements::mergeCanonicalChanges() instead.

Since
3.4.0

Merges recent source element changes into a draft.

View source

Arguments ​

publishDraft() ​

DEPRECATED

Deprecated in 3.7.0. Use applyDraft() instead.

Applies a draft to its canonical element, and deletes the draft.

If an unpublished draft is passed, its draft data will simply be removed from it.

View source

Arguments ​

Returns ​

craft\base\ElementInterface – The canonical element with the draft applied to it

Throws ​

purgeUnsavedDrafts() ​

Deletes any sourceless drafts that were never formally saved.

View source

Returns ​

void

saveElementAsDraft() ​

Saves an element as a draft.

View source

Arguments ​

Returns ​

boolean

Throws ​

Events ​

EVENT_AFTER_APPLY_DRAFT ​

Type
craft\events\DraftEvent
Since
3.1.0

The event that is triggered after a draft is applied to its canonical element.

See also applyDraft()


EVENT_AFTER_CREATE_DRAFT ​

Type
craft\events\DraftEvent

The event that is triggered after a draft is created.


EVENT_AFTER_MERGE_SOURCE_CHANGES ​

DEPRECATED

Deprecated in 3.7.0. Use craft\services\Elements::EVENT_AFTER_MERGE_CANONICAL_CHANGES instead.

Type
craft\events\DraftEvent
Since
3.4.0

The event that is triggered after source changes are merged into a draft.


EVENT_AFTER_PUBLISH_DRAFT ​

DEPRECATED

Deprecated in 3.7.0. Use EVENT_AFTER_APPLY_DRAFT instead.

Type
craft\events\DraftEvent
Since
3.6.0

The event that is triggered after a draft is applied to its canonical element.


EVENT_BEFORE_APPLY_DRAFT ​

Type
craft\events\DraftEvent
Since
3.1.0

The event that is triggered before a draft is applied to its canonical element.

See also applyDraft()


EVENT_BEFORE_CREATE_DRAFT ​

Type
craft\events\DraftEvent

The event that is triggered before a draft is created.


EVENT_BEFORE_MERGE_SOURCE_CHANGES ​

DEPRECATED

Deprecated in 3.7.0. Use craft\services\Elements::EVENT_BEFORE_MERGE_CANONICAL_CHANGES instead.

Type
craft\events\DraftEvent
Since
3.4.0

The event that is triggered before source changes are merged into a draft.


EVENT_BEFORE_PUBLISH_DRAFT ​

DEPRECATED

Deprecated in 3.7.0. Use EVENT_BEFORE_APPLY_DRAFT instead.

Type
craft\events\DraftEvent
Since
3.6.0

The event that is triggered before a draft is applied to its canonical element.