PHPackages                             sebastianlenz/linkfield - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sebastianlenz/linkfield

ActiveCraft-plugin[Utility &amp; Helpers](/categories/utility)

sebastianlenz/linkfield
=======================

A Craft field type for selecting links

2.1.5(3y ago)1191.0M—3%64[38 issues](https://github.com/sebastian-lenz/craft-linkfield/issues)[3 PRs](https://github.com/sebastian-lenz/craft-linkfield/pulls)18MITPHPPHP ^8.0

Since Jan 2Pushed 1y ago3 watchersCompare

[ Source](https://github.com/sebastian-lenz/craft-linkfield)[ Packagist](https://packagist.org/packages/sebastianlenz/linkfield)[ RSS](/packages/sebastianlenz-linkfield/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (49)Used By (18)

Link field plugin for Craft
===========================

[](#link-field-plugin-for-craft)

This plugin adds a new link field type to the Craft CMS. The link field allows content editors to choose from a list of link types and offers individual input fields for each of them.

Requirements
------------

[](#requirements)

This plugin requires Craft CMS 4.0.0 or later.

Installation
------------

[](#installation)

The plugin can be installed from the integrated plugin store by searching for "Typed Link Field" or using Composer:

1. Open your terminal and navigate to your Craft project:

    ```
    cd /path/to/project

    ```
2. Then tell Composer to load the plugin:

    ```
    composer require sebastianlenz/linkfield

    ```
3. Finally, install and enable the plugin:

    ```
    ./craft plugin/install typedlinkfield
    ./craft plugin/enable typedlinkfield

    ```

Usage
-----

[](#usage)

After the plugin has been installed, link fields can be created using the field settings within the control panel. All field settings can be found within the field manager.

Templating
----------

[](#templating)

Link fields can be rendered directly in Twig, they return the url the link is pointing to, or an empty string if they are unset:

```
Link
```

The field value is actually an instance of `lenz\linkfield\models\Link` which exposes additional properties and methods that can be used in templates. Depending on the link type, a more specific subclass will be returned.

### Render methods

[](#render-methods)

#### getLink($attributesOrText = null)

[](#getlinkattributesortext--null)

Renders a full html link using the attribute and content data of the field.

```
{{ entry.myLinkField.getLink() }}
```

To modify the inner text of the link tag, the desired content can be passed:

```
{{ entry.myLinkField.getLink('Imprint') }}
```

To modify the attributes of the link, an object can be passed. The special attribute `text` will be used as the inner text.

```
{{ entry.myLinkField.getLink({
  class: 'my-link-class',
  target: '_blank',
  text: 'Imprint',
}) }}
```

#### getLinkAttributes($extraAttributes = null)

[](#getlinkattributesextraattributes--null)

Renders only the link attributes. Attributes can be modified or appended by passing an object to the first argument.

```

  Custom markup

```

#### getRawLinkAttributes($extraAttributes = null)

[](#getrawlinkattributesextraattributes--null)

Returns the attributes of the link as an array. Can be used in junction with the `attr` or `tag` helpers exposed by Craft.

```
{% tag 'a' with entry.myLinkField.getRawLinkAttributes() %}
  Custom markup
{% endtag %}
```

### Helper methods

[](#helper-methods)

#### getAllowCustomText()

[](#getallowcustomtext)

Returns whether the field allows users to enter custom texts.

```
{{ entry.myLinkField.getAllowCustomText() }}
```

#### getAllowTarget()

[](#getallowtarget)

Returns whether the field shows the option for opening links in a new window.

```
{{ entry.myLinkField.getAllowTarget() }}
```

#### getAriaLabel()

[](#getarialabel)

Returns the aria label of the link.

```
{{ entry.myLinkField.getAriaLabel() }}
```

#### getCustomText($fallbackText = '')

[](#getcustomtextfallbacktext--)

Returns the custom text of the link. The first non-empty text from the following possibilities will be picked:

1. Custom text of the link.
2. Default text defined in the field settings.
3. Fallback text as passed to the function.

```
{{ entry.myLinkField.getCustomText('My fallback text') }}
```

#### getDefaultText()

[](#getdefaulttext)

Returns the default text set in the link field settings of this link.

```
{{ entry.myLinkField.getDefaultText() }}
```

#### getElement($ignoreStatus = false)

[](#getelementignorestatus--false)

Returns the linked element (entry, asset, etc.) or `NULL` if no element is linked.

By default, only published elements are returned. Set `$ignoreStatus`to `TRUE` to retrieve unpublished elements.

```
{{ entry.myLinkField.getElement() }}
```

#### getEnableAriaLabel()

[](#getenablearialabel)

Returns whether the field allows users to enter aria labels.

```
{{ entry.myLinkField.getEnableAriaLabel() }}
```

#### getEnableTitle()

[](#getenabletitle)

Returns whether the field allows users to enter link titles.

```
{{ entry.myLinkField.getEnableTitle() }}
```

#### getIntrinsicText()

[](#getintrinsictext)

Returns the text that is declared by the link itself (e.g. the title of the linked entry or asset).

```
{{ entry.myLinkField.getIntrinsicText() }}
```

#### getIntrinsicUrl()

[](#getintrinsicurl)

Returns the url that is declared by the link itself (e.g. the url of the linked entry or asset). Custom queries or hashes are taken into account and will be appended to the result.

```
{{ entry.myLinkField.getIntrinsicUrl() }}
```

#### getTarget()

[](#gettarget)

Returns the link target (e.g. `_blank`).

```
{{ entry.myLinkField.getTarget() }}
```

#### getText($fallbackText = "Learn More")

[](#gettextfallbacktext--learn-more)

Returns the link text. The first non-empty text from the following possibilities will be picked:

1. Custom text of the link.
2. Intrinsic text defined by the linked element.
3. Default text defined in the field settings.
4. Fallback text as passed to the function.

```
{{ entry.myLinkField.getText($fallbackText = "Learn More") }}
```

#### getType()

[](#gettype)

Returns a string that indicates the type of the link. The plugin ships with the following link types: `asset`, `category`, `custom`, `email`, `entry`, `site`, `tel`, `url` and `user`.

```
{{ entry.myLinkField.getType() }}
```

#### getUrl($options = null)

[](#geturloptions--null)

Returns the url of the link.

```
{{ entry.myLinkField.getUrl() }}
```

Allows the link to be modified by overwriting url attributes as returned by the php function `parse_url`. The following options are supported: `fragment`, `host`, `pass`, `path`, `port`, `query`, `scheme` and `user`.

- All options require a string value to be passed.
- The `query` option accepts an array or hash map. If an array is passed, the query parameters of the original url will be merged by default. To disable this behaviour, the option `queryMode` must be set to `replace`.

This example enforces the https scheme and replaces all query parameters of the original url:

```
{{ entry.myLinkField.getUrl({
  scheme: 'https',
  queryMode: 'replace',
  query: {
    param: 'value'
  },
}) }}
```

#### hasElement($ignoreStatus = false)

[](#haselementignorestatus--false)

Returns whether the link points to an element (e.g. entry or asset).

```
{{ entry.myLinkField.hasElement() }}
```

#### isEmpty()

[](#isempty)

Returns whether the link is empty. An empty link is a link that has no url.

```
{{ entry.myLinkField.isEmpty() }}
```

### Properties

[](#properties)

The properties generally expose the raw underlying data of the link.

#### ariaLabel

[](#arialabel)

The aria label as entered in th cp.

```
...
```

#### customText

[](#customtext)

The custom text as entered in th cp.

```
{{ entry.myLinkField.customText }}
```

#### target

[](#target)

The link target as text. Can be either `_blank` or an empty string.

```
...
```

#### title

[](#title)

The title as entered in th cp.

```
...
```

Eager-Loading
-------------

[](#eager-loading)

Link fields can be eager-loaded using Crafts `with` query parameter. Eager-loading can greatly improve the performance when fetching many entries, e.g. when rendering menus.

```
{% set entries = craft.entries({
  section: 'pages',
  with: 'myLinkField',
}).all() %}
```

API
---

[](#api)

You can register additional link types by listening to the `EVENT_REGISTER_LINK_TYPES`event of the plugin. If you just want to add another element type, you can do it like this in your module:

```
use craft\commerce\elements\Product;
use lenz\linkfield\Plugin as LinkPlugin;
use lenz\linkfield\events\LinkTypeEvent;
use lenz\linkfield\models\element\ElementLinkType;
use yii\base\Event;

/**
 * Custom module class.
 */
class Module extends \yii\base\Module
{
  public function init() {
    parent::init();
    Event::on(
      LinkPlugin::class,
      LinkPlugin::EVENT_REGISTER_LINK_TYPES,
      function(LinkTypeEvent $event) {
        $event->linkTypes['product'] = new ElementLinkType(Product::class);
      }
    );
  }
}
```

Each link type must have an unique name and a definition object extending `lenz\linkfield\models\LinkType`. Take a look at the bundled link types `ElementLinkType` and `InputLinkType` to get an idea of how to write your own link type definitions.

Remarks
-------

[](#remarks)

### Upgrading from Fruit Link It

[](#upgrading-from-fruit-link-it)

If wish to migrate a field created using "Fruit Link It", please follow the discussion and directions here:

[\#51 (comment)](https://github.com/sebastian-lenz/craft-linkfield/issues/51#issuecomment-538782716)

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance24

Infrequent updates — may be unmaintained

Popularity55

Moderate usage in the ecosystem

Community38

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 88.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~50 days

Recently: every ~190 days

Total

48

Last Release

711d ago

Major Versions

1.0.23 → 2.0.0-beta.92020-06-09

1.0.24 → 2.0.0-beta.112020-12-02

1.0.25 → 2.0.0-rc.12021-03-05

v1.0.x-dev → 2.1.1-rc2022-04-21

2.1.5 → 3.0.0-beta2024-06-07

PHP version history (5 changes)1.0.1PHP ^7.0

1.0.25PHP ^7.0 || ^8.0

2.0.0-rc.2PHP ^7.2.5 || ^8.0

2.1.0-rcPHP ^8.0

3.0.0-betaPHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/ea15c31a1580406c774c6e40609e977e82ffaaf2e17ddec29564456f9c574fbd?d=identicon)[sebastian-lenz](/maintainers/sebastian-lenz)

---

Top Contributors

[![sebastian-lenz](https://avatars.githubusercontent.com/u/2273359?v=4)](https://github.com/sebastian-lenz "sebastian-lenz (269 commits)")[![timkelty](https://avatars.githubusercontent.com/u/18329?v=4)](https://github.com/timkelty "timkelty (5 commits)")[![internetztube](https://avatars.githubusercontent.com/u/6382364?v=4)](https://github.com/internetztube "internetztube (3 commits)")[![mitch-rickman](https://avatars.githubusercontent.com/u/13687016?v=4)](https://github.com/mitch-rickman "mitch-rickman (3 commits)")[![JorgeAnzola](https://avatars.githubusercontent.com/u/15676614?v=4)](https://github.com/JorgeAnzola "JorgeAnzola (2 commits)")[![d--j](https://avatars.githubusercontent.com/u/100674?v=4)](https://github.com/d--j "d--j (2 commits)")[![pascalminator](https://avatars.githubusercontent.com/u/13754636?v=4)](https://github.com/pascalminator "pascalminator (2 commits)")[![arthur-akufen](https://avatars.githubusercontent.com/u/93678858?v=4)](https://github.com/arthur-akufen "arthur-akufen (1 commits)")[![holmey](https://avatars.githubusercontent.com/u/55244?v=4)](https://github.com/holmey "holmey (1 commits)")[![boboldehampsink](https://avatars.githubusercontent.com/u/378974?v=4)](https://github.com/boboldehampsink "boboldehampsink (1 commits)")[![JeroenJRP](https://avatars.githubusercontent.com/u/15321825?v=4)](https://github.com/JeroenJRP "JeroenJRP (1 commits)")[![jeroenlammerts](https://avatars.githubusercontent.com/u/4282711?v=4)](https://github.com/jeroenlammerts "jeroenlammerts (1 commits)")[![angrybrad](https://avatars.githubusercontent.com/u/61869?v=4)](https://github.com/angrybrad "angrybrad (1 commits)")[![KevinBeckers](https://avatars.githubusercontent.com/u/4037137?v=4)](https://github.com/KevinBeckers "KevinBeckers (1 commits)")[![leecrosdale](https://avatars.githubusercontent.com/u/12886732?v=4)](https://github.com/leecrosdale "leecrosdale (1 commits)")[![mia-tpa](https://avatars.githubusercontent.com/u/107607767?v=4)](https://github.com/mia-tpa "mia-tpa (1 commits)")[![michtio](https://avatars.githubusercontent.com/u/5818021?v=4)](https://github.com/michtio "michtio (1 commits)")[![bartrylant](https://avatars.githubusercontent.com/u/1434451?v=4)](https://github.com/bartrylant "bartrylant (1 commits)")[![nfourtythree](https://avatars.githubusercontent.com/u/266453?v=4)](https://github.com/nfourtythree "nfourtythree (1 commits)")[![nstCactus](https://avatars.githubusercontent.com/u/353843?v=4)](https://github.com/nstCactus "nstCactus (1 commits)")

---

Tags

craftfieldtype

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sebastianlenz-linkfield/health.svg)

```
[![Health](https://phpackages.com/badges/sebastianlenz-linkfield/health.svg)](https://phpackages.com/packages/sebastianlenz-linkfield)
```

###  Alternatives

[spicyweb/craft-neo

A Matrix-like field type with block hierarchy

395798.1k10](/packages/spicyweb-craft-neo)[verbb/formie

The most user-friendly forms plugin for Craft.

101372.9k40](/packages/verbb-formie)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

52664.9k12](/packages/solspace-craft-freeform)[supercool/tablemaker

Create customizable and user-defined table fields.

40141.7k](/packages/supercool-tablemaker)[verbb/vizy

A flexible visual editor field for Craft.

4348.6k](/packages/verbb-vizy)[verbb/social-poster

Automatically post entries to social media.

918.5k](/packages/verbb-social-poster)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
