OrderElementTrait

Type
Trait
Namespace
craft\commerce\elements\traits
Implemented by
craft\commerce\elements\Order

View source

# Public Methods

Method Description
find() Creates an \ElementQueryInterface instance for query purpose.
getFieldLayout() Returns the field layout used by this element.
getSearchKeywords() Returns the search keywords for a given search attribute.
hasContent() Returns whether elements of this type will be storing any data in the content table (titles or custom fields).

# find()

Creates an \ElementQueryInterface instance for query purpose.

The returned \ElementQueryInterface instance can be further customized by calling methods defined in \ElementQueryInterface before one() or all() is called to return populated \ElementInterface instances. For example,

// Find the entry whose ID is 5
$entry = Entry::find()->id(5)->one();
// Find all assets and order them by their filename:
$assets = Asset::find()
    ->orderBy('filename')
    ->all();

If you want to define custom criteria parameters for your elements, you can do so by overriding this method and returning a custom query class. For example,

class Product extends Element
{
    public static function find()
    {
        // use ProductQuery instead of the default ElementQuery
        return new ProductQuery(get_called_class());
    }
}

You can also set default criteria parameters on the ElementQuery if you don’t have a need for a custom query class. For example,

class Customer extends ActiveRecord
{
    public static function find()
    {
        return parent::find()->limit(50);
    }
}

View source

Returns

craft\commerce\elements\db\OrderQuery – The newly created \OrderQuery instance.

# getFieldLayout()

Returns the field layout used by this element.

View source

Returns

craft\models\FieldLayout, null

# getSearchKeywords()

Returns the search keywords for a given search attribute.

View source

Arguments

Returns

string

# hasContent()

Returns whether elements of this type will be storing any data in the content table (titles or custom fields).

View source

Returns

boolean – Whether elements of this type will be storing any data in the content table.

# Protected Methods

Method Description
defineActions() Defines the available element actions for a given source.
defineDefaultTableAttributes() Returns the list of table attribute keys that should be shown by default.
defineSearchableAttributes() Defines which element attributes should be searchable.
defineSortOptions() Returns the sort options for the element type.
defineSources() Defines the sources that elements of this type may belong to.
defineTableAttributes() Defines all of the available columns that can be shown in table views.
tableAttributeHtml() Returns the HTML that should be shown for a given attribute in Table View.

# defineActions()

Defines the available element actions for a given source.

View source

Arguments

  • $source (string, null) – The selected source’s key, if any.

Returns

array – The available element actions.

# defineDefaultTableAttributes()

Returns the list of table attribute keys that should be shown by default.

View source

Arguments

  • $source (string) – The selected source’s key

Returns

string[] – The table attributes.

# defineSearchableAttributes()

Defines which element attributes should be searchable.

View source

Returns

string[] – The element attributes that should be searchable

# defineSortOptions()

Returns the sort options for the element type.

View source

Returns

array – The attributes that elements can be sorted by

# defineSources()

Defines the sources that elements of this type may belong to.

View source

Arguments

  • $context (string, null) – The context ('index' or 'modal').

Returns

array – The sources.

# defineTableAttributes()

Defines all of the available columns that can be shown in table views.

View source

Returns

array – The table attributes.

# tableAttributeHtml()

Returns the HTML that should be shown for a given attribute in Table View.

This method can be used to completely customize what actually shows up within the table’s body for a given attribute, rather than simply showing the attribute’s raw value.

For example, if your elements have an email attribute that you want to wrap in a mailto: link, your getTableAttributesHtml() method could do this:

switch ($attribute) {
    case 'email':
        return $this->email ? Html::mailto(Html::encode($this->email)) : '';
    // ...
}
return parent::tableAttributeHtml($attribute);

WARNING

All untrusted text should be passed through Html::encode() to prevent XSS attacks.

By default the following will be returned:

  • If the attribute name is link or uri, it will be linked to the front-end URL.
  • If the attribute is a custom field handle, it will pass the responsibility off to the field type.
  • If the attribute value is a DateTime object, the date will be formatted with a localized date format.
  • For anything else, it will output the attribute value as a string.

View source

Arguments

  • $attribute (string) – The attribute name.

Returns

string – The HTML that should be shown for a given attribute in Table View.

Throws