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 canonical 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.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
purgeUnsavedDrafts()Deletes any unpublished drafts that were never formally saved.
removeDraftData()Removes draft data from the given draft.
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 ​

  • $draft (\craft\services\T) – The draft

Returns ​

\craft\services\T – The canonical element with the draft applied to it

Throws ​

createDraft() ​

Creates a new draft for the given element.

View source

Arguments ​

  • $canonical (\craft\services\T) – The element to create a draft for
  • $creatorId (integer, null) – 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\services\T – The new draft

Throws ​

generateDraftName() ​

Since
3.6.5

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

View source

Arguments ​

  • $canonicalId (integer) – The canonical 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 ​

purgeUnsavedDrafts() ​

Deletes any unpublished drafts that were never formally saved.

View source

removeDraftData() ​

Since
4.0.0

Removes draft data from the given draft.

View source

Arguments ​

Throws ​

saveElementAsDraft() ​

Saves an element as a draft.

View source

Arguments ​

Returns ​

boolean

Throws ​

Events ​

EVENT_AFTER_APPLY_DRAFT ​

Type
craft\events\DraftEvent

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_BEFORE_APPLY_DRAFT ​

Type
craft\events\DraftEvent
Since
3.6.0

The event that is triggered before a draft is published.


EVENT_BEFORE_CREATE_DRAFT ​

Type
craft\events\DraftEvent

The event that is triggered before a draft is created.