AssetElementType

Type
Class
Namespace
Craft
Inherits
Craft\AssetElementType » Craft\BaseElementType » Craft\BaseComponentType » Craft\BaseApplicationComponent » CApplicationComponent » CComponent
Implements
Craft\IComponentType, Craft\IElementType, IApplicationComponent
Since
1.0

The AssetElementType class is responsible for implementing and defining assets as a native element type in Craft.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$behaviorsarray – The behaviors that should be attached to this component.

Protected Properties

PropertyDescription
$componentTypestring

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
defineAvailableTableAttributes()Defines all of the available columns that can be shown in table views.
defineCriteriaAttributes()Defines any custom element criteria attributes for this element type.
defineSearchableAttributes()Defines which element model attributes should be searchable.
defineSortableAttributes()Defines the attributes that elements can be sorted by.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
eagerLoadTransforms()Eager-loads image transforms requested by an element criteria model.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getAvailableActions()Returns the available element actions for a given source (if one is provided).
getClassHandle()Returns the component’s handle, ideally based on the class name.
getContentFieldColumnsForElementsQuery()Returns the field column names that should be selected from the content table.
getContentTableForElementsQuery()Returns the content table name that should be joined into an elements query for a given element criteria.
getDefaultTableAttributes()Returns the list of table attribute keys that should be shown by default.
getEagerLoadingMap()Returns an array that maps source-to-target element IDs based on the given sub-property handle.
getEditorHtml()Returns the HTML for an editor HUD for the given element.
getElementQueryStatusCondition()Returns the element query condition for a custom status criteria.
getEventHandlers()Returns the list of attached event handlers for an event.
getFieldsForElementsQuery()Returns the fields that should take part in an upcoming elements qurery.
getIndexHtml()Returns the element index HTML.
getIsInitialized()Checks if this application component has been initialized.
getName()Returns the component’s name.
getSource()Returns a source by its key and context.
getSources()Returns this element type's sources.
getStatuses()Returns all of the possible statuses that elements of this type may have.
getTableAttributeHtml()Returns the HTML that should be shown for a given element’s attribute in Table View.
hasContent()Returns whether this element type will be storing any data in the content table (tiles or custom fields).
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
hasStatuses()Returns whether this element type can have statuses.
hasTitles()Returns whether this element type has titles.
init()Initializes the application component.
isInitialized()Checks if this application component has been initialized yet, or not.
isLocalized()Returns whether this element type stores data on a per-locale basis.
isSelectable()Returns whether this component should be shown when the user is creating a component of this type.
modifyElementsQuery()Modifies an element query targeting elements of this type.
onAfterMoveElementInStructure()Performs actions after an element has been moved within a structure.
populateElementModel()Populates an element model based on a query result.
raiseEvent()Raises an event.
routeRequestForMatchedElement()Routes the request when the URI matches an element.
saveElement()Saves a given element.

defineAvailableTableAttributes()

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 model’s {@link BaseElementModel::__toString() __toString()} method returns (the string representation of the element).

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 {@link getTableAttributeHtml()}.)

IElementType::defineAvailableTableAttributes()

IElementType::defineAvailableTableAttributes()

View source

Returns

array – The table attributes.

Signature

public array defineAvailableTableAttributes ( )

defineCriteriaAttributes()

Defines any custom element criteria attributes for this element type.

This method returns an array which will get merged into the array defined in {@link ElementCriteriaModel::defineAttributes()}, when new ElementCriteriaModel instances are created targeting this element type (generally from {@link ElementsService::getCriteria() craft()->elements->getCriteria()}).

If this method were to return the following:

return array(
    'foo' => AttributeType::String,
    'bar' => AttributeType::String,
);

then when someone creates a new ElementCriteriaModel instance targeting this elmeent type, they will be able to do this:

$criteria = craft()->elements->getCriteria('ThisElementType');
$criteria->foo = 'FooParamValue';
$criteria->bar = 'BarParamValue';

