ElementHelper
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\ElementHelper
- Since
- 3.0.0
Class ElementHelper
View source (opens new window)
# Public Methods
Method | Description |
---|---|
attributeHtml() | Returns the HTML for a given attribute value, to be shown in an element index view. |
doesUriFormatHaveSlugTag() | Returns whether a given URL format has a proper {slug} tag. |
editableSiteIdsForElement() | Returns the editable site IDs for a given element, taking user permissions into account. |
fieldColumn() | Returns the content column name based on the given field attributes. |
fieldColumnFromField() | Returns the content column name for a given field. |
findSource() | Returns an element type's source definition based on a given source key/path and context. |
generateSlug() | Generates a new slug based on a given string. |
isAttributeEmpty() | Returns whether the attribute on the given element is empty. |
isCanonical() | Returns whether the given element (or its root element if a block element) is a canonical element. |
isDerivative() | Returns whether the given element (or its root element if a block element) is a derivative of another element. |
isDraft() | Returns whether the given element (or its root element if a block element) is a draft. |
isDraftOrRevision() | Returns whether the given element (or its root element if a block element) is a draft or revision. |
isElementEditable() | Returns whether the given element is editable by the current user, taking user permissions into account. |
isOutdated() | Returns whether the given derivative element is outdated compared to its canonical element. |
isRevision() | Returns whether the given element (or its root element if a block element) is a revision. |
isTempSlug() | Returns whether the given slug is temporary. |
normalizeSlug() | Normalizes a slug. |
rootElement() | Returns the root element of a given element. |
rootSourceKey() | Returns the root level source key for a given source key/path |
setNextPrevOnElements() | Given an array of elements, will go through and set the appropriate "next" and "prev" elements on them. |
setUniqueUri() | Sets the URI on an element using a given URL format, tweaking its slug if necessary to ensure it's unique. |
shouldTrackChanges() | Returns whether changes should be tracked for the given element. |
siteStatusesForElement() | Returns the site statuses for a given element. |
sourceElement() | Returns the canonical version of an element. |
supportedSitesForElement() | Returns a list of sites that a given element supports. |
tempSlug() | Generates a new temporary slug. |
translationDescription() | Returns the description of a field’s translation support. |
translationKey() | Returns the translation key for an element title or custom field, based on the given translation method and translation key format. |
# attributeHtml()
- Since
- 4.3.0
Returns the HTML for a given attribute value, to be shown in an element index view.
View source (opens new window)
Arguments
$value
(mixed
) – The field value
Returns
# doesUriFormatHaveSlugTag()
Returns whether a given URL format has a proper {slug} tag.
View source (opens new window)
Arguments
$uriFormat
(string (opens new window))
Returns
# editableSiteIdsForElement()
Returns the editable site IDs for a given element, taking user permissions into account.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# fieldColumn()
- Since
- 3.7.0
Returns the content column name based on the given field attributes.
View source (opens new window)
Arguments
$columnPrefix
(string (opens new window), null (opens new window))$handle
(string (opens new window))$columnSuffix
(string (opens new window), null (opens new window))$columnKey
(string (opens new window), null (opens new window))
Returns
# fieldColumnFromField()
- Since
- 3.7.0
Returns the content column name for a given field.
View source (opens new window)
Arguments
$field
(craft\base\FieldInterface)$columnKey
(string (opens new window), null (opens new window))
Returns
string (opens new window), null (opens new window)
# findSource()
Returns an element type's source definition based on a given source key/path and context.
View source (opens new window)
Arguments
$elementType
(string (opens new window)) – The element type class$sourceKey
(string (opens new window)) – The source key/path$context
(string (opens new window)) – The context
Returns
array (opens new window), null (opens new window) – The source definition, or null if it cannot be found
# generateSlug()
- Since
- 3.5.0
Generates a new slug based on a given string.
This is different from normalizeSlug() in two ways:
- Periods and underscores will be converted to dashes, whereas normalizeSlug() will leave those in-tact.
- The string may be converted to ASCII.
View source (opens new window)
Arguments
$str
(string (opens new window)) – The string$ascii
(boolean (opens new window), null (opens new window)) – Whether the slug should be converted to ASCII. If null, it will depend on the config4:limitAutoSlugsToAscii (opens new window) config setting value.$language
(string (opens new window), null (opens new window)) – The language to pull ASCII character mappings for, if needed
Returns
# isAttributeEmpty()
- Since
- 4.2.6
Returns whether the attribute on the given element is empty.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)$attribute
(string (opens new window))
Returns
# isCanonical()
- Since
- 3.7.17
Returns whether the given element (or its root element if a block element) is a canonical element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isDerivative()
- Since
- 3.7.17
Returns whether the given element (or its root element if a block element) is a derivative of another element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isDraft()
- Since
- 3.7.0
Returns whether the given element (or its root element if a block element) is a draft.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isDraftOrRevision()
- Since
- 3.2.0
Returns whether the given element (or its root element if a block element) is a draft or revision.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isElementEditable()
Returns whether the given element is editable by the current user, taking user permissions into account.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isOutdated()
- Since
- 3.7.12
Returns whether the given derivative element is outdated compared to its canonical element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isRevision()
- Since
- 3.7.0
Returns whether the given element (or its root element if a block element) is a revision.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# isTempSlug()
- Since
- 3.2.2
Returns whether the given slug is temporary.
View source (opens new window)
Arguments
$slug
(string (opens new window))
Returns
# normalizeSlug()
- Since
- 3.5.0
Normalizes a slug.
View source (opens new window)
Arguments
$slug
(string (opens new window))
Returns
# rootElement()
- Since
- 3.2.0
Returns the root element of a given element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# rootSourceKey()
- Since
- 3.7.25.1
Returns the root level source key for a given source key/path
View source (opens new window)
Arguments
$sourceKey
(string (opens new window))
Returns
# setNextPrevOnElements()
Given an array of elements, will go through and set the appropriate "next" and "prev" elements on them.
View source (opens new window)
Arguments
$elements
(iterable
, craft\base\ElementInterface[]) – The array of elements.
# setUniqueUri()
Sets the URI on an element using a given URL format, tweaking its slug if necessary to ensure it's unique.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Throws
- craft\errors\OperationAbortedException
if a unique URI could not be found
# shouldTrackChanges()
- Since
- 3.7.4
Returns whether changes should be tracked for the given element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)
Returns
# siteStatusesForElement()
- Since
- 4.4.7
Returns the site statuses for a given element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface) – The element to return site statuses for$editableOnly
(boolean (opens new window)) – Whether to only return statuses for sites the user has access to
Returns
array<int,bool>
– The site statuses, indexed by site ID
# sourceElement()
DEPRECATED
Deprecated in 3.7.0. Use craft\base\ElementInterface::getCanonical() instead.
- Since
- 3.3.0
Returns the canonical version of an element.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface) – The source/draft/revision element$anySite
(boolean (opens new window)) – Whether the source element can be retrieved in any site
Returns
# supportedSitesForElement()
Returns a list of sites that a given element supports.
Each site is represented as an array with siteId
, propagate
, and enabledByDefault
keys.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface) – The element to return supported site info for$withUnpropagatedSites
(boolean (opens new window)) – Whether to include sites the element is currently not being propagated to
Returns
Throws
- yii\base\Exception (opens new window)
if any of the element’s supported sites are invalid
# tempSlug()
- Since
- 3.2.2
Generates a new temporary slug.
View source (opens new window)
Returns
# translationDescription()
- Since
- 3.5.0
Returns the description of a field’s translation support.
View source (opens new window)
Arguments
$translationMethod
(string (opens new window))
Returns
string (opens new window), null (opens new window)
# translationKey()
- Since
- 3.5.0
Returns the translation key for an element title or custom field, based on the given translation method and translation key format.
View source (opens new window)
Arguments
$element
(craft\base\ElementInterface)$translationMethod
(string (opens new window))$translationKeyFormat
(string (opens new window), null (opens new window))
Returns
# Constants
Constant | Description |
---|---|
URI_MAX_LENGTH |
← Diff FieldHelper →