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
createSlug() Creates a slug based on a given string.
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.
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.
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.
isTempSlug() Returns whether the given slug is temporary.
normalizeSlug() Normalizes a slug.
rootElement() Returns the root element of a given element.
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.
sourceElement() Returns the element, or if it’s a draft/revision, the source 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.

# createSlug()

DEPRECATED

Deprecated in 3.5.0. Use normalizeSlug() instead.

Creates a slug based on a given string.

View source (opens new window)

Arguments

Returns

string (opens new window)

# doesUriFormatHaveSlugTag()

Returns whether a given URL format has a proper {slug} tag.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# editableSiteIdsForElement()

Returns the editable site IDs for a given element, taking user permissions into account.

View source (opens new window)

Arguments

Returns

array (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

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

Returns

string (opens new window)

# 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

Returns

boolean (opens new window)

# isElementEditable()

Returns whether the given element is editable by the current user, taking user permissions into account.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# isTempSlug()

Since
3.2.2

Returns whether the given slug is temporary.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# normalizeSlug()

Since
3.5.0

Normalizes a slug.

View source (opens new window)

Arguments

Returns

string (opens new window)

# rootElement()

Since
3.2.0

Returns the root element of a given element.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface

# 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

# 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

Throws

# sourceElement()

Since
3.3.0

Returns the element, or if it’s a draft/revision, the source element.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface, null (opens new window)

# 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

Returns

array (opens new window)

Throws

# tempSlug()

Since
3.2.2

Generates a new temporary slug.

View source (opens new window)

Returns

string (opens new window)

# translationDescription()

Since
3.5.0

Returns the description of a field’s translation support.

View source (opens new window)

Arguments

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

Returns

string (opens new window)