You can check for these custom criteria attributes, and factor their values into the actual database query, from {@link modifyElementsQuery()}.

IElementType::defineCriteriaAttributes()

IElementType::defineCriteriaAttributes()

View source

Returns

array – Custom criteria attributes.

Signature

public array defineCriteriaAttributes ( )

defineSearchableAttributes()

Defines which element model attributes should be searchable.

This method should return an array of attribute names that can be accessed on your {@link BaseElementModel element model} (for example, the attributes defined by your element model’s {@link BaseElementType::defineAttributes() defineAttributes()} method). {@link SearchService} 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 array('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.

IElementType::defineSearchableAttributes()

IElementType::defineSearchableAttributes()

View source

Returns

array – The attributes that should be searchable

Signature

public array defineSearchableAttributes ( )

defineSortableAttributes()

Defines the attributes that elements can be sorted by.

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 array(
    'columnName1' => Craft::t('Attribute Label 1'),
    'columnName2' => Craft::t('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 array(
    'columnName1, columnName2 asc' => Craft::t('Attribute Label 1'),
    'columnName3'                  => Craft::t('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.

IElementType::defineSortableAttributes()

IElementType::defineSortableAttributes()

View source

Returns

array – The attributes that elements can be sorted by.

Signature

public array defineSortableAttributes ( )

eagerLoadTransforms()

Eager-loads image transforms requested by an element criteria model.

View source

Arguments

Returns

void

Signature

public void eagerLoadTransforms ( Craft\Event $event )

getAvailableActions()

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 {@link IElementAction} instance.

IElementType::getAvailableActions()

IElementType::getAvailableActions()

View source

Arguments

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

Returns

array, null – The available element actions.

Signature

public array, null getAvailableActions ( $source = null )

getDefaultTableAttributes()

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 {@link defineAvailableTableAttributes()}.

IElementType::getDefaultTableAttributes()

IElementType::getDefaultTableAttributes()

View source

Arguments

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

Returns

array – The table attribute keys.

Signature

public array getDefaultTableAttributes ( $source = null )

getEditorHtml()

Returns the HTML for an editor HUD for the given element.

IElementType::getEditorHtml()

IElementType::getEditorHtml()

View source

Arguments

Returns

string – The HTML for the editor HUD.

Signature

public string getEditorHtml ( Craft\BaseElementModel $element )

getName()

Returns the component’s name.

This is what your component will be called throughout the Control Panel.

IComponentType::getName()

View source

Returns

string – The component’s name.

Signature

public string getName ( )

getSource()

Returns a source by its key and context.

IElementType::getSource()

IElementType::getSource()

View source

Arguments

  • $key (string) – The source’s key.
  • $context (string, null) – The context ('index' or 'modal').

Returns

array, null

Signature

public array, null getSource ( $key, $context = null )

getSources()

Returns this element type's sources.

This defines what will show up in the source list on element indexes and element selector modals.

Each item in the array should have a key that identifies the source’s key (e.g. "section:3"), and should be set to an array that has the following keys:

  • label – The human-facing label of the source.
  • criteria – An array of criteria parameters that the source should use when the source is selected. (Optional)
  • data – An array of data-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 boolean that defines whether this source supports Thumbs View. (Use your element model’s {@link BaseElementModel::getThumbUrl() 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.

IElementType::getSources()

IElementType::getSources()

View source

Arguments

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

Returns

array, false – The element type's sources.

Signature

public array, false getSources ( $context = null )

getTableAttributeHtml()

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

This method can be used to completely customize what actually shows up within the table’s body for a given attribtue, 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':
    {
        if ($element->email)
        {
            return '<a href="mailto:'.$element->email.'">'.$element->email.'</a>';
        }

        break;
    }
    default:
    {
        return parent::getTableAttributeHtml($element, $attribute);
    }
}

BaseElementType::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 ‘uri’, it will be linked to the front-end URL.
  • If the attribute name is ‘dateCreated’ or ‘dateUpdated’, the date will be formatted according to the active locale.

IElementType::getTableAttributeHtml()

IElementType::getTableAttributeHtml()

View source

Arguments

Returns

string

Signature

public string getTableAttributeHtml ( Craft\BaseElementModel $element, $attribute )

hasContent()

Returns whether this element type will be storing any data in the content table (tiles or custom fields).

IElementType::hasContent()

IElementType::hasContent()

View source

Returns

boolean – Whether the element type has content. Default is false.

Signature

public boolean hasContent ( )

hasTitles()

Returns whether this element type has titles.

IElementType::hasTitles()

IElementType::hasTitles()

View source

Returns

boolean – Whether the element type has titles. Default is false.

Signature

public boolean hasTitles ( )

isLocalized()

Returns whether this element type stores data on a per-locale basis.

If this returns true, the element model’s {@link BaseElementModel::getLocales() getLocales()} method will be responsible for defining which locales its data should be stored in.

IElementType::isLocalized()

IElementType::isLocalized()

View source

Returns

boolean – Whether the element type is localized. Default is false.

Signature

public boolean isLocalized ( )

modifyElementsQuery()

Modifies an element query targeting elements of this type.

If your element type is storing additional data in its own table, this method is the place to join that table in.

$query
    ->addSelect('mytable.foo, mytable.bar')
    ->join('mytable mytable', 'mytable.id = elements.id');

This is also where you get to check the {@link ElementCriteriaModel} for all the custom attributes that this element type supports via {@defineCriteriaAttributes()}, and modify the database query to reflect those parameters.

if ($criteria->foo)
{
    $query->andWhere(DbHelper::parseParam('mytable.foo', $criteria->foo, $query->params));
}

if ($criteria->bar)
{
    $query->andWhere(DbHelper::parseParam('mytable.bar', $criteria->bar, $query->params));
}

If you are able to determine from the element criteria’s paramteers that there’s no way that the query is going to match any elements, you can have it return false. The query will be stopped before it ever gets a chance to execute.

IElementType::modifyElementsQuery()

IElementType::modifyElementsQuery()

View source

Arguments

Returns

mixedfalse in the event that the method is sure that no elements are going to be found.

Signature

public mixed modifyElementsQuery ( Craft\DbCommand $query, Craft\ElementCriteriaModel $criteria )

populateElementModel()

Populates an element model based on a query result.

This method is called by {@link ElementsService::findElements()} after it has finished fetching all of the matching elements’ rows from the database.

For each row of data returned by the query, it will call this method on the element type, and it is up to this method to take that array of raw data from the database, and populate a new element model with it.

You should be able to accomplish this with a single line:

return MyElementTypeModel::populateModel($row);

IElementType::populateElementModel()

IElementType::populateElementModel()

View source

Arguments

  • $row (array) – The row of data in the database query result.

Returns

array – The element model, populated with the data in $row.

Signature

public array populateElementModel ( $row )

saveElement()

Saves a given element.

This method will be called when an Element Editor’s Save button is clicked. It should just wrap your service’s saveX() method.

IElementType::saveElement()

IElementType::saveElement()

View source

Arguments

Returns

boolean – Whether the element was saved successfully.

Signature

public boolean saveElement ( Craft\BaseElementModel $element, $params )

Protected Methods

MethodDescription
getTableAttributesForSource()Returns the attributes that should be shown for the given source.
prepElementCriteriaForTableAttribute()Preps the element criteria for a given table attribute

getTableAttributesForSource()

Returns the attributes that should be shown for the given source.

BaseElementType::getTableAttributesForSource()

View source

Arguments

  • $sourceKey (string) – The source key

Returns

array – The attributes that should be shown for the given source

Signature

protected array getTableAttributesForSource ( $sourceKey )