You are viewing beta documentation for Hyper 3.x.
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; the field itself is a LinkCollection.

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

Properties

type

Type: string

Link class name, such as verbb\hyper\links\Entry.

linkType

Type: verbb\hyper\base\LinkInterface|null

Settings prototype for the link’s configured handle.

url

Type: string|null

Resolved destination after applying the prefix, suffix and URL-policy checks. Returns null when the destination is absent or unavailable; a suffix alone does not create a destination. Stored destinations are literal text; environment-variable references and Craft aliases are not expanded.

text

Type: string|null

Display label using entered text, layout defaults and type-specific fallbacks. Ordinary links return null without a usable URL. Passive links can return a label without a URL.

target

Type: string|null

_blank when the link opens in a new window; otherwise null.

newWindow

Type: bool|null

Saved new-window choice; target uses the resolved choice including applicable field defaults.

linkUrl

Type: string|null

Type-specific destination before prefix and suffix.

linkUri

Type: string|null

URI of the resolved element, if available.

linkValue

Type: mixed

Type-specific stored value: a URL string, element ID, site UID or embed metadata.

linkText

Type: string|null

Link Text with layout defaults and type-specific fallbacks.

customLinkText

Type: string|null

Only the editor-entered Link Text; null when blank.

ariaLabel

Type: string|null

HTML aria-label value.

urlSuffix

Type: string|null

Suffix such as a query string or fragment.

linkTitle

Type: string|null, title

HTML title value, not the title of a selected entry.

classes

Type: string|null

HTML class value.

customAttributes

Type: array

Additional HTML attribute name/value pairs, subject to attribute-name validation.

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

Methods

getElement($status)

Returns: craft\base\ElementInterface|null · Return and Behaviour: Selected Craft element or null. Entry links default to live entries; other element types use their applicable enabled status.

Selected Craft element or null. Entry links default to live entries; other element types use their applicable enabled status.

hasElement($status)

Returns: bool · Return and Behaviour: Whether the selected element can be resolved with the requested status.

Whether the selected element can be resolved with the requested status.

Returns: Twig\Markup|null · Return and Behaviour: Twig markup for an anchor, or null without a usable URL. The special text key overrides its label. Ordinary strings are escaped; trusted Twig markup is preserved.

Twig markup for an anchor, or null without a usable URL. The special text key overrides its label. Ordinary strings are escaped; trusted Twig markup is preserved.

getLinkAttributes(array $attributes = [], bool $asString = false)

Returns: Twig\Markup|array · Return and Behaviour: Attribute array, or Twig markup containing the attribute string when asString is true.

Attribute array, or Twig markup containing the attribute string when asString is true.

getCustomLinkText()

Returns: string|null · Return and Behaviour: Editor-entered text only, or null when blank.

Editor-entered text only, or null when blank.

isEmpty()

Returns: bool · Return and Behaviour: Whether the link resolves to no non-blank URL. Labels, attributes and custom fields do not make a destination-less link non-empty. Unavailable targets and Passive links are empty; stored content is still retained.

Whether the link resolves to no non-blank URL. Labels, attributes and custom fields do not make a destination-less link non-empty. Unavailable targets and Passive links are empty; stored content is still retained.

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

getHtml()

Returns: Twig\Markup|null · Return and Behaviour: Stored embed HTML as Twig markup, or null.

Stored embed HTML as Twig markup, or null.

getIframeSrc()

Returns: string|null · Return and Behaviour: First iframe source in stored embed HTML, or null.

First iframe source in stored embed HTML, or null.

getEmbedImage()

Returns: string|null · Return and Behaviour: Stored thumbnail/image URL, or null.

Stored thumbnail/image URL, or null.

getEmbedProviderName()

Returns: string|null · Return and Behaviour: Stored provider name, or null.

Stored provider name, or null.

getData()

Returns: array|null

Available on Embed links.

Return and Behaviour: Embed metadata on an Embed link.

Embed metadata on an Embed link.

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. Field settings and the content type identifier are separate contracts; do not put a complete type definition into each content row.

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.