Reference

Link

A Link represents one destination and its saved label, attributes and custom fields. Obtain one with entry.myLinkField.first() or by looping over a Hyper field. first() can return null, and the field itself is a LinkCollection.

Rendering Links provides template examples. This page describes the individual object and its saved content.

Properties

type

The link type’s class name, such as verbb\hyper\links\Entry. Built-in Classes lists the classes Hyper includes.

linkType

The link type object that supplies settings for this link.

url

The resolved destination, after Hyper applies the URL prefix, suffix and URL checks. This is null when the destination is absent or unavailable, and a suffix alone doesn’t create a destination. Stored destinations are literal text, so environment variables and Craft aliases aren’t expanded.

text

The label to show, using the entered Link Text, then layout defaults and type-specific fallbacks such as an entry’s title. Ordinary links return null without a usable URL, while Passive links can return a label without one.

target

The target attribute, which is _blank when the link opens in a new window and null otherwise.

newWindow

Whether the editor chose to open the link in a new window, or null when no choice was saved. target uses the resolved choice, including any field default.

linkUrl

The type-specific destination before Hyper adds the URL prefix and suffix.

linkUri

The URI of the linked element, or null when the link has no element or the element has no URI.

linkValue

The value stored for the link type, such as a URL string, an element ID, a site UID or embed metadata.

linkText

The Link Text, falling back to layout defaults and type-specific values such as an element’s title.

customLinkText

The Link Text an editor entered, with no fallbacks. This is null when the field is blank.

ariaLabel

The value for the link’s aria-label attribute.

urlSuffix

The suffix added to the URL, such as a query string or fragment.

linkTitle

The value for the link’s title attribute, which you can also read as title. This isn’t the title of a selected entry.

classes

The value for the link’s class attribute.

customAttributes

The Custom Attributes rows as they were saved, each with an attribute and a value key. These rows aren’t validated, so build markup from getCustomAttributes() or getLinkAttributes() instead.

Custom layout fields are accessible by their handles. Values on the selected destination are accessed through getElement() instead.

Methods

getElement()

Returns: craft\base\ElementInterface|null

Returns the linked Craft element, or null when there isn’t one or it doesn’t match the status. Element links require an enabled element by default, and Entry links require a live entry.

hasElement()

Returns: bool

Returns whether the linked element can be loaded with the requested status. Links that aren’t element links always return false.

Returns: Twig\Markup|null

Returns the link as an <a> element, or null when it has no usable URL. The special text key overrides its label. Ordinary strings are escaped, and trusted Twig markup is kept as it is.

Passed classes are added to saved classes. Other passed attributes override saved values with the same name.

getLinkAttributes()

Returns: Twig\Markup|array

Returns the link’s attributes as an array, or as Twig markup containing the attribute string when $asString is true. Use it to build your own anchor or button.

getCustomAttributes()

Returns the link’s Custom Attributes as attribute names and values, as getLink() and getLinkAttributes() render them. Invalid names and event-handler attributes such as onclick are left out, as are href, xlink:href, ping, formaction and action values whose URL scheme isn’t allowed. Rows named html or text are left out too, so passing the attributes to Craft’s tag() function keeps the label your template supplies. The saved rows are unchanged, and stay available as customAttributes.

getCustomLinkText()

Returns the Link Text an editor entered, with no fallbacks, or null when it’s blank.

isEmpty()

Returns whether the link has no usable URL. Labels, attributes and custom fields don’t count as a destination, so unavailable targets and Passive links are empty, although their stored content is kept.

Element links also expose linkSiteId, the selected destination’s site ID. For efficient access to their fields, see Loading Links.

getHtml()

Returns the stored embed HTML as Twig markup, or null when the link has no embed HTML.

getIframeSrc()

Returns the src of the first iframe in the embed HTML, or null when there isn’t one.

getEmbedImage()

Returns the embed’s thumbnail or image URL, or null when the embed has none.

getEmbedProviderName()

Returns the embed provider’s name, such as YouTube, or null when the link has no embed.

getData()

Returns all the stored embed metadata for an Embed link, or null when there is none. Only Embed links have this method.

For example, a URL link’s linkValue is an address string, an Entry link identifies an element, and an Embed link can store an object containing url, title, code and other provider metadata. Available embed keys depend on the fetched result.

Saved Content

Hyper stores each link’s content on its owner rather than saving a Craft element row for each link. A LinkInstance carries supported values while Links::createLinkFromInstance($field, $instance) creates the runtime Link object using its configured type and layout.

The supported content includes linkTypeHandle, uid, linkValue, linkSiteId, newWindow, linkText, ariaLabel, urlSuffix, linkTitle, classes, customAttributes and fields. Empty optional values may be omitted. Custom field values are stored by their layout placement UID; normal programmatic input can supply them by handle.

Use linkTypeHandle when selecting an exact configured type in a content array. Input also accepts handle, or type containing a registered class name or built-in type key. Keep field settings separate from saved link values; each saved link only needs to identify its configured type.

For conversions of raw values, use Managing Embedded Content. For normal element saves, follow Creating Links Programmatically.

Built-in Classes

All names below are in the verbb\hyper\links namespace. Types requiring another plugin are available only when their dependency is enabled.

ClassDestination
AssetCraft asset.
CalendarEventCalendar event.
CategoryCraft category.
CustomCustom address subject to the shared URI policy.
EmailEmail address.
EmbedRemote URL and its embed metadata.
EntryCraft entry.
FormieFormFormie form.
PassiveLabel without a destination URL.
PhonePhone number.
ProductCommerce product.
ShopifyProductShopify product.
SiteCraft site.
UrlRelative or absolute address.
UserCraft user.
VariantCommerce variant.