CraftVariable

Type
Class
Namespace
craft\web\twig\variables
Inherits
craft\web\twig\variables\CraftVariable » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

Craft defines the craft global template variable.

View source

Public Properties

PropertyDescription
$appcraft\web\Application, craft\console\Application, null – The Craft application class
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$componentsarray – The list of the component definitions or the loaded component instances (ID => definition or instance).

$app

Signature

public craft\web\Application, craft\console\Application, null $app = 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.
assets()Returns a new asset query.
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.
categories()Returns a new category query.
className()Returns the fully qualified name of this class.
clear()Removes the component from the locator.
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.
entries()Returns a new entry query.
get()Returns the component instance with the specified ID.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getComponents()Returns the list of the component definitions or the loaded component instances.
globalSets()Returns a new global set query.
has()Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
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.
isLocalized()Returns whether this site has multiple locales.
locale()Gets the current language in use.
matrixBlocks()Returns a new Matrix block query.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
query()Returns a new generic query.
set()Registers a component definition with this locator.
setComponents()Registers a set of component definitions in this locator.
tags()Returns a new tag query.
trigger()Triggers an event.
users()Returns a new user query.

__call()

Calls the named method which is not a class method.

This method will check if any attached behavior has the named method and will execute it if available.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

View source

Arguments

  • $name (string) – The method name
  • $params (array) – Method parameters

Returns

mixed – The method return value

Throws

Signature

public mixed __call ( $name, $params )

__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.

View source

Arguments

  • $config (array) – Name-value pairs that will be used to initialize the object properties

Signature

public void __construct ( $config = [] )

assets()

Returns a new asset query.

View source

Arguments

Returns

craft\elements\db\AssetQuery

Signature

public craft\elements\db\AssetQuery assets ( array $criteria = [] )

behaviors()

Returns a list of behaviors that this component should behave as.

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

Note that a behavior class must extend from \craft\web\twig\variables\Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using getBehavior() or be detached using detachBehavior(). Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

View source

Returns

array – The behavior configurations.

Signature

public array behaviors ( )

canGetProperty()

Returns a value indicating whether a property can be read.

A property can be read if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);
  • an attached behavior has a readable property of the given name (when $checkBehaviors is true).

View source

Arguments

  • $name (string) – The property name
  • $checkVars (boolean) – Whether to treat member variables as properties
  • $checkBehaviors (boolean) – Whether to treat behaviors' properties as properties of this component

Returns

boolean – Whether the property can be read

Signature

public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true )

categories()

Returns a new category query.

View source

Arguments

Returns

craft\elements\db\CategoryQuery

Signature

public craft\elements\db\CategoryQuery categories ( array $criteria = [] )

entries()

Returns a new entry query.

View source

Arguments

Returns

craft\elements\db\EntryQuery

Signature

public craft\elements\db\EntryQuery entries ( array $criteria = [] )

globalSets()

Returns a new global set query.

View source

Arguments

Returns

craft\elements\db\GlobalSetQuery

Signature

public craft\elements\db\GlobalSetQuery globalSets ( array $criteria = [] )

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

Signature

public void init ( )

isLocalized()

DEPRECATED

Deprecated in 3.0. Use craft.app.isMultiSite instead

Returns whether this site has multiple locales.

View source

Returns

boolean

Signature

public boolean isLocalized ( )

locale()

DEPRECATED

Deprecated in 3.0

Gets the current language in use.

View source

Returns

string

Signature

public string locale ( )

matrixBlocks()

Signature

public craft\elements\db\MatrixBlockQuery matrixBlocks ( array $criteria = [] )

query()

Returns a new generic query.

View source

Returns

craft\db\Query

Signature

public craft\db\Query query ( )

tags()

Returns a new tag query.

View source

Arguments

Returns

craft\elements\db\TagQuery

Signature

public craft\elements\db\TagQuery tags ( array $criteria = [] )

users()

Returns a new user query.

View source

Arguments

Returns

craft\elements\db\UserQuery

Signature

public craft\elements\db\UserQuery users ( array $criteria = [] )

Events

EVENT_DEFINE_BEHAVIORS

Type
craft\events\DefineBehaviorsEvent

The event that is triggered when defining the class behaviors

See also behaviors()

EVENT_DEFINE_COMPONENTS

DEPRECATED

Deprecated since 3.0.0-beta.23

Type
craft\events\DefineComponentsEvent

The event that is triggered when defining the Service Locator components.

See also __construct()

EVENT_INIT

Type
yii\base\Event

The event that is triggered after the component's init cycle

See also init()