Link#

Whenever you're dealing with a link in your template, you're actually working with a Link object.

Link objects can be classified into two parts, the Link Type and the Link, which represent the settings and value respectively. All attributes and methods are available to you in templates, but you'll likely only need Link data for the front-end.

The Link Type represents the chosen type of link you want the link to be.

Attributes#

AttributeDescription
labelThe label for the link type.
handleThe handle for the link type. Automatically generated and cannot be changed.
enabledWhether the link type is enabled or not.
isCustomWhether this is a custom-created link type, or a default one.

Methods#

MethodDescription
getSettingsConfig()An array of variables for this link type, used in the field settings template and saved to the database.
getInputConfig()An array of variables for this link type, used in the field input Vue component.
getSerializedValues()An array of variables to save as the value of a link (its content) to the database.
getFieldLayout()Returns the field layout for fields and UI elements.

An Element Link is an extension of a regular Link object, and is inherited by all element-base link types like an Entry, Category, etc.

Attributes#

AttributeDescription
sourcesThe allowed sources for users to pick elements from.
selectionLabelThe text used for the Choose button to select an element.

The Link represents the actual value of the field, as you'd want to output in your template.

Attributes#

AttributeDescription
typeReturns the link type class name chosen for the link.
linkTypeReturns the link type chosen for the link.
urlThe value used for the href for the link. Supports .env variables and aliases, and combines any prefix or suffix.
textThe custom text for the label of the link. If an element link type, the title of the element will be used automatically.
targetReturns _blank if the link should open in a new window.
newWindowWhether the link should open in a new window.
linkUrlThe link URL. Supports .env variables and aliases.
linkValueThe value of the link. This will vary depending on the link type.
ariaLabelThe value for the aria-label attribute for the link.
urlSuffixThe suffix value to append to the URL.
linkTitleThe value for the title attribute for the link.
classesThe value for the class attribute for the link.
customAttributesAny custom attributes for the link.

Methods#

MethodDescription
getElement(status)Returns the linked element if an element-based link type. status can be supplied to filter based on the status (by default, only live elements will be returned).
hasElement(status)Returns whether linked to an element, or an element-based link type. status can be supplied to filter based on the status (by default, only live elements will be returned).
getLink(attributes)Returns an <a> anchor element. Pass in an array of attributes to override any.
getLinkAttributes(attributes, asString)Returns a collection of attributes to be used when creating an <a> HTML element. You can also have this returned as a string instead of an array.

An Element Link is an extension of a regular Link object, and is inherited by all element-base link types like an Entry, Category, etc.

Attributes#

AttributeDescription
linkSiteIdThe site ID for the chosen linked element.

An Embed Link contains extra content fetched from the link target.

Methods#

MethodDescription
getHtml()Returns the HTML code for rendering a preview of the media.
getData()Returns the raw data as fetched from the link target.

You can create Link objects programatically for cases where you might want to add links to a Hyper field in your own code. To do this, you'll need to create the Link object, and assign it to the field on the element that stores your Hyper field.

For example, let's say we have a Hyper field called ctaLink attached to an entry.

$value = new \verbb\hyper\links\Url();
$value->linkText = 'some text';
$value->linkValue = 'http://…';
$value->fields = [
    'myCustomField' => 'some value',
];

$entry->setFieldValue('ctaLink', [$value]);

Here, we set the type of link we want to use, the linkText and linkValue as applicable, and any custom fields (fields) that are set for the link type. We then use $entry->setFieldValue() or $entry->setFieldValues() to add that link to the field's value. Note that we always deal with an array of links!

Similarly, for an Entry link type:

$value = new \verbb\hyper\links\Entry();
$value->linkValue = 1234;

$entry->setFieldValue('ctaLink', [$value]);

Where the linkValue represents the ID of the entry you are linking to.

Previous ← Rendering Links Next Link Type →