Skip to content

OrderElementTrait ​

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

View source

Public Methods ​

MethodDescription
createCondition()Returns an element condition for the element type.
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.
modifyCustomSource()Modifies a custom source’s config, before it’s returned by craft\services\ElementSources::getSources()
prepElementQueryForTableAttribute()Prepares an element query for an element index that includes a given table attribute.

createCondition() ​

Returns an element condition for the element type.

View source

Returns ​

craft\commerce\elements\conditions\orders\OrderCondition

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,

php
// 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,

php
class Product extends Element
{
    public static function find(): ElementQueryInterface
    {
        // 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,

php
class Customer extends ActiveRecord
{
    public static function find(): ElementQueryInterface
    {
        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

modifyCustomSource() ​

Modifies a custom source’s config, before it’s returned by craft\services\ElementSources::getSources()

View source

Arguments ​

Returns ​

array

prepElementQueryForTableAttribute() ​

Prepares an element query for an element index that includes a given table attribute.

View source

Arguments ​

Protected Methods ​

MethodDescription
attributeHtml()Returns the HTML that should be shown for a given attribute in table and card views.
defineActions()Defines the available bulk element actions for a given source.
defineDefaultTableAttributes()Returns the list of table attribute keys that should be shown by default.
defineExporters()Defines the available element exporters for a given source.
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.
htmlAttributes()Returns any attributes that should be included in the element’s chips and cards.

attributeHtml() ​

Returns the HTML that should be shown for a given attribute in table and card views.

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

php
return match ($attribute) {
    'email' => $this->email ? Html::mailto(Html::encode($this->email)) : '',
    default => parent::attributeHtml($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 and card views.

Throws ​

defineActions() ​

Defines the available bulk element actions for a given source.

View source

Arguments ​

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

Returns ​

array – The available bulk 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.

defineExporters() ​

Defines the available element exporters for a given source.

View source

Arguments ​

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

Returns ​

array – The available element exporters

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) – The context ('index', 'modal', 'field', or 'settings').

Returns ​

array – The sources.

Throws ​

defineTableAttributes() ​

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

View source

Returns ​

array – The table attributes.

htmlAttributes() ​

Returns any attributes that should be included in the element’s chips and cards.

View source

Arguments ​

  • $context (string) – The context that the element is being rendered in ('index', 'modal', 'field', or 'settings'.)

Returns ​

array