Skip to content

LinkData ​

Type
Class
Namespace
craft\fields\data
Inherits
craft\fields\data\LinkData » yii\base\BaseObject
Implements
craft\base\Serializable, yii\base\Arrayable, yii\base\Configurable
Uses traits
yii\base\ArrayableTrait
Since
5.3.0

Link field data class.

View source

Public Properties ​

PropertyDescription
ariaLabelstring, null – The link’s aria-label attribute.
attributesarray, null – The attributes that should be added to <a> tags for this link
classstring, null – The link’s class attribute.
downloadboolean – Whether the link should have a download attribute.
elementcraft\base\ElementInterface, null – The element linked by the field, if there is one
elementQuerycraft\elements\db\ElementQueryInterface, null – An element query that will fetch the element linked by the field, if there is one
filenamestring, null – The download filename
idstring, null – The link’s id attribute.
labelstring – The link label
link\Twig\Markup – An anchor tag for this link
relstring, null – The link’s rel attribute.
targetstring, null – The link’s target attribute.
titlestring, null – The link’s title attribute.
typestring – The link type ID
urlstring – The full link URL, including the suffix
urlSuffixstring, null – The link’s URL suffix value.
valuestring – The link value

ariaLabel ​

Type
string, null
Default value
null
Since
5.6.0

The link’s aria-label attribute.

View source

attributes ​

Type
array, null
Default value
null

The attributes that should be added to <a> tags for this link

View source

class ​

Type
string, null
Default value
null
Since
5.6.0

The link’s class attribute.

View source

download ​

Type
boolean
Default value
false
Since
5.7.0

Whether the link should have a download attribute.

View source

element ​

Type
craft\base\ElementInterface, null
Default value
null

The element linked by the field, if there is one

View source

elementQuery ​

Type
craft\elements\db\ElementQueryInterface, null
Default value
null

An element query that will fetch the element linked by the field, if there is one

View source

filename ​

Type
string, null
Default value
null

The download filename

View source

id ​

Type
string, null
Default value
null
Since
5.6.0

The link’s id attribute.

View source

label ​

Type
string
Default value
null

The link label

View source

Type
\Twig\Markup
Default value
null

An anchor tag for this link

View source

rel ​

Type
string, null
Default value
null
Since
5.6.0

The link’s rel attribute.

View source

target ​

Type
string, null
Default value
null
Since
5.5.0

The link’s target attribute.

View source

title ​

Type
string, null
Default value
null
Since
5.6.0

The link’s title attribute.

View source

type ​

Type
string
Default value
null

The link type ID

View source

url ​

Type
string
Default value
null

The full link URL, including the suffix

View source

urlSuffix ​

Type
string, null
Default value
null
Since
5.6.0

The link’s URL suffix value.

View source

value ​

Type
string
Default value
null

The link value

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__construct()
__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.
__toString()
__unset()Sets an object property to null.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
extraFields()Returns the list of fields that can be expanded further and returned by toArray().
fields()Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.
getAttributes()Returns the attributes that should be added to <a> tags for this link.
getElement()Returns the element linked by the field, if there is one.
getElementQuery()Returns an element query that will fetch the element linked by the field, if there is one.
getFilename()Returns the download filename.
getLabel()Returns the link label.
getLink()Returns an anchor tag for this link.
getType()Returns the link type ID.
getUrl()Returns the full link URL.
getValue()Returns the link value.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
serialize()
setFilename()Sets the download filename.
setLabel()Sets the link label.
toArray()Converts the model into an array.

__construct() ​

View source

Arguments ​

  • $value
  • $linkType
  • $config

__toString() ​

View source

fields() ​

Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.

A field is a named element in the returned array by toArray().

This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be:

function ($model, $field) {
    // return field value
}

For example, the following code declares four fields:

  • email: the field name is the same as the property name email;
  • firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties;
  • fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name.
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function ($model) {
        return $model->first_name . ' ' . $model->last_name;
    },
];

View source

Returns ​

array – The list of field names or field definitions.

getAttributes() ​

Since
5.9.0

Returns the attributes that should be added to <a> tags for this link.

View source

Returns ​

array, null

getElement() ​

Returns the element linked by the field, if there is one.

View source

Returns ​

craft\base\ElementInterface, null

getElementQuery() ​

Since
5.6.0

Returns an element query that will fetch the element linked by the field, if there is one.

View source

Returns ​

craft\elements\db\ElementQueryInterface, null

getFilename() ​

Since
5.7.0

Returns the download filename.

View source

Arguments ​

  • $custom (boolean) – Whether to return the custom filename

Returns ​

string, null

getLabel() ​

Returns the link label.

View source

Arguments ​

  • $custom (boolean, null) – Whether to return the custom label

Returns ​

string, null

Returns an anchor tag for this link.

View source

Returns ​

\Twig\Markup

getType() ​

Returns the link type ID.

View source

Returns ​

string

getUrl() ​

Since
5.6.0

Returns the full link URL.

View source

Arguments ​

  • $anyStatus (boolean) – Whether to return a value regardless of the linked element’s status

getValue() ​

Returns the link value.

View source

serialize() ​

View source

setFilename() ​

Since
5.7.0

Sets the download filename.

View source

Arguments ​

setLabel() ​

Since
5.5.0

Sets the link label.

View source

Arguments ​

Protected Methods ​

MethodDescription
extractFieldsFor()Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id".
extractRootFields()Extracts the root field names from nested fields.
resolveFields()Determines which fields can be returned by toArray().