Interface craft\base\ElementInterface
- Extends
- craft\base\ComponentInterface
- Implemented by
- craft\base\Element, craft\elements\Asset, craft\elements\Category, craft\elements\Entry, craft\elements\GlobalSet, craft\elements\MatrixBlock, craft\elements\Tag, craft\elements\User, craft\models\BaseEntryRevisionModel, craft\models\EntryDraft, craft\models\EntryVersion
- Available since version
- 3.0
- Source Code
- https://github.com/craftcms/cms/blob/master/src/base/ElementInterface.php
ElementInterface defines the common interface to be implemented by element classes.
A class implementing this interface should also use craft\base\ElementTrait and \craft\base\ContentTrait.
Method | Description | Defined By |
---|---|---|
actions() |
Returns the available element actions for a given source (if one is provided). | craft\base\ElementInterface |
afterDelete() |
Performs actions after an element is deleted. | craft\base\ElementInterface |
afterMoveInStructure() |
Performs actions after an element is moved within a structure. | craft\base\ElementInterface |
afterSave() |
Performs actions after an element is saved. | craft\base\ElementInterface |
beforeDelete() |
Performs actions before an element is deleted. | craft\base\ElementInterface |
beforeMoveInStructure() |
Performs actions before an element is moved within a structure. | craft\base\ElementInterface |
beforeSave() |
Performs actions before an element is saved. | craft\base\ElementInterface |
defaultTableAttributes() |
Returns the list of table attribute keys that should be shown by default. | craft\base\ElementInterface |
displayName() |
Returns the display name of this class. | craft\base\ComponentInterface |
eagerLoadingMap() |
Returns an array that maps source-to-target element IDs based on the given sub-property handle. | craft\base\ElementInterface |
find() |
Creates an craft\elements\db\ElementQueryInterface instance for query purpose. | craft\base\ElementInterface |
findAll() |
Returns a list of elements that match the specified ID(s) or a set of element criteria parameters. | craft\base\ElementInterface |
findOne() |
Returns a single element instance by a primary key or a set of element criteria parameters. | craft\base\ElementInterface |
getAncestors() |
Returns the element’s ancestors. | craft\base\ElementInterface |
getChildren() |
Returns the element’s children. | craft\base\ElementInterface |
getContentTable() |
Returns the name of the table this element’s content is stored in. | craft\base\ElementInterface |
getCpEditUrl() |
Returns the element’s CP edit URL. | craft\base\ElementInterface |
getDescendants() |
Returns the element’s descendants. | craft\base\ElementInterface |
getEditorHtml() |
Returns the HTML for the element’s editor HUD. | craft\base\ElementInterface |
getFieldColumnPrefix() |
Returns the field column prefix this element’s content uses. | craft\base\ElementInterface |
getFieldContext() |
Returns the field context this element’s content uses. | craft\base\ElementInterface |
getFieldLayout() |
Returns the field layout used by this element. | craft\base\ElementInterface |
getFieldParamNamespace() |
Returns the namespace used by custom field params on the request. | craft\base\ElementInterface |
getFieldValue() |
Returns the value for a given field. | craft\base\ElementInterface |
getFieldValues() |
Returns the element’s normalized custom field values, indexed by their handles. | craft\base\ElementInterface |
getHasDescendants() |
Returns whether the element has descendants. | craft\base\ElementInterface |
getHasFreshContent() |
Returns whether the element’s content is "fresh" (unsaved and without validation errors). | craft\base\ElementInterface |
getHtmlAttributes() |
Returns any attributes that should be included in the element’s DOM representation in the Control Panel. | craft\base\ElementInterface |
getId() |
Returns the element’s ID. | craft\base\ElementInterface |
getIsEditable() |
Returns whether the current user can edit the element. | craft\base\ElementInterface |
getLink() |
Returns an anchor pre-filled with this element’s URL and title. | craft\base\ElementInterface |
getNext() |
Returns the next element relative to this one, from a given set of criteria. | craft\base\ElementInterface |
getNextSibling() |
Returns the element’s next sibling. | craft\base\ElementInterface |
getParent() |
Returns the element’s parent. | craft\base\ElementInterface |
getPrev() |
Returns the previous element relative to this one, from a given set of criteria. | craft\base\ElementInterface |
getPrevSibling() |
Returns the element’s previous sibling. | craft\base\ElementInterface |
getRef() |
Returns the reference string to this element. | craft\base\ElementInterface |
getRoute() |
Returns the route that should be used when the element’s URI is requested. | craft\base\ElementInterface |
getSearchKeywords() |
Returns the search keywords for a given search attribute. | craft\base\ElementInterface |
getSerializedFieldValues() |
Returns an array of the element’s serialized custom field values, indexed by their handles. | craft\base\ElementInterface |
getSiblings() |
Returns all of the element’s siblings. | craft\base\ElementInterface |
getStatus() |
Returns the element’s status. | craft\base\ElementInterface |
getSupportedSites() |
Returns the sites this element is associated with. | craft\base\ElementInterface |
getTableAttributeHtml() |
Returns the HTML that should be shown for a given attribute in Table View. | craft\base\ElementInterface |
getThumbUrl() |
Returns the URL to the element’s thumbnail, if there is one. | craft\base\ElementInterface |
getTotalDescendants() |
Returns the total number of descendants that the element has. | craft\base\ElementInterface |
getUriFormat() |
Returns the URI format used to generate this element’s URI. | craft\base\ElementInterface |
getUrl() |
Returns the element’s full URL. | craft\base\ElementInterface |
hasContent() |
Returns whether elements of this type will be storing any data in the content table (tiles or custom fields). |
craft\base\ElementInterface |
hasStatuses() |
Returns whether elements of this type have statuses. | craft\base\ElementInterface |
hasTitles() |
Returns whether elements of this type have traditional titles. | craft\base\ElementInterface |
hasUris() |
Returns whether elements of this type can have their own slugs and URIs. | craft\base\ElementInterface |
indexHtml() |
Returns the element index HTML. | craft\base\ElementInterface |
isAncestorOf() |
Returns whether this element is an ancestor of another one. | craft\base\ElementInterface |
isChildOf() |
Returns whether this element is a direct child of another one. | craft\base\ElementInterface |
isDescendantOf() |
Returns whether this element is a descendant of another one. | craft\base\ElementInterface |
isLocalized() |
Returns whether elements of this type store content on a per-site basis. | craft\base\ElementInterface |
isNextSiblingOf() |
Returns whether this element is the direct next sibling of another one. | craft\base\ElementInterface |
isParentOf() |
Returns whether this element is a direct parent of another one. | craft\base\ElementInterface |
isPrevSiblingOf() |
Returns whether this element is the direct previous sibling of another one. | craft\base\ElementInterface |
isSiblingOf() |
Returns whether this element is a sibling of another one. | craft\base\ElementInterface |
offsetExists() |
Treats custom fields as array offsets. | craft\base\ElementInterface |
refHandle() |
Returns the handle that should be used to refer to this element type from reference tags. | craft\base\ElementInterface |
searchableAttributes() |
Defines which element attributes should be searchable. | craft\base\ElementInterface |
setFieldParamNamespace() |
Sets the namespace used by custom field params on the request. | craft\base\ElementInterface |
setFieldValue() |
Sets the value for a given field. | craft\base\ElementInterface |
setFieldValues() |
Sets the element’s custom field values. | craft\base\ElementInterface |
setFieldValuesFromRequest() |
Sets the element’s custom field values, when the values have come from post data. | craft\base\ElementInterface |
setNext() |
Sets the default next element. | craft\base\ElementInterface |
setParent() |
Sets the element’s parent. | craft\base\ElementInterface |
setPrev() |
Sets the default previous element. | craft\base\ElementInterface |
sortOptions() |
Returns the sort options for the element type. | craft\base\ElementInterface |
sources() |
Returns the source definitions that elements of this type may belong to. | craft\base\ElementInterface |
statuses() |
Returns all of the possible statuses that elements of this type may have. | craft\base\ElementInterface |
tableAttributes() |
Defines all of the available columns that can be shown in table views. | craft\base\ElementInterface |
Method Details
actions()
public abstract static method
#
Returns the available element actions for a given source (if one is provided).
The actions can either be represented by their class handle (e.g. 'SetStatus'), or by an craft\base\ElementActionInterface instance.
public abstract static array actions ( \craft\base\string $source )
$source |
string | The selected source’s key. |
return | array | The available element actions. |
---|
afterDelete()
public abstract method
#
Performs actions after an element is deleted.
public abstract void afterDelete ( )
afterMoveInStructure()
public abstract method
#
Performs actions after an element is moved within a structure.
public abstract void afterMoveInStructure ( \craft\base\int $structureId )
$structureId |
integer | The structure ID |
afterSave()
public abstract method
#
Performs actions after an element is saved.
public abstract void afterSave ( \craft\base\bool $isNew )
$isNew |
boolean | Whether the element is brand new |
beforeDelete()
public abstract method
#
Performs actions before an element is deleted.
public abstract boolean beforeDelete ( )
return | boolean | Whether the element should be deleted |
---|
beforeMoveInStructure()
public abstract method
#
Performs actions before an element is moved within a structure.
public abstract boolean beforeMoveInStructure ( \craft\base\int $structureId )
$structureId |
integer | The structure ID |
return | boolean | Whether the element should be moved within the structure |
---|
beforeSave()
public abstract method
#
Performs actions before an element is saved.
public abstract boolean beforeSave ( \craft\base\bool $isNew )
$isNew |
boolean | Whether the element is brand new |
return | boolean | Whether the element should be saved |
---|
defaultTableAttributes()
public abstract static method
#
Returns the list of table attribute keys that should be shown by default.
This method should return an array where each element in the array maps to one of the keys of the array returned by tableAttributes().
public abstract static string[] defaultTableAttributes ( \craft\base\string $source )
$source |
string | The selected source’s key |
return | string[] | The table attribute keys |
---|
eagerLoadingMap()
public abstract static method
#
Returns an array that maps source-to-target element IDs based on the given sub-property handle.
This method aids in the eager-loading of elements when performing an element query. The returned array should contain the following keys:
elementType
– the fully qualified class name of the element type that should be eager-loadedmap
– an array of element ID mappings, where each element is a sub-array withsource
andtarget
keys.criteria
(optional) – Any criteria parameters that should be applied to the element query when fetching the eager-loaded elements.
public abstract static array, false eagerLoadingMap ( array $sourceElements, \craft\base\string $handle )
$sourceElements |
craft\base\ElementInterface[] | An array of the source elements |
$handle |
string | The property handle used to identify which target elements should be included in the map |
return | array, false | The eager-loading element ID mappings, or false if no mappings exist |
---|
find()
public abstract static method
#
Creates an craft\elements\db\ElementQueryInterface instance for query purpose.
The returned craft\elements\db\ElementQueryInterface instance can be further customized by calling
methods defined in craft\elements\db\ElementQueryInterface before one()
or all()
is called to return
populated craft\base\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);
}
}
public abstract static craft\elements\db\ElementQueryInterface find ( )
return | craft\elements\db\ElementQueryInterface | The newly created craft\elements\db\ElementQueryInterface instance. |
---|
findAll()
public abstract static method
#
Returns a list of elements that match the specified ID(s) or a set of element criteria parameters.
The method accepts:
- an int: query by a single ID value and return an array containing the corresponding element (or an empty array if not found).
- an array of integers: query by a list of ID values and return the corresponding elements (or an empty array if none was found). Note that an empty array will result in an empty result as it will be interpreted as a search for primary keys and not an empty set of element criteria parameters.
- an array of name-value pairs: query by a set of parameter values and return an array of elements matching all of them (or an empty array if none was found).
Note that this method will automatically call the all()
method and return an array of
\craft\base\Element instances. For example,
// find the entries whose ID is 10
$entries = Entry::findAll(10);
// the above code is equivalent to:
$entries = Entry::find()->id(10)->all();
// find the entries whose ID is 10, 11 or 12.
$entries = Entry::findAll([10, 11, 12]);
// the above code is equivalent to:
$entries = Entry::find()->id([10, 11, 12]])->all();
// find users whose email ends in "example.com"
$users = User::findAll(['email' => '*example.com']);
// the above code is equivalent to:
$users = User::find()->email('*example.com')->all();
public abstract static static[] findAll ( $criteria = null )
$criteria |
mixed | The element ID, an array of IDs, or a set of element criteria parameters |
return | static[] | An array of Element instances, or an empty array if nothing matches. |
---|
findOne()
public abstract static method
#
Returns a single element instance by a primary key or a set of element criteria parameters.
The method accepts:
- an int: query by a single ID value and return the corresponding element (or null if not found).
- an array of name-value pairs: query by a set of parameter values and return the first element matching all of them (or null if not found).
Note that this method will automatically call the one()
method and return an
\craft\base\Element instance. For example,
// find a single entry whose ID is 10
$entry = Entry::findOne(10);
// the above code is equivalent to:
$entry = Entry::find->id(10)->one();
// find the first user whose email ends in "example.com"
$user = User::findOne(['email' => '*example.com']);
// the above code is equivalent to:
$user = User::find()->email('*example.com')->one();
public abstract static static, null findOne ( $criteria = null )
$criteria |
mixed | The element ID or a set of element criteria parameters |
return | static, null | Element instance matching the condition, or null if nothing matches. |
---|
getAncestors()
public abstract method
#
Returns the element’s ancestors.
public abstract craft\elements\db\ElementQueryInterface, craft\base\ElementInterface[] getAncestors ( \craft\base\int $dist = null )
$dist |
integer, null |
getChildren()
public abstract method
#
Returns the element’s children.
public abstract craft\elements\db\ElementQueryInterface, craft\base\ElementInterface[] getChildren ( )
getContentTable()
public abstract method
#
Returns the name of the table this element’s content is stored in.
public abstract string getContentTable ( )
getCpEditUrl()
public abstract method
#
Returns the element’s CP edit URL.
public abstract string, null getCpEditUrl ( )
getDescendants()
public abstract method
#
Returns the element’s descendants.
public abstract craft\elements\db\ElementQueryInterface, craft\base\ElementInterface[] getDescendants ( \craft\base\int $dist = null )
$dist |
integer, null |
getEditorHtml()
public abstract method
#
Returns the HTML for the element’s editor HUD.
public abstract string getEditorHtml ( )
return | string | The HTML for the editor HUD |
---|
getFieldColumnPrefix()
public abstract method
#
Returns the field column prefix this element’s content uses.
public abstract string getFieldColumnPrefix ( )
getFieldContext()
public abstract method
#
Returns the field context this element’s content uses.
public abstract string getFieldContext ( )
getFieldLayout()
public abstract method
#
Returns the field layout used by this element.
public abstract craft\models\FieldLayout, null getFieldLayout ( )
getFieldParamNamespace()
public abstract method
#
Returns the namespace used by custom field params on the request.
public abstract string, null getFieldParamNamespace ( )
return | string, null | The field param namespace |
---|
getFieldValue()
public abstract method
#
Returns the value for a given field.
public abstract mixed getFieldValue ( \craft\base\string $fieldHandle )
$fieldHandle |
string | The field handle whose value needs to be returned |
return | mixed | The field value |
---|
getFieldValues()
public abstract method
#
Returns the element’s normalized custom field values, indexed by their handles.
public abstract array getFieldValues ( array $fieldHandles = null )
$fieldHandles |
string[], null | The list of field handles whose values need to be returned. Defaults to null, meaning all fields’ values will be returned. If it is an array, only the fields in the array will be returned. |
return | array | The field values (handle => value) |
---|
getHasDescendants()
public abstract method
#
Returns whether the element has descendants.
public abstract boolean getHasDescendants ( )
getHasFreshContent()
public abstract method
#
Returns whether the element’s content is "fresh" (unsaved and without validation errors).
public abstract boolean getHasFreshContent ( )
return | boolean | Whether the element’s content is fresh |
---|
getHtmlAttributes()
public abstract method
#
Returns any attributes that should be included in the element’s DOM representation in the Control Panel.
public abstract array getHtmlAttributes ( \craft\base\string $context )
$context |
string | The context that the element is being rendered in ('index', 'field', etc.) |
getId()
public abstract method
#
Returns the element’s ID.
public abstract integer, null getId ( )
getIsEditable()
public abstract method
#
Returns whether the current user can edit the element.
public abstract boolean getIsEditable ( )
getLink()
public abstract method
#
Returns an anchor pre-filled with this element’s URL and title.
public abstract Twig_Markup, null getLink ( )
getNext()
public abstract method
#
Returns the next element relative to this one, from a given set of criteria.
public abstract craft\base\ElementInterface, null getNext ( $criteria = false )
$criteria |
mixed |
getNextSibling()
public abstract method
#
Returns the element’s next sibling.
public abstract craft\base\ElementInterface, null getNextSibling ( )
getParent()
public abstract method
#
Returns the element’s parent.
public abstract craft\base\ElementInterface, null getParent ( )
getPrev()
public abstract method
#
Returns the previous element relative to this one, from a given set of criteria.
public abstract craft\base\ElementInterface, null getPrev ( $criteria = false )
$criteria |
mixed |
getPrevSibling()
public abstract method
#
Returns the element’s previous sibling.
public abstract craft\base\ElementInterface, null getPrevSibling ( )
getRef()
public abstract method
#
Returns the reference string to this element.
public abstract string, null getRef ( )
getRoute()
public abstract method
#
Returns the route that should be used when the element’s URI is requested.
public abstract mixed getRoute ( )
return | mixed | The route that the request should use, or null if no special action should be taken |
---|
getSearchKeywords()
public abstract method
#
Returns the search keywords for a given search attribute.
public abstract string getSearchKeywords ( \craft\base\string $attribute )
$attribute |
string |
getSerializedFieldValues()
public abstract method
#
Returns an array of the element’s serialized custom field values, indexed by their handles.
public abstract array getSerializedFieldValues ( array $fieldHandles = null )
$fieldHandles |
string[], null | The list of field handles whose values need to be returned. Defaults to null, meaning all fields’ values will be returned. If it is an array, only the fields in the array will be returned. |
getSiblings()
public abstract method
#
Returns all of the element’s siblings.
public abstract craft\elements\db\ElementQueryInterface, craft\base\ElementInterface[] getSiblings ( )
getStatus()
public abstract method
#
Returns the element’s status.
public abstract string, null getStatus ( )
getSupportedSites()
public abstract method
#
Returns the sites this element is associated with.
The function can either return an array of site IDs, or an array of sub-arrays, each with the keys 'siteId' (int) and 'enabledByDefault' (bool).
public abstract integer[], array getSupportedSites ( )
getTableAttributeHtml()
public abstract method
#
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 ? '<a href="mailto:'.$this->email.'">'.$this->email.'</a>' : '';
// ...
}
return parent::getTableAttributeHtml($attribute);
craft\base\Element::getTableAttributeHtml() provides a couple handy attribute checks by default, so it is a good idea to let the parent method get called (as shown above). They are:
- 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 class.
- 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.
public abstract string getTableAttributeHtml ( \craft\base\string $attribute )
$attribute |
string | The attribute name. |
return | string | The HTML that should be shown for a given attribute in Table View. |
---|
getThumbUrl()
public abstract method
#
Returns the URL to the element’s thumbnail, if there is one.
public abstract string, null getThumbUrl ( \craft\base\int $size )
$size |
integer |
getTotalDescendants()
public abstract method
#
Returns the total number of descendants that the element has.
public abstract integer getTotalDescendants ( )
getUriFormat()
public abstract method
#
Returns the URI format used to generate this element’s URI.
Note that element types that can have URIs must return true
from hasUris().
See also:
public abstract string, null getUriFormat ( )
getUrl()
public abstract method
#
Returns the element’s full URL.
public abstract string, null getUrl ( )
hasContent()
public abstract static method
#
Returns whether elements of this type will be storing any data in the content
table (tiles or custom fields).
public abstract static boolean hasContent ( )
return | boolean | Whether elements of this type will be storing any data in the content table.
|
---|
hasStatuses()
public abstract static method
#
Returns whether elements of this type have statuses.
If this returns true
, the element index template will show a Status menu by default, and your elements will
get status indicator icons next to them.
Use statuses() to customize which statuses the elements might have.
See also statuses().
public abstract static boolean hasStatuses ( )
return | boolean | Whether elements of this type have statuses. |
---|
hasTitles()
public abstract static method
#
Returns whether elements of this type have traditional titles.
public abstract static boolean hasTitles ( )
return | boolean | Whether elements of this type have traditional titles. |
---|
hasUris()
public abstract static method
#
Returns whether elements of this type can have their own slugs and URIs.
Note that individual elements must also return a URI format from getUriFormat() if they are to actually get a URI.
See also getUriFormat().
public abstract static boolean hasUris ( )
return | boolean | Whether elements of this type can have their own slugs and URIs. |
---|
indexHtml()
public abstract static method
#
Returns the element index HTML.
public abstract static string indexHtml ( craft\elements\db\ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, \craft\base\string $sourceKey = null, \craft\base\string $context = null, \craft\base\bool $includeContainer, \craft\base\bool $showCheckboxes )
$elementQuery |
craft\elements\db\ElementQueryInterface | |
$disabledElementIds |
integer[], null | |
$viewState |
array | |
$sourceKey |
string, null | |
$context |
string, null | |
$includeContainer |
boolean | |
$showCheckboxes |
boolean | |
return | string | The element index HTML |
---|
isAncestorOf()
public abstract method
#
Returns whether this element is an ancestor of another one.
public abstract boolean isAncestorOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isChildOf()
public abstract method
#
Returns whether this element is a direct child of another one.
public abstract boolean isChildOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isDescendantOf()
public abstract method
#
Returns whether this element is a descendant of another one.
public abstract boolean isDescendantOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isLocalized()
public abstract static method
#
Returns whether elements of this type store content on a per-site basis.
If this returns true
, the element’s getSupportedSites() method will
be responsible for defining which sites its content should be stored in.
public abstract static boolean isLocalized ( )
return | boolean | Whether elements of this type store data on a per-site basis. |
---|
isNextSiblingOf()
public abstract method
#
Returns whether this element is the direct next sibling of another one.
public abstract boolean isNextSiblingOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isParentOf()
public abstract method
#
Returns whether this element is a direct parent of another one.
public abstract boolean isParentOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isPrevSiblingOf()
public abstract method
#
Returns whether this element is the direct previous sibling of another one.
public abstract boolean isPrevSiblingOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
isSiblingOf()
public abstract method
#
Returns whether this element is a sibling of another one.
public abstract boolean isSiblingOf ( craft\base\ElementInterface $element )
$element |
craft\base\ElementInterface |
offsetExists()
public abstract method
#
Treats custom fields as array offsets.
public abstract boolean offsetExists ( $offset )
$offset |
string, integer |
refHandle()
public abstract static method
#
Returns the handle that should be used to refer to this element type from reference tags.
public abstract static string, null refHandle ( )
return | string, null | The reference handle, or null if the elemnet type doesn’t support reference tags |
---|
searchableAttributes()
public abstract static method
#
Defines which element attributes should be searchable.
This method should return an array of attribute names that can be accessed on your elements. craft\services\Search will call this method when it is indexing keywords for one of your elements, and for each attribute it returns, it will fetch the corresponding property’s value on the element. For example, if your elements have a “color” attribute which you want to be indexed, this method could return:
return ['color'];
Not only will the “color” attribute’s values start getting indexed, but users will also be able to search directly against that attribute’s values using this search syntax:
color:blue
There is no need for this method to worry about the ‘title’ or ‘slug’ attributes, or custom field handles; those are indexed automatically.
public abstract static string[] searchableAttributes ( )
return | string[] | The element attributes that should be searchable |
---|
setFieldParamNamespace()
public abstract method
#
Sets the namespace used by custom field params on the request.
public abstract void setFieldParamNamespace ( \craft\base\string $namespace )
$namespace |
string | The field param namespace |
setFieldValue()
public abstract method
#
Sets the value for a given field.
public abstract void setFieldValue ( \craft\base\string $fieldHandle, $value )
$fieldHandle |
string | The field handle whose value needs to be set |
$value |
mixed | The value to set on the field |
setFieldValues()
public abstract method
#
Sets the element’s custom field values.
public abstract void setFieldValues ( array $values )
$values |
array | The custom field values (handle => value) |
setFieldValuesFromRequest()
public abstract method
#
Sets the element’s custom field values, when the values have come from post data.
public abstract void setFieldValuesFromRequest ( \craft\base\string $paramNamespace )
$paramNamespace |
string | The field param namespace |
setNext()
public abstract method
#
Sets the default next element.
public abstract void setNext ( $element )
$element |
craft\base\ElementInterface, false |
setParent()
public abstract method
#
Sets the element’s parent.
public abstract void setParent ( craft\base\ElementInterface $parent = null )
$parent |
craft\base\ElementInterface, null |
setPrev()
public abstract method
#
Sets the default previous element.
public abstract void setPrev ( $element )
$element |
craft\base\ElementInterface, false | Return void |
sortOptions()
public abstract static method
#
Returns the sort options for the element type.
This method should return an array, where the keys reference database column names that should be sorted on, and where the values define the user-facing labels.
return [
'columnName1' => Craft::t('app', 'Attribute Label 1'),
'columnName2' => Craft::t('app', 'Attribute Label 2'),
];
If you want to sort by multilple columns simultaneously, you can specify multiple column names in the key, separated by commas.
return [
'columnName1, columnName2 asc' => Craft::t('app', 'Attribute Label 1'),
'columnName3' => Craft::t('app', 'Attribute Label 2'),
];
If you do that, you can specify the sort direction for the subsequent columns (asc
or desc
. There is no point
in specifying the sort direction for the first column, though, since the end user has full control over that.
Note that this method will only get called once for the entire index; not each time that a new source is
selected.
public abstract static array sortOptions ( )
return | array | The attributes that elements can be sorted by |
---|
sources()
public abstract static method
#
Returns the source definitions that elements of this type may belong to.
This defines what will show up in the source list on element indexes and element selector modals. Each item in the array should be set to an array that has the following keys:
key
– The source’s key. This is the string that will be passed into the $source argument of actions(), indexHtml(), and defaultTableAttributes().label
– The human-facing label of the source.sites
– An array of site IDs that the source should be shown for, on multi-site element indexes. (Optional; by default the source will be shown for all sites.)criteria
– An array of element criteria parameters that the source should use when the source is selected. (Optional)data
– An array ofdata-X
attributes that should be set on the source’s<a>
tag in the source list’s, HTML, where each key is the name of the attribute (without the “data-” prefix), and each value is the value of the attribute. (Optional)defaultSort
– A string identifying the sort attribute that should be selected by default, or an array where the first value identifies the sort attribute, and the second determines which direction to sort by. (Optional)hasThumbs
– A bool that defines whether this source supports Thumbs View. (Use your element’s getThumbUrl() method to define your elements’ thumb URL.) (Optional)structureId
– The ID of the Structure that contains the elements in this source. If set, Structure View will be available to this source. (Optional)newChildUrl
– The URL that should be loaded when a usel select’s the “New child” menu option on an element in this source while it is in Structure View. (Optional)nested
– An array of sources that are nested within this one. Each nested source can have the same keys as top-level sources.
public abstract static array sources ( \craft\base\string $context = null )
$context |
string, null | The context ('index' or 'modal'). |
return | array | The sources. |
---|
statuses()
public abstract static method
#
Returns all of the possible statuses that elements of this type may have.
This method will be called when populating the Status menu on element indexes, for element types whose
hasStatuses() method returns true
. It will also be called when craft\elements\db\ElementQuery is querying for
elements, to ensure that its “status” parameter is set to a valid status.
It should return an array whose keys are the status values, and values are the human-facing status labels, or an array
with the following keys:
label
– The human-facing status label.color
– The status color (green, orange, red, yellow, pink, purple, blue, turquoise, light, grey, black, or white) You can customize the database query condition that should be applied for your custom statuses from craft\elements\db\ElementQuery::statusCondition().
See also hasStatuses().
public abstract static array statuses ( )
tableAttributes()
public abstract static method
#
Defines all of the available columns that can be shown in table views.
This method should return an array whose keys map to attribute names and database columns that can be sorted against when querying for elements, and whose values make up the table’s column headers. The first item that this array returns will just identify the database column name, and the table column’s header, but will not have any effect on what shows up in the table’s body. That’s because the first column is reserved for displaying whatever your element’s __toString() method returns. All other items besides the first one will also define which element attribute should be shown within the data cells. (The actual HTML to be shown can be customized with getTableAttributeHtml().)
public abstract static array tableAttributes ( )
return | array | The table attributes. |
---|