ElementHelper ​
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\ElementHelper
- Since
- 3.0.0
Class ElementHelper
Public Methods ​
Method | Description |
---|---|
actionConfig() | Returns an element action’s JavaScript configuration. |
addElementEditorUrlParams() | Ensures the given element edit URL includes the necessary query string params. |
attributeHtml() | Returns the HTML for a given attribute value, to be shown in table and card views. |
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. |
elementEditorUrl() | Returns a generic editor URL for the given element. |
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. |
linkAttributeHtml() | Returns the HTML for a link attribute based on provided URL. |
normalizeSlug() | Normalizes a slug. |
postEditUrl() | Returns the URL that users should be redirected to after editing the given element. |
renderElements() | Renders the given elements using their partial templates. |
rootElement() | Returns the root owner of a given element. |
rootElementIfCanonical() | Returns the root element of a given element, unless the element or any of its owners are not canonical. |
rootSourceKey() | Returns the root level source key for a given source key/path |
searchableAttributes() | Returns the searchable attributes for a given element, ensuring that slug and title are included. |
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. |
swapInProvisionalDrafts() | Swaps out any canonical elements with provisional drafts, when they exist. |
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. |
uriAttributeHtml() | Returns the HTML for URI attribute based on a value (text) and a URL it's supposed to link to. |
actionConfig()
​
- Since
- 5.0.0
Returns an element action’s JavaScript configuration.
Arguments ​
$action
(craft\base\ElementActionInterface)
Returns ​
addElementEditorUrlParams()
​
- Since
- 5.0.0
Ensures the given element edit URL includes the necessary query string params.
Arguments ​
$url
(string)$element
(craft\base\ElementInterface)
Returns ​
attributeHtml()
​
- Since
- 5.0.0
Returns the HTML for a given attribute value, to be shown in table and card views.
Arguments ​
$value
(mixed
) – The field value
Returns ​
doesUriFormatHaveSlugTag()
​
Returns whether a given URL format has a proper {slug} tag.
Arguments ​
$uriFormat
(string)
Returns ​
editableSiteIdsForElement()
​
Returns the editable site IDs for a given element, taking user permissions into account.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
elementEditorUrl()
​
- Since
- 5.0.0
Returns a generic editor URL for the given element.
Arguments ​
$element
(craft\base\ElementInterface)$withParams
(boolean) – Whether to include the necessary query string params
Returns ​
findSource()
​
Returns an element type's source definition based on a given source key/path and context.
Arguments ​
$elementType
(class-string<\craft\base\ElementInterface>
) – The element type class$sourceKey
(string) – The source key/path$context
(string) – The context
Returns ​
array, null – 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.
Arguments ​
$str
(string) – The string$ascii
(boolean, null) – Whether the slug should be converted to ASCII. If null, it will depend on the config5:limitAutoSlugsToAscii config setting value.$language
(string, null) – 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.
Arguments ​
$element
(craft\base\ElementInterface)$attribute
(string)
Returns ​
isCanonical()
​
- Since
- 3.7.17
Returns whether the given element (or its root element if a block element) is a canonical element.
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.
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.
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.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
isElementEditable()
​
Returns whether the given element is editable by the current user, taking user permissions into account.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
isOutdated()
​
- Since
- 3.7.12
Returns whether the given derivative element is outdated compared to its canonical element.
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.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
isTempSlug()
​
- Since
- 3.2.2
Returns whether the given slug is temporary.
Arguments ​
$slug
(string)
Returns ​
linkAttributeHtml()
​
- Since
- 5.5.0
Returns the HTML for a link attribute based on provided URL.
Arguments ​
Returns ​
normalizeSlug()
​
- Since
- 3.5.0
Normalizes a slug.
Arguments ​
$slug
(string)
Returns ​
postEditUrl()
​
- Since
- 5.2.0
Returns the URL that users should be redirected to after editing the given element.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
renderElements()
​
- Since
- 5.0.0
Renders the given elements using their partial templates.
If no partial template exists for an element, its string representation will be output instead.
Arguments ​
$elements
(craft\base\ElementInterface[])$variables
(array)
Returns ​
\Twig\Markup
Throws ​
rootElement()
​
DEPRECATED
Deprecated in 5.4.0. Use craft\base\ElementInterface::getRootOwner() instead.
- Since
- 3.2.0
Returns the root owner of a given element.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
rootElementIfCanonical()
​
- Since
- 5.0.0
Returns the root element of a given element, unless the element or any of its owners are not canonical.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
craft\base\ElementInterface, null
rootSourceKey()
​
- Since
- 3.7.25.1
Returns the root level source key for a given source key/path
Arguments ​
$sourceKey
(string)
Returns ​
searchableAttributes()
​
- Since
- 4.6.0
Returns the searchable attributes for a given element, ensuring that slug
and title
are included.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
string[]
setNextPrevOnElements()
​
Given an array of elements, will go through and set the appropriate "next" and "prev" elements on them.
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.
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.
Arguments ​
$element
(craft\base\ElementInterface)
Returns ​
siteStatusesForElement()
​
- Since
- 4.4.7
Returns the site statuses for a given element.
Arguments ​
$element
(craft\base\ElementInterface) – The element to return site statuses for$editableOnly
(boolean) – 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.
Arguments ​
$element
(craft\base\ElementInterface) – The source/draft/revision element$anySite
(boolean) – 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.
Arguments ​
$element
(craft\base\ElementInterface) – The element to return supported site info for$withUnpropagatedSites
(boolean) – Whether to include sites the element is currently not being propagated to
Returns ​
array[]
Throws ​
- yii\base\Exception
if any of the element’s supported sites are invalid
swapInProvisionalDrafts()
​
- Since
- 5.2.0
Swaps out any canonical elements with provisional drafts, when they exist.
Arguments ​
$elements
(craft\base\ElementInterface[])
tempSlug()
​
- Since
- 3.2.2
Generates a new temporary slug.
Returns ​
translationDescription()
​
- Since
- 3.5.0
Returns the description of a field’s translation support.
Arguments ​
$translationMethod
(string)
Returns ​
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.
Arguments ​
$element
(craft\base\ElementInterface)$translationMethod
(string)$translationKeyFormat
(string, null)
Returns ​
uriAttributeHtml()
​
- Since
- 5.5.0
Returns the HTML for URI attribute based on a value (text) and a URL it's supposed to link to.
Arguments ​
Returns ​
Constants ​
Constant | Description |
---|---|
URI_MAX_LENGTH |